@@ -32,6 +32,21 @@ module.exports = (context, options = {}) => {
3232 const plugins = [ ]
3333 const defaultEntryFiles = JSON . parse ( process . env . VUE_CLI_ENTRY_FILES || '[]' )
3434
35+ // Though in the vue-cli repo, we only use the two envrionment variables
36+ // for tests, users may have relied on them for some features,
37+ // dropping them may break some projects.
38+ // So in the following blocks we don't directly test the `NODE_ENV`.
39+ // Rather, we turn it into the two commonly used feature flags.
40+ if ( process . env . NODE_ENV === 'test' ) {
41+ // Both Jest & Mocha set NODE_ENV to 'test'.
42+ // And both requires the `node` target.
43+ process . env . VUE_CLI_BABEL_TARGET_NODE = 'true'
44+ // Jest runs without bundling so it needs this.
45+ // With the node target, tree shaking is not a necessity,
46+ // so we set it for maximum compatibility.
47+ process . env . VUE_CLI_BABEL_TRANSPILE_MODULES = 'true'
48+ }
49+
3550 // JSX
3651 if ( options . jsx !== false ) {
3752 presets . push ( [ require ( '@vue/babel-preset-jsx' ) , typeof options . jsx === 'object' ? options . jsx : { } ] )
@@ -134,8 +149,10 @@ module.exports = (context, options = {}) => {
134149 // cli-plugin-jest sets this to true because Jest runs without bundling
135150 if ( process . env . VUE_CLI_BABEL_TRANSPILE_MODULES ) {
136151 envOptions . modules = 'commonjs'
137- // necessary for dynamic import to work in tests
138- plugins . push ( require ( 'babel-plugin-dynamic-import-node' ) )
152+ if ( process . env . VUE_CLI_BABEL_TARGET_NODE ) {
153+ // necessary for dynamic import to work in tests
154+ plugins . push ( require ( 'babel-plugin-dynamic-import-node' ) )
155+ }
139156 }
140157
141158 // pass options along to babel-preset-env
0 commit comments