Skip to content

fix: use an environment variable to determine the entry files to inject default polyfills #3565

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 3 commits into from
Mar 5, 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
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