Skip to content

fix: use absolute import path for injected core-js polyfills #3710

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 5 commits into from
Mar 27, 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
19 changes: 12 additions & 7 deletions packages/@vue/babel-preset-app/__tests__/babel-preset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const defaultOptions = {
filename: 'test-entry-file.js'
}

const genCoreJSImportRegExp = mod => {
// expected to include a `node_modules` in the import path because we use absolute path for core-js
return new RegExp(`import "${['.*node_modules', 'core-js', 'modules', mod].join(`[\\${path.sep}]+`)}`)
}

beforeEach(() => {
process.env.VUE_CLI_ENTRY_FILES = JSON.stringify([path.join(process.cwd(), 'test-entry-file.js')])
})
Expand All @@ -22,9 +27,9 @@ test('polyfill detection', () => {
filename: 'test-entry-file.js'
})
// default includes
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
// usage-based detection
expect(code).not.toMatch(`import "core-js/modules/es6.map"`)
expect(code).not.toMatch(genCoreJSImportRegExp('es6.map'))

;({ code } = babel.transformSync(`
const a = new Map()
Expand All @@ -36,9 +41,9 @@ test('polyfill detection', () => {
filename: 'test-entry-file.js'
}))
// default includes
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
expect(code).toMatch(genCoreJSImportRegExp('es6.promise'))
// promise polyfill alone doesn't work in IE, needs this as well. fix: #1642
expect(code).toMatch(`import "core-js/modules/es6.array.iterator"`)
expect(code).toMatch(genCoreJSImportRegExp('es6.array.iterator'))
// usage-based detection
expect(code).toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
})
Expand All @@ -56,7 +61,7 @@ test('modern mode always skips polyfills', () => {
filename: 'test-entry-file.js'
})
// default includes
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
// usage-based detection
expect(code).not.toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)

Expand All @@ -71,7 +76,7 @@ test('modern mode always skips polyfills', () => {
filename: 'test-entry-file.js'
}))
// default includes
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
expect(code).not.toMatch(genCoreJSImportRegExp('es6.promise'))
// usage-based detection
expect(code).not.toMatch(/import _Map from ".*runtime-corejs2\/core-js\/map"/)
delete process.env.VUE_CLI_MODERN_BUILD
Expand All @@ -98,7 +103,7 @@ test('async/await', () => {
}
hello()
`.trim(), defaultOptions)
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
expect(code).toMatch(genCoreJSImportRegExp('es6.promise'))
// should use regenerator runtime
expect(code).toMatch(`import "regenerator-runtime/runtime"`)
// should use required helper instead of inline
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/babel-preset-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"homepage": "https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/babel-preset-app#readme",
"dependencies": {
"@babel/helper-module-imports": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.1.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
Expand Down
26 changes: 22 additions & 4 deletions packages/@vue/babel-preset-app/polyfillsPlugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
const { addSideEffect } = require('@babel/helper-module-imports')

// slightly modifiled from @babel/preset-env/src/utils
// use an absolute path for core-js modules, to fix conflicts of different core-js versions
function getModulePath (mod) {
if (mod === 'regenerator-runtime') {
return require.resolve('regenerator-runtime/runtime')
}

return require.resolve(`core-js/modules/${mod}`)
}

function createImport (path, mod) {
return addSideEffect(path, getModulePath(mod))
}

// add polyfill imports to the first file encountered.
module.exports = ({ types }, { entryFiles = [] }) => {
return {
Expand All @@ -9,11 +25,13 @@ module.exports = ({ types }, { entryFiles = [] }) => {
}

const { polyfills } = state.opts
const { createImport } = require('@babel/preset-env/lib/utils')
// imports are injected in reverse order
polyfills.slice().reverse().forEach(p => {
createImport(path, p)
})
polyfills
.slice()
.reverse()
.forEach(p => {
createImport(path, p)
})
}
}
}
Expand Down