Skip to content

Commit bc1a345

Browse files
authored
fix: should not add polyfills from transform-runtime plugin (#3899)
The basic idea is the same as #3730, except that this time we use babel-plugin-module-resolve for aliasing instead of relying on webpack.
1 parent a3e0858 commit bc1a345

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

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

Lines changed: 4 additions & 4 deletions
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

@@ -153,5 +153,5 @@ test('disable absoluteRuntime', () => {
153153
})
154154

155155
expect(code).toMatch('import _toConsumableArray from "@babel/runtime-corejs2/helpers/esm/toConsumableArray"')
156-
// expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
156+
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
157157
})

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = (context, options = {}) => {
3737
presets.push([require('@vue/babel-preset-jsx'), typeof options.jsx === 'object' ? options.jsx : {}])
3838
}
3939

40+
const runtimePath = path.dirname(require.resolve('@babel/runtime/package.json'))
4041
const {
4142
polyfills: userPolyfills,
4243
loose = false,
@@ -62,7 +63,7 @@ module.exports = (context, options = {}) => {
6263
// However, this may cause hash inconsitency if the project is moved to another directory.
6364
// So here we allow user to explicit disable this option if hash consistency is a requirement
6465
// and the runtime version is sure to be correct.
65-
absoluteRuntime = path.dirname(require.resolve('@babel/runtime/package.json'))
66+
absoluteRuntime = runtimePath
6667
} = options
6768

6869
// resolve targets
@@ -154,16 +155,32 @@ module.exports = (context, options = {}) => {
154155
// transform runtime, but only for helpers
155156
plugins.push([require('@babel/plugin-transform-runtime'), {
156157
regenerator: useBuiltIns !== 'usage',
157-
// use @babel/runtime-corejs2 so that helpers that need polyfillable APIs will reference core-js instead.
158-
// if useBuiltIns is not set to 'usage', then it means users would take care of the polyfills on their own,
159-
// i.e., core-js 2 is no longer needed.
160-
corejs: (useBuiltIns === 'usage' && !process.env.VUE_CLI_MODERN_BUILD) ? 2 : false,
158+
159+
// polyfills are injected by preset-env & polyfillsPlugin, so no need to add them again
160+
corejs: false,
161+
161162
helpers: useBuiltIns === 'usage',
162163
useESModules: !process.env.VUE_CLI_BABEL_TRANSPILE_MODULES,
163164

164165
absoluteRuntime
165166
}])
166167

168+
// use @babel/runtime-corejs2 so that helpers that need polyfillable APIs will reference core-js instead.
169+
// if useBuiltIns is not set to 'usage', then it means users would take care of the polyfills on their own,
170+
// i.e., core-js is no longer needed.
171+
// this extra plugin can be removed once one of the two issues resolves:
172+
// https://github.com/babel/babel/issues/7597
173+
// https://github.com/babel/babel/issues/9903
174+
if (useBuiltIns === 'usage' && !process.env.VUE_CLI_MODERN_BUILD) {
175+
const runtimeCoreJs2Path = path.dirname(require.resolve('@babel/runtime-corejs2/package.json'))
176+
plugins.push([require('babel-plugin-module-resolver'), {
177+
alias: {
178+
'@babel/runtime': '@babel/runtime-corejs2',
179+
[runtimePath]: runtimeCoreJs2Path
180+
}
181+
}])
182+
}
183+
167184
return {
168185
presets,
169186
plugins

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@babel/runtime-corejs2": "^7.2.0",
3434
"@vue/babel-preset-jsx": "^1.0.0-beta.3",
3535
"babel-plugin-dynamic-import-node": "^2.2.0",
36+
"babel-plugin-module-resolver": "3.2.0",
3637
"core-js": "^2.6.5"
37-
},
38-
"gitHead": "0dc793497281718762a5477a3de4a7ee439cdda6"
38+
}
3939
}

yarn.lock

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3240,6 +3240,17 @@ babel-plugin-jest-hoist@^23.2.0:
32403240
resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-23.2.0.tgz#e61fae05a1ca8801aadee57a6d66b8cefaf44167"
32413241
integrity sha1-5h+uBaHKiAGq3uV6bWa4zvr0QWc=
32423242

3243+
babel-plugin-module-resolver@3.2.0:
3244+
version "3.2.0"
3245+
resolved "https://registry.yarnpkg.com/babel-plugin-module-resolver/-/babel-plugin-module-resolver-3.2.0.tgz#ddfa5e301e3b9aa12d852a9979f18b37881ff5a7"
3246+
integrity sha512-tjR0GvSndzPew/Iayf4uICWZqjBwnlMWjSx6brryfQ81F9rxBVqwDJtFCV8oOs0+vJeefK9TmdZtkIFdFe1UnA==
3247+
dependencies:
3248+
find-babel-config "^1.1.0"
3249+
glob "^7.1.2"
3250+
pkg-up "^2.0.0"
3251+
reselect "^3.0.1"
3252+
resolve "^1.4.0"
3253+
32433254
babel-plugin-syntax-object-rest-spread@^6.13.0, babel-plugin-syntax-object-rest-spread@^6.8.0:
32443255
version "6.13.0"
32453256
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"
@@ -6431,7 +6442,7 @@ eslint-plugin-vue@^4.5.0, eslint-plugin-vue@^4.7.1:
64316442
dependencies:
64326443
vue-eslint-parser "^2.0.3"
64336444

6434-
eslint-plugin-vue@^5.2.2:
6445+
eslint-plugin-vue@^5.0.0, eslint-plugin-vue@^5.2.2:
64356446
version "5.2.2"
64366447
resolved "https://registry.yarnpkg.com/eslint-plugin-vue/-/eslint-plugin-vue-5.2.2.tgz#86601823b7721b70bc92d54f1728cfc03b36283c"
64376448
integrity sha512-CtGWH7IB0DA6BZOwcV9w9q3Ri6Yuo8qMjx05SmOGJ6X6E0Yo3y9E/gQ5tuNxg2dEt30tRnBoFTbvtmW9iEoyHA==
@@ -12758,6 +12769,13 @@ pkg-dir@^3.0.0:
1275812769
dependencies:
1275912770
find-up "^3.0.0"
1276012771

12772+
pkg-up@^2.0.0:
12773+
version "2.0.0"
12774+
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-2.0.0.tgz#c819ac728059a461cab1c3889a2be3c49a004d7f"
12775+
integrity sha1-yBmscoBZpGHKscOImivjxJoATX8=
12776+
dependencies:
12777+
find-up "^2.1.0"
12778+
1276112779
please-upgrade-node@^3.0.2, please-upgrade-node@^3.1.1:
1276212780
version "3.1.1"
1276312781
resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.1.1.tgz#ed320051dfcc5024fae696712c8288993595e8ac"
@@ -14494,6 +14512,11 @@ requires-port@^1.0.0:
1449414512
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
1449514513
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=
1449614514

14515+
reselect@^3.0.1:
14516+
version "3.0.1"
14517+
resolved "https://registry.yarnpkg.com/reselect/-/reselect-3.0.1.tgz#efdaa98ea7451324d092b2b2163a6a1d7a9a2147"
14518+
integrity sha1-79qpjqdFEyTQkrKyFjpqHXqaIUc=
14519+
1449714520
resolve-cwd@^2.0.0:
1449814521
version "2.0.0"
1449914522
resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-2.0.0.tgz#00a9f7387556e27038eae232caa372a6a59b665a"

0 commit comments

Comments
 (0)