index.cjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. warnCjsUsage()
  2. // type utils
  3. module.exports.defineConfig = (config) => config
  4. // proxy cjs utils (sync functions)
  5. Object.assign(module.exports, require('./dist/node-cjs/publicUtils.cjs'))
  6. // async functions, can be redirect from ESM build
  7. const asyncFunctions = [
  8. 'build',
  9. 'createServer',
  10. 'preview',
  11. 'transformWithEsbuild',
  12. 'resolveConfig',
  13. 'optimizeDeps',
  14. 'formatPostcssSourceMap',
  15. 'loadConfigFromFile',
  16. 'preprocessCSS',
  17. ]
  18. asyncFunctions.forEach((name) => {
  19. module.exports[name] = (...args) =>
  20. import('./dist/node/index.js').then((i) => i[name](...args))
  21. })
  22. function warnCjsUsage() {
  23. if (process.env.VITE_CJS_IGNORE_WARNING) return
  24. const logLevelIndex = process.argv.findIndex((arg) =>
  25. /^(?:-l|--logLevel)/.test(arg),
  26. )
  27. if (logLevelIndex > 0) {
  28. const logLevelValue = process.argv[logLevelIndex + 1]
  29. if (logLevelValue === 'silent' || logLevelValue === 'error') {
  30. return
  31. }
  32. if (/silent|error/.test(process.argv[logLevelIndex])) {
  33. return
  34. }
  35. }
  36. const yellow = (str) => `\u001b[33m${str}\u001b[39m`
  37. console.warn(
  38. yellow(
  39. `The CJS build of Vite's Node API is deprecated. See https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.`,
  40. ),
  41. )
  42. if (process.env.VITE_CJS_TRACE) {
  43. const e = {}
  44. const stackTraceLimit = Error.stackTraceLimit
  45. Error.stackTraceLimit = 100
  46. Error.captureStackTrace(e)
  47. Error.stackTraceLimit = stackTraceLimit
  48. console.log(
  49. e.stack
  50. .split('\n')
  51. .slice(1)
  52. .filter((line) => !line.includes('(node:'))
  53. .join('\n'),
  54. )
  55. }
  56. }