You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

  1. const description =
  2. ' See https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.'
  3. warnCjsUsage()
  4. // type utils
  5. module.exports.defineConfig = (config) => config
  6. // proxy cjs utils (sync functions)
  7. Object.assign(module.exports, require('./dist/node-cjs/publicUtils.cjs'))
  8. // async functions, can be redirect from ESM build
  9. const asyncFunctions = [
  10. 'build',
  11. 'createServer',
  12. 'preview',
  13. 'transformWithEsbuild',
  14. 'resolveConfig',
  15. 'optimizeDeps',
  16. 'formatPostcssSourceMap',
  17. 'loadConfigFromFile',
  18. 'preprocessCSS',
  19. 'createBuilder',
  20. 'runnerImport',
  21. ]
  22. asyncFunctions.forEach((name) => {
  23. module.exports[name] = (...args) =>
  24. import('./dist/node/index.js').then((i) => i[name](...args))
  25. })
  26. // variables and sync functions that cannot be used from cjs build
  27. const disallowedVariables = [
  28. // was not exposed in cjs from the beginning
  29. 'parseAst',
  30. 'parseAstAsync',
  31. 'buildErrorMessage',
  32. 'sortUserPlugins',
  33. // Environment API related variables that are too big to include in the cjs build
  34. 'DevEnvironment',
  35. 'BuildEnvironment',
  36. 'createIdResolver',
  37. 'createRunnableDevEnvironment',
  38. // can be redirected from ESM, but doesn't make sense as it's Environment API related
  39. 'fetchModule',
  40. 'moduleRunnerTransform',
  41. // can be exposed, but doesn't make sense as it's Environment API related
  42. 'createServerHotChannel',
  43. 'createServerModuleRunner',
  44. 'createServerModuleRunnerTransport',
  45. 'isRunnableDevEnvironment',
  46. ]
  47. disallowedVariables.forEach((name) => {
  48. Object.defineProperty(module.exports, name, {
  49. get() {
  50. throw new Error(
  51. `${name} is not available in the CJS build of Vite.` + description,
  52. )
  53. },
  54. })
  55. })
  56. function warnCjsUsage() {
  57. if (process.env.VITE_CJS_IGNORE_WARNING) return
  58. const logLevelIndex = process.argv.findIndex((arg) =>
  59. /^(?:-l|--logLevel)/.test(arg),
  60. )
  61. if (logLevelIndex > 0) {
  62. const logLevelValue = process.argv[logLevelIndex + 1]
  63. if (logLevelValue === 'silent' || logLevelValue === 'error') {
  64. return
  65. }
  66. if (/silent|error/.test(process.argv[logLevelIndex])) {
  67. return
  68. }
  69. }
  70. const yellow = (str) => `\u001b[33m${str}\u001b[39m`
  71. console.warn(
  72. yellow("The CJS build of Vite's Node API is deprecated." + description),
  73. )
  74. if (process.env.VITE_CJS_TRACE) {
  75. const e = {}
  76. const stackTraceLimit = Error.stackTraceLimit
  77. Error.stackTraceLimit = 100
  78. Error.captureStackTrace(e)
  79. Error.stackTraceLimit = stackTraceLimit
  80. console.log(
  81. e.stack
  82. .split('\n')
  83. .slice(1)
  84. .filter((line) => !line.includes('(node:'))
  85. .join('\n'),
  86. )
  87. }
  88. }