Skip to content

Commit 93f57ac

Browse files
authoredMar 5, 2019
fix: use an environment variable to determine the entry files to inject default polyfills (#3565)
The old logic is not reliable due to the presence of thread-loader closes #2983
1 parent 6f93bfe commit 93f57ac

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed
 

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1+
const path = require('path')
12
const babel = require('@babel/core')
23
const preset = require('../index')
34
const defaultOptions = {
45
babelrc: false,
5-
presets: [preset]
6+
presets: [preset],
7+
filename: 'test-entry-file.js'
68
}
79

10+
beforeEach(() => {
11+
process.env.VUE_CLI_ENTRY_FILES = JSON.stringify([path.join(process.cwd(), 'test-entry-file.js')])
12+
})
13+
814
test('polyfill detection', () => {
915
let { code } = babel.transformSync(`
1016
const a = new Map()
1117
`.trim(), {
1218
babelrc: false,
1319
presets: [[preset, {
1420
targets: { node: 'current' }
15-
}]]
21+
}]],
22+
filename: 'test-entry-file.js'
1623
})
17-
// default i ncludes
24+
// default includes
1825
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
1926
// usage-based detection
2027
expect(code).not.toMatch(`import "core-js/modules/es6.map"`)
@@ -25,7 +32,8 @@ test('polyfill detection', () => {
2532
babelrc: false,
2633
presets: [[preset, {
2734
targets: { ie: 9 }
28-
}]]
35+
}]],
36+
filename: 'test-entry-file.js'
2937
}))
3038
// default includes
3139
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
@@ -44,7 +52,8 @@ test('modern mode always skips polyfills', () => {
4452
presets: [[preset, {
4553
targets: { ie: 9 },
4654
useBuiltIns: 'usage'
47-
}]]
55+
}]],
56+
filename: 'test-entry-file.js'
4857
})
4958
// default includes
5059
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
@@ -58,7 +67,8 @@ test('modern mode always skips polyfills', () => {
5867
presets: [[preset, {
5968
targets: { ie: 9 },
6069
useBuiltIns: 'entry'
61-
}]]
70+
}]],
71+
filename: 'test-entry-file.js'
6272
}))
6373
// default includes
6474
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
@@ -133,7 +143,8 @@ test('disable absoluteRuntime', () => {
133143
babelrc: false,
134144
presets: [[preset, {
135145
absoluteRuntime: false
136-
}]]
146+
}]],
147+
filename: 'test-entry-file.js'
137148
})
138149

139150
expect(code).toMatch('import _toConsumableArray from "@babel/runtime-corejs2/helpers/esm/toConsumableArray"')

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function getPolyfills (targets, includes, { ignoreBrowserslistConfig, configPath
3030
module.exports = (context, options = {}) => {
3131
const presets = []
3232
const plugins = []
33+
const defaultEntryFiles = JSON.parse(process.env.VUE_CLI_ENTRY_FILES || '[]')
3334

3435
// JSX
3536
if (options.jsx !== false) {
@@ -53,7 +54,7 @@ module.exports = (context, options = {}) => {
5354
decoratorsBeforeExport,
5455
decoratorsLegacy,
5556
// entry file list
56-
entryFiles,
57+
entryFiles = defaultEntryFiles,
5758

5859
// Undocumented option of @babel/plugin-transform-runtime.
5960
// When enabled, an absolute path is used when importing a runtime helper atfer tranforming.

‎packages/@vue/babel-preset-app/polyfillsPlugin.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
// add polyfill imports to the first file encountered.
22
module.exports = ({ types }, { entryFiles = [] }) => {
3-
let entryFile
43
return {
54
name: 'vue-cli-inject-polyfills',
65
visitor: {
76
Program (path, state) {
8-
if (entryFiles.length === 0) {
9-
if (!entryFile) {
10-
entryFile = state.filename
11-
} else if (state.filename !== entryFile) {
12-
return
13-
}
14-
} else if (!entryFiles.includes(state.filename)) {
7+
if (!entryFiles.includes(state.filename)) {
158
return
169
}
1710

‎packages/@vue/cli-service/lib/Service.js

+5
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ module.exports = class Service {
270270
)
271271
}
272272

273+
const entryFiles = Object.values(config.entry || []).reduce((allEntries, curr) => {
274+
return allEntries.concat(curr)
275+
}, [])
276+
process.env.VUE_CLI_ENTRY_FILES = JSON.stringify(entryFiles)
277+
273278
return config
274279
}
275280

0 commit comments

Comments
 (0)
Please sign in to comment.