index.cjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. 'createFetchableDevEnvironment',
  47. 'isFetchableDevEnvironment',
  48. ]
  49. disallowedVariables.forEach((name) => {
  50. Object.defineProperty(module.exports, name, {
  51. get() {
  52. throw new Error(
  53. `${name} is not available in the CJS build of Vite.` + description,
  54. )
  55. },
  56. })
  57. })
  58. function warnCjsUsage() {
  59. if (process.env.VITE_CJS_IGNORE_WARNING) return
  60. const logLevelIndex = process.argv.findIndex((arg) =>
  61. /^(?:-l|--logLevel)/.test(arg),
  62. )
  63. if (logLevelIndex > 0) {
  64. const logLevelValue = process.argv[logLevelIndex + 1]
  65. if (logLevelValue === 'silent' || logLevelValue === 'error') {
  66. return
  67. }
  68. if (/silent|error/.test(process.argv[logLevelIndex])) {
  69. return
  70. }
  71. }
  72. const yellow = (str) => `\u001b[33m${str}\u001b[39m`
  73. console.warn(
  74. yellow("The CJS build of Vite's Node API is deprecated." + description),
  75. )
  76. if (process.env.VITE_CJS_TRACE) {
  77. const e = {}
  78. const stackTraceLimit = Error.stackTraceLimit
  79. Error.stackTraceLimit = 100
  80. Error.captureStackTrace(e)
  81. Error.stackTraceLimit = stackTraceLimit
  82. console.log(
  83. e.stack
  84. .split('\n')
  85. .slice(1)
  86. .filter((line) => !line.includes('(node:'))
  87. .join('\n'),
  88. )
  89. }
  90. }