Skip to content

Commit b72db4e

Browse files
committed
fix: add promise polyfill if not used in bundle
1 parent 52ac81a commit b72db4e

File tree

1 file changed

+57
-45
lines changed

1 file changed

+57
-45
lines changed

packages/plugin-legacy/index.js

+57-45
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ function viteLegacyPlugin(options = {}) {
5656

5757
async generateBundle(opts, bundle) {
5858
if (!isLegacyOutput(opts)) {
59+
if (!modernPolyfills.size) {
60+
return
61+
}
5962
isDebug &&
6063
console.log(
6164
`[@vitejs/plugin-legacy] modern polyfills:`,
6265
modernPolyfills
6366
)
64-
if (!modernPolyfills.size) {
65-
return
66-
}
6767
await buildPolyfillChunk(
6868
'polyfills-modern',
6969
modernPolyfills,
@@ -79,12 +79,19 @@ function viteLegacyPlugin(options = {}) {
7979
}
8080

8181
// legacy bundle
82-
isDebug &&
83-
console.log(
84-
`[@vitejs/plugin-legacy] legacy polyfills:`,
85-
legacyPolyfills
86-
)
8782
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+
8895
await buildPolyfillChunk(
8996
'polyfills-legacy',
9097
legacyPolyfills,
@@ -150,46 +157,16 @@ function viteLegacyPlugin(options = {}) {
150157
) {
151158
return null
152159
}
153-
154160
// 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)
185162
return null
186163
}
187164

188165
if (!genLegacy) {
189166
return
190167
}
191168

192-
const detectPolyfills =
169+
const needPolyfills =
193170
options.polyfills !== false && !Array.isArray(options.polyfills)
194171

195172
// transform the legacy chunk with @babel/preset-env
@@ -204,21 +181,20 @@ function viteLegacyPlugin(options = {}) {
204181
presetEnv,
205182
{
206183
targets,
207-
// modules are already converted to systemjs by rollup
208184
modules: false,
209185
bugfixes: true,
210-
useBuiltIns: detectPolyfills ? 'usage' : false,
211-
shippedProposals: true,
212-
corejs: detectPolyfills
186+
useBuiltIns: needPolyfills ? 'usage' : false,
187+
corejs: needPolyfills
213188
? { version: 3, proposals: false }
214189
: undefined,
190+
shippedProposals: true,
215191
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
216192
}
217193
]
218194
]
219195
})
220196

221-
if (detectPolyfills) {
197+
if (needPolyfills) {
222198
// detect and remove polyfill imports. Since the legacy bundle uses
223199
// format: 'system', any import declarations are polyfill imports injected
224200
// by @babel/preset-env.
@@ -336,6 +312,42 @@ function viteLegacyPlugin(options = {}) {
336312
return [legacyGenerateBundlePlugin, legacyPostPlugin]
337313
}
338314

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+
339351
/**
340352
* @param {string} name
341353
* @param {Set<string>} imports

0 commit comments

Comments
 (0)