1
+ const description =
2
+ ' See https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.'
3
+
1
4
warnCjsUsage ( )
2
5
3
6
// type utils
@@ -17,12 +20,43 @@ const asyncFunctions = [
17
20
'formatPostcssSourceMap' ,
18
21
'loadConfigFromFile' ,
19
22
'preprocessCSS' ,
23
+ 'createBuilder' ,
20
24
]
21
25
asyncFunctions . forEach ( ( name ) => {
22
26
module . exports [ name ] = ( ...args ) =>
23
27
import ( './dist/node/index.js' ) . then ( ( i ) => i [ name ] ( ...args ) )
24
28
} )
25
29
30
+ // variables and sync functions that cannot be used from cjs build
31
+ const disallowedVariables = [
32
+ // was not exposed in cjs from the beginning
33
+ 'parseAst' ,
34
+ 'parseAstAsync' ,
35
+ 'buildErrorMessage' ,
36
+ 'sortUserPlugins' ,
37
+ // Environment API related variables that are too big to include in the cjs build
38
+ 'DevEnvironment' ,
39
+ 'BuildEnvironment' ,
40
+ 'createIdResolver' ,
41
+ 'createRunnableDevEnvironment' ,
42
+ // can be redirected from ESM, but doesn't make sense as it's Environment API related
43
+ 'fetchModule' ,
44
+ 'moduleRunnerTransform' ,
45
+ // can be exposed, but doesn't make sense as it's Environment API related
46
+ 'createServerHotChannel' ,
47
+ 'createServerModuleRunner' ,
48
+ 'isRunnableDevEnvironment' ,
49
+ ]
50
+ disallowedVariables . forEach ( ( name ) => {
51
+ Object . defineProperty ( module . exports , name , {
52
+ get ( ) {
53
+ throw new Error (
54
+ `${ name } is not available in the CJS build of Vite.` + description ,
55
+ )
56
+ } ,
57
+ } )
58
+ } )
59
+
26
60
function warnCjsUsage ( ) {
27
61
if ( process . env . VITE_CJS_IGNORE_WARNING ) return
28
62
const logLevelIndex = process . argv . findIndex ( ( arg ) =>
@@ -39,9 +73,7 @@ function warnCjsUsage() {
39
73
}
40
74
const yellow = ( str ) => `\u001b[33m${ str } \u001b[39m`
41
75
console . warn (
42
- yellow (
43
- `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.` ,
44
- ) ,
76
+ yellow ( "The CJS build of Vite's Node API is deprecated." + description ) ,
45
77
)
46
78
if ( process . env . VITE_CJS_TRACE ) {
47
79
const e = { }
0 commit comments