Skip to content

Commit

Permalink
fix: use an environment variable to determine the entry files to inje…
Browse files Browse the repository at this point in the history
…ct default polyfills (#3565)

The old logic is not reliable due to the presence of thread-loader
closes #2983
  • Loading branch information
haoqunjiang authored Mar 5, 2019
1 parent 6f93bfe commit 93f57ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
25 changes: 18 additions & 7 deletions packages/@vue/babel-preset-app/__tests__/babel-preset.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
const path = require('path')
const babel = require('@babel/core')
const preset = require('../index')
const defaultOptions = {
babelrc: false,
presets: [preset]
presets: [preset],
filename: 'test-entry-file.js'
}

beforeEach(() => {
process.env.VUE_CLI_ENTRY_FILES = JSON.stringify([path.join(process.cwd(), 'test-entry-file.js')])
})

test('polyfill detection', () => {
let { code } = babel.transformSync(`
const a = new Map()
`.trim(), {
babelrc: false,
presets: [[preset, {
targets: { node: 'current' }
}]]
}]],
filename: 'test-entry-file.js'
})
// default i ncludes
// default includes
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
// usage-based detection
expect(code).not.toMatch(`import "core-js/modules/es6.map"`)
Expand All @@ -25,7 +32,8 @@ test('polyfill detection', () => {
babelrc: false,
presets: [[preset, {
targets: { ie: 9 }
}]]
}]],
filename: 'test-entry-file.js'
}))
// default includes
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
Expand All @@ -44,7 +52,8 @@ test('modern mode always skips polyfills', () => {
presets: [[preset, {
targets: { ie: 9 },
useBuiltIns: 'usage'
}]]
}]],
filename: 'test-entry-file.js'
})
// default includes
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
Expand All @@ -58,7 +67,8 @@ test('modern mode always skips polyfills', () => {
presets: [[preset, {
targets: { ie: 9 },
useBuiltIns: 'entry'
}]]
}]],
filename: 'test-entry-file.js'
}))
// default includes
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
Expand Down Expand Up @@ -133,7 +143,8 @@ test('disable absoluteRuntime', () => {
babelrc: false,
presets: [[preset, {
absoluteRuntime: false
}]]
}]],
filename: 'test-entry-file.js'
})

expect(code).toMatch('import _toConsumableArray from "@babel/runtime-corejs2/helpers/esm/toConsumableArray"')
Expand Down
3 changes: 2 additions & 1 deletion packages/@vue/babel-preset-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function getPolyfills (targets, includes, { ignoreBrowserslistConfig, configPath
module.exports = (context, options = {}) => {
const presets = []
const plugins = []
const defaultEntryFiles = JSON.parse(process.env.VUE_CLI_ENTRY_FILES || '[]')

// JSX
if (options.jsx !== false) {
Expand All @@ -53,7 +54,7 @@ module.exports = (context, options = {}) => {
decoratorsBeforeExport,
decoratorsLegacy,
// entry file list
entryFiles,
entryFiles = defaultEntryFiles,

// Undocumented option of @babel/plugin-transform-runtime.
// When enabled, an absolute path is used when importing a runtime helper atfer tranforming.
Expand Down
9 changes: 1 addition & 8 deletions packages/@vue/babel-preset-app/polyfillsPlugin.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
// add polyfill imports to the first file encountered.
module.exports = ({ types }, { entryFiles = [] }) => {
let entryFile
return {
name: 'vue-cli-inject-polyfills',
visitor: {
Program (path, state) {
if (entryFiles.length === 0) {
if (!entryFile) {
entryFile = state.filename
} else if (state.filename !== entryFile) {
return
}
} else if (!entryFiles.includes(state.filename)) {
if (!entryFiles.includes(state.filename)) {
return
}

Expand Down
5 changes: 5 additions & 0 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ module.exports = class Service {
)
}

const entryFiles = Object.values(config.entry || []).reduce((allEntries, curr) => {
return allEntries.concat(curr)
}, [])
process.env.VUE_CLI_ENTRY_FILES = JSON.stringify(entryFiles)

return config
}

Expand Down

0 comments on commit 93f57ac

Please sign in to comment.