vite icon indicating copy to clipboard operation
vite copied to clipboard

fix: increase CJS warning trace depth

Open KaelWD opened this issue 1 year ago • 2 comments

Description

VITE_CJS_TRACE=true doesn't give any useful information currently, it's all node:internal/modules/cjs/loader methods.

KaelWD avatar Aug 23 '24 06:08 KaelWD

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Good point. I don't think I've thoroughly tested this when I implemented it. I'm trying something locally to also omit the node: stuff from the stack trace, maybe this would be better?

function warnCjsUsage() {
  if (process.env.VITE_CJS_IGNORE_WARNING) return
  const yellow = (str) => `\u001b[33m${str}\u001b[39m`
  console.warn(
    yellow(
      `The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.`,
    ),
  )
  if (process.env.VITE_CJS_TRACE) {
    const e = {}
    const stackTraceLimit = Error.stackTraceLimit
    Error.stackTraceLimit = 100
    Error.captureStackTrace(e)
    Error.stackTraceLimit = stackTraceLimit
    console.log(
      e.stack
        .split('\n')
        .slice(1)
        .map((line) => {
          if (line.includes('(node:')) return
          return line
        })
        .filter(Boolean)
        .join('\n'),
    )
  }
}

Result:

The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
    at warnCjsUsage (/Users/bjorn/Work/oss/vite/packages/vite/index.cjs:38:11)
    at Object.<anonymous> (/Users/bjorn/Work/oss/vite/packages/vite/index.cjs:1:1)
    at Object._require.extensions.<computed> [as .js] (file:///Users/bjorn/Work/oss/vite/packages/vite/dist/node/chunks/dep-B1T3k7dc.js:66593:9)
    at Object.<anonymous> (/Users/bjorn/Work/oss/vite/playground/cli/vite.config.cjs:3:24)
    at Object._require.extensions.<computed> [as .js] (file:///Users/bjorn/Work/oss/vite/packages/vite/dist/node/chunks/dep-B1T3k7dc.js:66591:16)
    at loadConfigFromBundledFile (file:///Users/bjorn/Work/oss/vite/packages/vite/dist/node/chunks/dep-B1T3k7dc.js:66597:17)
    at loadConfigFromFile (file:///Users/bjorn/Work/oss/vite/packages/vite/dist/node/chunks/dep-B1T3k7dc.js:66420:24)
    at resolveConfig (file:///Users/bjorn/Work/oss/vite/packages/vite/dist/node/chunks/dep-B1T3k7dc.js:66028:24)
    at _createServer (file:///Users/bjorn/Work/oss/vite/packages/vite/dist/node/chunks/dep-B1T3k7dc.js:62653:18)
    at CAC.<anonymous> (file:///Users/bjorn/Work/oss/vite/packages/vite/dist/node/cli.js:735:20)

bluwy avatar Aug 23 '24 07:08 bluwy

Yeah that would be even better actually, good idea.

.map().filter() is weird though, why not just filter?

KaelWD avatar Aug 23 '24 15:08 KaelWD

Oh true. Yeah it can be simplified with only a filter

bluwy avatar Aug 23 '24 15:08 bluwy