Skip to content
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

refactor: use babel overrides to transpile babel runtime helpers #4777

Merged
merged 6 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refactor: use babel overrides to transpile babel runtime helpers
As recommended in babel/babel#9903.
Get rid of the module-resolver plugin, may fix #3928.
Seems to have fixed #4742 as well.
  • Loading branch information
haoqunjiang committed Oct 30, 2019
commit c6cd32213f2eadc4ea305d72a7974b7388326473
37 changes: 19 additions & 18 deletions packages/@vue/babel-preset-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,25 @@ module.exports = (context, options = {}) => {
absoluteRuntime
}])

// use @babel/runtime-corejs3 so that helpers that need polyfillable APIs will reference core-js instead.
// if useBuiltIns is not set to 'usage', then it means users would take care of the polyfills on their own,
// i.e., core-js 3 is no longer needed.
// this extra plugin can be removed once one of the two issues resolves:
// https://github.com/babel/babel/issues/7597
// https://github.com/babel/babel/issues/9903
if (useBuiltIns === 'usage' && !process.env.VUE_CLI_MODERN_BUILD) {
const runtimeCoreJs3Path = path.dirname(require.resolve('@babel/runtime-corejs3/package.json'))
plugins.push([require('babel-plugin-module-resolver'), {
alias: {
'@babel/runtime': '@babel/runtime-corejs3',
[runtimePath]: runtimeCoreJs3Path
}
}])
}

return {
presets,
plugins
overrides: [{
exclude: [/@babel\/runtime/, /core-js/],
presets,
plugins
}, {
// there are some untranspiled code in @babel/runtime
// https://github.com/babel/babel/issues/9903
include: [/@babel\/runtime/],
presets: [
[require('@babel/preset-env'), {
useBuiltIns,
corejs: 3
}]
]
}]
}
}

// a special flag to tell @vue/cli-plugin-babel to include @babel/runtime for transpilation
// otherwise the above `include` option won't take effect
process.env.VUE_CLI_TRANSPILE_BABEL_RUNTIME = true
2 changes: 0 additions & 2 deletions packages/@vue/babel-preset-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.6.3",
"@babel/runtime": "^7.6.3",
"@babel/runtime-corejs3": "^7.6.3",
"@vue/babel-preset-jsx": "^1.1.1",
"babel-plugin-dynamic-import-node": "^2.2.0",
"babel-plugin-module-resolver": "^3.2.0",
"core-js": "^3.3.2",
"core-js-compat": "^3.3.2"
}
Expand Down
17 changes: 16 additions & 1 deletion packages/@vue/cli-plugin-babel/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path')
const babel = require('@babel/core')
const { isWindows } = require('@vue/cli-shared-utils')

function genTranspileDepRegex (transpileDependencies) {
Expand All @@ -17,9 +18,14 @@ function genTranspileDepRegex (transpileDependencies) {

module.exports = (api, options) => {
const useThreads = process.env.NODE_ENV === 'production' && !!options.parallel
const cliServicePath = require('path').dirname(require.resolve('@vue/cli-service'))
const cliServicePath = path.dirname(require.resolve('@vue/cli-service'))
const transpileDepRegex = genTranspileDepRegex(options.transpileDependencies)

// try to load the project babel config;
// if the default preset is used,
// there will be a VUE_CLI_TRANSPILE_BABEL_RUNTIME env var set.
babel.loadPartialConfig()

api.chainWebpack(webpackConfig => {
webpackConfig.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))

Expand All @@ -36,6 +42,15 @@ module.exports = (api, options) => {
if (filepath.startsWith(cliServicePath)) {
return true
}

// only include @babel/runtime when the @vue/babel-preset-app preset is used
if (
process.env.VUE_CLI_TRANSPILE_BABEL_RUNTIME &&
filepath.includes(path.join('@babel', 'runtime'))
) {
return false
}

// check if this is something the user explicitly wants to transpile
if (transpileDepRegex && transpileDepRegex.test(filepath)) {
return false
Expand Down