Skip to content

Commit 4d6fcf5

Browse files
authored
fix: use absolute import path for injected core-js polyfills (#3710)
fixes #3678
1 parent 156ef21 commit 4d6fcf5

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const defaultOptions = {
77
filename: 'test-entry-file.js'
88
}
99

10+
const genCoreJSImportRegExp = mod => {
11+
// expected to include a `node_modules` in the import path because we use absolute path for core-js
12+
return new RegExp(`import "${['.*node_modules', 'core-js', 'modules', mod].join(`[\\${path.sep}]+`)}`)
13+
}
14+
1015
beforeEach(() => {
1116
process.env.VUE_CLI_ENTRY_FILES = JSON.stringify([path.join(process.cwd(), 'test-entry-file.js')])
1217
})
@@ -22,9 +27,9 @@ test('polyfill detection', () => {
2227
filename: 'test-entry-file.js'
2328
})
2429
// default includes
25-
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
30+
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
2631
// usage-based detection
27-
expect(code).not.toMatch(`import "core-js/modules/es6.map"`)
32+
expect(code).not.toMatch(genCoreJSImportRegExp('es6.map'))
2833

2934
;({ code } = babel.transformSync(`
3035
const a = new Map()
@@ -36,9 +41,9 @@ test('polyfill detection', () => {
3641
filename: 'test-entry-file.js'
3742
}))
3843
// default includes
39-
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
44+
expect(code).toMatch(genCoreJSImportRegExp('es6.promise'))
4045
// promise polyfill alone doesn't work in IE, needs this as well. fix: #1642
41-
expect(code).toMatch(`import "core-js/modules/es6.array.iterator"`)
46+
expect(code).toMatch(genCoreJSImportRegExp('es6.array.iterator'))
4247
// usage-based detection
4348
expect(code).toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
4449
})
@@ -56,7 +61,7 @@ test('modern mode always skips polyfills', () => {
5661
filename: 'test-entry-file.js'
5762
})
5863
// default includes
59-
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
64+
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
6065
// usage-based detection
6166
expect(code).not.toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
6267

@@ -71,7 +76,7 @@ test('modern mode always skips polyfills', () => {
7176
filename: 'test-entry-file.js'
7277
}))
7378
// default includes
74-
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
79+
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
7580
// usage-based detection
7681
expect(code).not.toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
7782
delete process.env.VUE_CLI_MODERN_BUILD
@@ -98,7 +103,7 @@ test('async/await', () => {
98103
}
99104
hello()
100105
`.trim(), defaultOptions)
101-
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
106+
expect(code).toMatch(genCoreJSImportRegExp('es6.promise'))
102107
// should use regenerator runtime
103108
expect(code).toMatch(`import "regenerator-runtime/runtime"`)
104109
// should use required helper instead of inline

Diff for: packages/@vue/babel-preset-app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"homepage": "https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/babel-preset-app#readme",
2424
"dependencies": {
25+
"@babel/helper-module-imports": "^7.0.0",
2526
"@babel/plugin-proposal-class-properties": "^7.0.0",
2627
"@babel/plugin-proposal-decorators": "^7.1.0",
2728
"@babel/plugin-syntax-dynamic-import": "^7.0.0",

Diff for: packages/@vue/babel-preset-app/polyfillsPlugin.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
const { addSideEffect } = require('@babel/helper-module-imports')
2+
3+
// slightly modifiled from @babel/preset-env/src/utils
4+
// use an absolute path for core-js modules, to fix conflicts of different core-js versions
5+
function getModulePath (mod) {
6+
if (mod === 'regenerator-runtime') {
7+
return require.resolve('regenerator-runtime/runtime')
8+
}
9+
10+
return require.resolve(`core-js/modules/${mod}`)
11+
}
12+
13+
function createImport (path, mod) {
14+
return addSideEffect(path, getModulePath(mod))
15+
}
16+
117
// add polyfill imports to the first file encountered.
218
module.exports = ({ types }, { entryFiles = [] }) => {
319
return {
@@ -9,11 +25,13 @@ module.exports = ({ types }, { entryFiles = [] }) => {
925
}
1026

1127
const { polyfills } = state.opts
12-
const { createImport } = require('@babel/preset-env/lib/utils')
1328
// imports are injected in reverse order
14-
polyfills.slice().reverse().forEach(p => {
15-
createImport(path, p)
16-
})
29+
polyfills
30+
.slice()
31+
.reverse()
32+
.forEach(p => {
33+
createImport(path, p)
34+
})
1735
}
1836
}
1937
}

0 commit comments

Comments
 (0)