source: node_modules/vite/index.cjs

Last change on this file was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

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