Skip to content

Commit da9269b

Browse files
authored
feat(babel-preset): set target to node whenever NODE_ENV === 'test' (#4663)
Typically only unit test frameworks do so.
1 parent 617dd04 commit da9269b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

packages/@vue/babel-preset-app/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

packages/@vue/cli-plugin-unit-jest/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = api => {
99
`All jest command line options are supported.\n` +
1010
`See https://facebook.github.io/jest/docs/en/cli.html for more details.`
1111
}, (args, rawArgv) => {
12-
// for @vue/babel-preset-app
12+
// for @vue/babel-preset-app <= v4.0.0-rc.7
1313
process.env.VUE_CLI_BABEL_TARGET_NODE = true
1414
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true
1515
require('jest').run(rawArgv)

packages/@vue/cli-plugin-unit-mocha/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = api => {
4242
if (inspectPos !== -1) {
4343
nodeArgs = rawArgv.splice(inspectPos, inspectPos + 1)
4444
}
45-
// for @vue/babel-preset-app
45+
// for @vue/babel-preset-app <= v4.0.0-rc.7
4646
process.env.VUE_CLI_BABEL_TARGET_NODE = true
4747
// start runner
4848
const { execa } = require('@vue/cli-shared-utils')

0 commit comments

Comments
 (0)