@@ -56,14 +56,14 @@ function viteLegacyPlugin(options = {}) {
56
56
57
57
async generateBundle ( opts , bundle ) {
58
58
if ( ! isLegacyOutput ( opts ) ) {
59
+ if ( ! modernPolyfills . size ) {
60
+ return
61
+ }
59
62
isDebug &&
60
63
console . log (
61
64
`[@vitejs/plugin-legacy] modern polyfills:` ,
62
65
modernPolyfills
63
66
)
64
- if ( ! modernPolyfills . size ) {
65
- return
66
- }
67
67
await buildPolyfillChunk (
68
68
'polyfills-modern' ,
69
69
modernPolyfills ,
@@ -79,12 +79,19 @@ function viteLegacyPlugin(options = {}) {
79
79
}
80
80
81
81
// legacy bundle
82
- isDebug &&
83
- console . log (
84
- `[@vitejs/plugin-legacy] legacy polyfills:` ,
85
- legacyPolyfills
86
- )
87
82
if ( legacyPolyfills . size ) {
83
+ if ( ! legacyPolyfills . has ( 'es.promise' ) ) {
84
+ // check if the target needs Promise polyfill because SystemJS relies
85
+ // on it
86
+ detectPolyfills ( `Promise.resolve()` , targets , legacyPolyfills )
87
+ }
88
+
89
+ isDebug &&
90
+ console . log (
91
+ `[@vitejs/plugin-legacy] legacy polyfills:` ,
92
+ legacyPolyfills
93
+ )
94
+
88
95
await buildPolyfillChunk (
89
96
'polyfills-legacy' ,
90
97
legacyPolyfills ,
@@ -150,46 +157,16 @@ function viteLegacyPlugin(options = {}) {
150
157
) {
151
158
return null
152
159
}
153
-
154
160
// analyze and record modern polyfills
155
- const { ast } = babel . transformSync ( raw , {
156
- ast : true ,
157
- code : false ,
158
- configFile : false ,
159
- sourceMaps : false ,
160
- presets : [
161
- [
162
- presetEnv ,
163
- {
164
- targets : { esmodules : true } ,
165
- modules : false ,
166
- useBuiltIns : 'usage' ,
167
- corejs : { version : 3 , proposals : false } ,
168
- shippedProposals : true ,
169
- ignoreBrowserslistConfig : true
170
- }
171
- ]
172
- ]
173
- } )
174
- for ( const node of ast . program . body ) {
175
- if ( node . type === 'ImportDeclaration' ) {
176
- const source = node . source . value
177
- if (
178
- source . startsWith ( 'core-js/' ) ||
179
- source . startsWith ( 'regenerator-runtime/' )
180
- ) {
181
- modernPolyfills . add ( node . source . value )
182
- }
183
- }
184
- }
161
+ detectPolyfills ( raw , { esmodules : true } , modernPolyfills )
185
162
return null
186
163
}
187
164
188
165
if ( ! genLegacy ) {
189
166
return
190
167
}
191
168
192
- const detectPolyfills =
169
+ const needPolyfills =
193
170
options . polyfills !== false && ! Array . isArray ( options . polyfills )
194
171
195
172
// transform the legacy chunk with @babel /preset-env
@@ -204,21 +181,20 @@ function viteLegacyPlugin(options = {}) {
204
181
presetEnv ,
205
182
{
206
183
targets,
207
- // modules are already converted to systemjs by rollup
208
184
modules : false ,
209
185
bugfixes : true ,
210
- useBuiltIns : detectPolyfills ? 'usage' : false ,
211
- shippedProposals : true ,
212
- corejs : detectPolyfills
186
+ useBuiltIns : needPolyfills ? 'usage' : false ,
187
+ corejs : needPolyfills
213
188
? { version : 3 , proposals : false }
214
189
: undefined ,
190
+ shippedProposals : true ,
215
191
ignoreBrowserslistConfig : options . ignoreBrowserslistConfig
216
192
}
217
193
]
218
194
]
219
195
} )
220
196
221
- if ( detectPolyfills ) {
197
+ if ( needPolyfills ) {
222
198
// detect and remove polyfill imports. Since the legacy bundle uses
223
199
// format: 'system', any import declarations are polyfill imports injected
224
200
// by @babel /preset-env.
@@ -336,6 +312,42 @@ function viteLegacyPlugin(options = {}) {
336
312
return [ legacyGenerateBundlePlugin , legacyPostPlugin ]
337
313
}
338
314
315
+ /**
316
+ * @param {string } code
317
+ * @param {any } targets
318
+ * @param {Set<string> } list
319
+ */
320
+ function detectPolyfills ( code , targets , list ) {
321
+ const { ast } = babel . transformSync ( code , {
322
+ ast : true ,
323
+ configFile : false ,
324
+ presets : [
325
+ [
326
+ presetEnv ,
327
+ {
328
+ targets,
329
+ modules : false ,
330
+ useBuiltIns : 'usage' ,
331
+ corejs : { version : 3 , proposals : false } ,
332
+ shippedProposals : true ,
333
+ ignoreBrowserslistConfig : true
334
+ }
335
+ ]
336
+ ]
337
+ } )
338
+ for ( const node of ast . program . body ) {
339
+ if ( node . type === 'ImportDeclaration' ) {
340
+ const source = node . source . value
341
+ if (
342
+ source . startsWith ( 'core-js/' ) ||
343
+ source . startsWith ( 'regenerator-runtime/' )
344
+ ) {
345
+ list . add ( source )
346
+ }
347
+ }
348
+ }
349
+ }
350
+
339
351
/**
340
352
* @param {string } name
341
353
* @param {Set<string> } imports
0 commit comments