1
1
const path = require ( 'path' )
2
+ const semver = require ( 'semver' )
2
3
3
4
const defaultPolyfills = [
4
5
// promise polyfill alone doesn't work in IE,
@@ -9,21 +10,78 @@ const defaultPolyfills = [
9
10
// this is needed for object rest spread support in templates
10
11
// as vue-template-es2015-compiler 1.8+ compiles it to Object.assign() calls.
11
12
'es.object.assign' ,
12
- // #2012 es6 .promise replaces native Promise in FF and causes missing finally
13
+ // #2012 es .promise replaces native Promise in FF and causes missing finally
13
14
'es.promise.finally'
14
15
]
15
16
16
- function getPolyfills ( targets , includes , { ignoreBrowserslistConfig, configPath } ) {
17
- const getTargets = require ( '@babel/helper-compilation-targets' ) . default
18
- const builtInTargets = getTargets ( targets , { ignoreBrowserslistConfig, configPath } )
17
+ const {
18
+ default : getTargets ,
19
+ isRequired
20
+ } = require ( '@babel/helper-compilation-targets' )
19
21
22
+ function getIntersectionTargets ( targets , constraintTargets ) {
23
+ const intersection = Object . keys ( constraintTargets ) . reduce (
24
+ ( results , browser ) => {
25
+ // exclude the browsers that the user does not need
26
+ if ( ! targets [ browser ] ) {
27
+ return results
28
+ }
29
+
30
+ // if the user-specified version is higher the minimum version that supports esmodule, than use it
31
+ results [ browser ] = semver . gt (
32
+ semver . coerce ( constraintTargets [ browser ] ) ,
33
+ semver . coerce ( targets [ browser ] )
34
+ )
35
+ ? constraintTargets [ browser ]
36
+ : targets [ browser ]
37
+
38
+ return results
39
+ } ,
40
+ { }
41
+ )
42
+
43
+ return intersection
44
+ }
45
+
46
+ function getModernTargets ( targets ) {
47
+ const allModernTargets = getTargets (
48
+ { esmodules : true } ,
49
+ { ignoreBrowserslistConfig : true }
50
+ )
51
+
52
+ // use the intersection of modern mode browsers and user defined targets config
53
+ return getIntersectionTargets ( targets , allModernTargets )
54
+ }
55
+
56
+ function getWCTargets ( targets ) {
57
+ // targeting browsers that at least support ES2015 classes
58
+ // https://github.com/babel/babel/blob/v7.9.6/packages/babel-compat-data/data/plugins.json#L194-L204
59
+ const allWCTargets = getTargets (
60
+ {
61
+ browsers : [
62
+ 'Chrome >= 46' ,
63
+ 'Firefox >= 45' ,
64
+ 'Safari >= 10' ,
65
+ 'Edge >= 13' ,
66
+ 'iOS >= 10' ,
67
+ 'Electron >= 0.36'
68
+ ]
69
+ } ,
70
+ { ignoreBrowserslistConfig : true }
71
+ )
72
+
73
+ // use the intersection of browsers supporting Web Components and user defined targets config
74
+ return getIntersectionTargets ( targets , allWCTargets )
75
+ }
76
+
77
+ function getPolyfills ( targets , includes ) {
20
78
// if no targets specified, include all default polyfills
21
- if ( ! targets && ! Object . keys ( builtInTargets ) . length ) {
79
+ if ( ! targets || ! Object . keys ( targets ) . length ) {
22
80
return includes
23
81
}
24
82
25
- const { list } = require ( 'core-js-compat' ) ( { targets : builtInTargets } )
26
- return includes . filter ( item => list . includes ( item ) )
83
+ const compatData = require ( 'core-js-compat' ) . data
84
+ return includes . filter ( item => isRequired ( item , targets , { compatData } ) )
27
85
}
28
86
29
87
module . exports = ( context , options = { } ) => {
@@ -36,7 +94,7 @@ module.exports = (context, options = {}) => {
36
94
// dropping them may break some projects.
37
95
// So in the following blocks we don't directly test the `NODE_ENV`.
38
96
// Rather, we turn it into the two commonly used feature flags.
39
- if ( process . env . NODE_ENV === 'test' ) {
97
+ if ( ! process . env . VUE_CLI_TEST && process . env . NODE_ENV === 'test' ) {
40
98
// Both Jest & Mocha set NODE_ENV to 'test'.
41
99
// And both requires the `node` target.
42
100
process . env . VUE_CLI_BABEL_TARGET_NODE = 'true'
@@ -62,7 +120,7 @@ module.exports = (context, options = {}) => {
62
120
bugfixes = true ,
63
121
targets : rawTargets ,
64
122
spec,
65
- ignoreBrowserslistConfig = ! ! process . env . VUE_CLI_MODERN_BUILD ,
123
+ ignoreBrowserslistConfig,
66
124
configPath,
67
125
include,
68
126
exclude,
@@ -88,29 +146,17 @@ module.exports = (context, options = {}) => {
88
146
version = runtimeVersion
89
147
} = options
90
148
91
- // resolve targets
92
- let targets
149
+ // resolve targets for preset-env
150
+ let targets = getTargets ( rawTargets , { ignoreBrowserslistConfig , configPath } )
93
151
if ( process . env . VUE_CLI_BABEL_TARGET_NODE ) {
94
152
// running tests in Node.js
95
153
targets = { node : 'current' }
96
154
} else if ( process . env . VUE_CLI_BUILD_TARGET === 'wc' || process . env . VUE_CLI_BUILD_TARGET === 'wc-async' ) {
97
155
// targeting browsers that at least support ES2015 classes
98
- // https://github.com/babel/babel/blob/master/packages/babel-preset-env/data/plugins.json#L52-L61
99
- targets = {
100
- browsers : [
101
- 'Chrome >= 49' ,
102
- 'Firefox >= 45' ,
103
- 'Safari >= 10' ,
104
- 'Edge >= 13' ,
105
- 'iOS >= 10' ,
106
- 'Electron >= 0.36'
107
- ]
108
- }
156
+ targets = getWCTargets ( targets )
109
157
} else if ( process . env . VUE_CLI_MODERN_BUILD ) {
110
- // targeting browsers that support <script type="module">
111
- targets = { esmodules : true }
112
- } else {
113
- targets = rawTargets
158
+ // targeting browsers that at least support <script type="module">
159
+ targets = getModernTargets ( targets )
114
160
}
115
161
116
162
// included-by-default polyfills. These are common polyfills that 3rd party
@@ -122,13 +168,9 @@ module.exports = (context, options = {}) => {
122
168
if (
123
169
buildTarget === 'app' &&
124
170
useBuiltIns === 'usage' &&
125
- ! process . env . VUE_CLI_BABEL_TARGET_NODE &&
126
- ! process . env . VUE_CLI_MODERN_BUILD
171
+ ! process . env . VUE_CLI_BABEL_TARGET_NODE
127
172
) {
128
- polyfills = getPolyfills ( targets , userPolyfills || defaultPolyfills , {
129
- ignoreBrowserslistConfig,
130
- configPath
131
- } )
173
+ polyfills = getPolyfills ( targets , userPolyfills || defaultPolyfills )
132
174
plugins . push ( [
133
175
require ( './polyfillsPlugin' ) ,
134
176
{ polyfills, entryFiles, useAbsolutePath : ! ! absoluteRuntime }
@@ -139,7 +181,7 @@ module.exports = (context, options = {}) => {
139
181
140
182
const envOptions = {
141
183
bugfixes,
142
- corejs : useBuiltIns ? 3 : false ,
184
+ corejs : useBuiltIns ? require ( 'core-js/package.json' ) . version : false ,
143
185
spec,
144
186
loose,
145
187
debug,
@@ -206,7 +248,7 @@ module.exports = (context, options = {}) => {
206
248
presets : [
207
249
[ require ( '@babel/preset-env' ) , {
208
250
useBuiltIns,
209
- corejs : useBuiltIns ? 3 : false
251
+ corejs : useBuiltIns ? require ( 'core-js/package.json' ) . version : false
210
252
} ]
211
253
]
212
254
} ]
0 commit comments