Skip to content

feat(babel-preset): set target to node whenever NODE_ENV === 'test' #4663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/@vue/babel-preset-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ module.exports = (context, options = {}) => {
const plugins = []
const defaultEntryFiles = JSON.parse(process.env.VUE_CLI_ENTRY_FILES || '[]')

// Though in the vue-cli repo, we only use the two envrionment variables
// for tests, users may have relied on them for some features,
// dropping them may break some projects.
// So in the following blocks we don't directly test the `NODE_ENV`.
// Rather, we turn it into the two commonly used feature flags.
if (process.env.NODE_ENV === 'test') {
// Both Jest & Mocha set NODE_ENV to 'test'.
// And both requires the `node` target.
process.env.VUE_CLI_BABEL_TARGET_NODE = 'true'
// Jest runs without bundling so it needs this.
// With the node target, tree shaking is not a necessity,
// so we set it for maximum compatibility.
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = 'true'
}

// JSX
if (options.jsx !== false) {
presets.push([require('@vue/babel-preset-jsx'), typeof options.jsx === 'object' ? options.jsx : {}])
Expand Down Expand Up @@ -134,8 +149,10 @@ module.exports = (context, options = {}) => {
// cli-plugin-jest sets this to true because Jest runs without bundling
if (process.env.VUE_CLI_BABEL_TRANSPILE_MODULES) {
envOptions.modules = 'commonjs'
// necessary for dynamic import to work in tests
plugins.push(require('babel-plugin-dynamic-import-node'))
if (process.env.VUE_CLI_BABEL_TARGET_NODE) {
// necessary for dynamic import to work in tests
plugins.push(require('babel-plugin-dynamic-import-node'))
}
}

// pass options along to babel-preset-env
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-unit-jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = api => {
`All jest command line options are supported.\n` +
`See https://facebook.github.io/jest/docs/en/cli.html for more details.`
}, (args, rawArgv) => {
// for @vue/babel-preset-app
// for @vue/babel-preset-app <= v4.0.0-rc.7
process.env.VUE_CLI_BABEL_TARGET_NODE = true
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true
require('jest').run(rawArgv)
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-unit-mocha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = api => {
if (inspectPos !== -1) {
nodeArgs = rawArgv.splice(inspectPos, inspectPos + 1)
}
// for @vue/babel-preset-app
// for @vue/babel-preset-app <= v4.0.0-rc.7
process.env.VUE_CLI_BABEL_TARGET_NODE = true
// start runner
const { execa } = require('@vue/cli-shared-utils')
Expand Down