Skip to content

Commit b987969

Browse files
authoredMar 31, 2019
fix: should not add polyfills from transform-runtime plugin (#3730)
It is due to a misunderstanding when upgrading babel from 7.0.0-beta.47 to 7.x. In Vue CLI, usage-based polyfills are **only** added by @babel/preset-env so that it avoids overheads. `transform-runtime` plugin adds polyfills regardless of env targets so we must disable its polyfill feature. Though, there're edge cases where runtime-helpers' polyfill dependency are not correctly added (babel/babel#7597), so we also need to whitelist `@babel/runtime` for webpack to transpile.
1 parent eeb350e commit b987969

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed
 

‎packages/@vue/babel-preset-app/__tests__/babel-preset.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test('polyfill detection', () => {
4545
// promise polyfill alone doesn't work in IE, needs this as well. fix: #1642
4646
expect(code).toMatch(genCoreJSImportRegExp('es6.array.iterator'))
4747
// usage-based detection
48-
expect(code).toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
48+
expect(code).toMatch(/import "core-js\/modules\/es6.map"/)
4949
})
5050

5151
test('modern mode always skips polyfills', () => {
@@ -63,7 +63,7 @@ test('modern mode always skips polyfills', () => {
6363
// default includes
6464
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
6565
// usage-based detection
66-
expect(code).not.toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
66+
expect(code).not.toMatch(/import "core-js\/modules\/es6.map"/)
6767

6868
;({ code } = babel.transformSync(`
6969
const a = new Map()
@@ -78,7 +78,7 @@ test('modern mode always skips polyfills', () => {
7878
// default includes
7979
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
8080
// usage-based detection
81-
expect(code).not.toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
81+
expect(code).not.toMatch(/import "core-js\/modules\/es6.map"/)
8282
delete process.env.VUE_CLI_MODERN_BUILD
8383
})
8484

@@ -107,7 +107,7 @@ test('async/await', () => {
107107
// should use regenerator runtime
108108
expect(code).toMatch(`import "regenerator-runtime/runtime"`)
109109
// should use required helper instead of inline
110-
expect(code).toMatch(/import _asyncToGenerator from ".*runtime-corejs2\/helpers\/esm\/asyncToGenerator\"/)
110+
expect(code).toMatch(/import _asyncToGenerator from ".*runtime\/helpers\/esm\/asyncToGenerator\"/)
111111
})
112112

113113
test('jsx', () => {
@@ -152,5 +152,5 @@ test('disable absoluteRuntime', () => {
152152
filename: 'test-entry-file.js'
153153
})
154154

155-
expect(code).toMatch('import _toConsumableArray from "@babel/runtime-corejs2/helpers/esm/toConsumableArray"')
155+
expect(code).toMatch('import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"')
156156
})

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ module.exports = (context, options = {}) => {
151151
// transform runtime, but only for helpers
152152
plugins.push([require('@babel/plugin-transform-runtime'), {
153153
regenerator: useBuiltIns !== 'usage',
154-
// use @babel/runtime-corejs2 so that helpers that need polyfillable APIs will reference core-js instead.
155-
// if useBuiltIns is not set to 'usage', then it means users would take care of the polyfills on their own,
156-
// i.e., core-js 2 is no longer needed.
157-
corejs: (useBuiltIns === 'usage' && !process.env.VUE_CLI_MODERN_BUILD) ? 2 : false,
154+
155+
// polyfills are injected by preset-env & polyfillsPlugin, so no need to add them again
156+
corejs: false,
157+
158158
helpers: useBuiltIns === 'usage',
159159
useESModules: !process.env.VUE_CLI_BABEL_TRANSPILE_MODULES,
160160

‎packages/@vue/babel-preset-app/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"@babel/plugin-transform-runtime": "^7.4.0",
3131
"@babel/preset-env": "^7.0.0 < 7.4.0",
3232
"@babel/runtime": "^7.0.0",
33-
"@babel/runtime-corejs2": "^7.2.0",
3433
"@vue/babel-preset-jsx": "^1.0.0-beta.2",
3534
"babel-plugin-dynamic-import-node": "^2.2.0",
3635
"core-js": "^2.6.5"

‎packages/@vue/cli-plugin-babel/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ module.exports = (api, options) => {
4040
if (transpileDepRegex && transpileDepRegex.test(filepath)) {
4141
return false
4242
}
43+
// may need to add polyfills (by preset-env) for babel helpers
44+
// https://github.com/babel/babel/issues/7597
45+
if (/node_modules\/@babel\/runtime/.test(filepath)) {
46+
return false
47+
}
4348
// Don't transpile node_modules
4449
return /node_modules/.test(filepath)
4550
})

0 commit comments

Comments
 (0)
Please sign in to comment.