@vitejs/plugin-legacy 5.4.0 → 5.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -89,9 +89,9 @@ npm add -D terser
89
89
 
90
90
  Set to a list of strings to explicitly control which polyfills to include. See [Polyfill Specifiers](#polyfill-specifiers) for details.
91
91
 
92
- Note it is **not recommended** to use the `true` value (which uses auto-detection) because `core-js@3` is very aggressive in polyfill inclusions due to all the bleeding edge features it supports. Even when targeting native ESM support, it injects 15kb of polyfills!
92
+ If `modernTargets` is not set, it is **not recommended** to use the `true` value (which uses auto-detection) because `core-js@3` is very aggressive in polyfill inclusions due to all the bleeding edge features it supports. Even when targeting native ESM support, it injects 15kb of polyfills!
93
93
 
94
- If you don't have hard reliance on bleeding edge runtime features, it is not that hard to avoid having to use polyfills in the modern build altogether. Alternatively, consider using an on-demand service like [Polyfill.io](https://polyfill.io) to only inject necessary polyfills based on actual browser user-agents (most modern browsers will need nothing!).
94
+ If you don't have hard reliance on bleeding edge runtime features, it is not that hard to avoid having to use polyfills in the modern build altogether. Alternatively, consider setting `modernTargets` or using an on-demand service like https://cdnjs.cloudflare.com/polyfill/ to only inject necessary polyfills based on actual browser user-agents (most modern browsers will need nothing!).
95
95
 
96
96
  ### `renderLegacyChunks`
97
97
 
package/dist/index.cjs CHANGED
@@ -23,17 +23,21 @@ function getDefaultExportFromCjs (x) {
23
23
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
24
24
  }
25
25
 
26
- var picocolors = {exports: {}};
26
+ function commonjsRequire(path) {
27
+ throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
28
+ }
27
29
 
28
- let tty = require$$0__default;
30
+ var picocolors = {exports: {}};
29
31
 
32
+ let argv = process.argv || [],
33
+ env = process.env;
30
34
  let isColorSupported =
31
- !("NO_COLOR" in process.env || process.argv.includes("--no-color")) &&
32
- ("FORCE_COLOR" in process.env ||
33
- process.argv.includes("--color") ||
35
+ !("NO_COLOR" in env || argv.includes("--no-color")) &&
36
+ ("FORCE_COLOR" in env ||
37
+ argv.includes("--color") ||
34
38
  process.platform === "win32" ||
35
- (tty.isatty(1) && process.env.TERM !== "dumb") ||
36
- "CI" in process.env);
39
+ (commonjsRequire != null && require$$0__default.isatty(1) && env.TERM !== "dumb") ||
40
+ "CI" in env);
37
41
 
38
42
  let formatter =
39
43
  (open, close, replace = open) =>
@@ -46,40 +50,47 @@ let formatter =
46
50
  };
47
51
 
48
52
  let replaceClose = (string, close, replace, index) => {
49
- let start = string.substring(0, index) + replace;
50
- let end = string.substring(index + close.length);
51
- let nextIndex = end.indexOf(close);
52
- return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
53
+ let result = "";
54
+ let cursor = 0;
55
+ do {
56
+ result += string.substring(cursor, index) + replace;
57
+ cursor = index + close.length;
58
+ index = string.indexOf(close, cursor);
59
+ } while (~index)
60
+ return result + string.substring(cursor)
53
61
  };
54
62
 
55
- let createColors = (enabled = isColorSupported) => ({
56
- isColorSupported: enabled,
57
- reset: enabled ? s => `\x1b[0m${s}\x1b[0m` : String,
58
- bold: enabled ? formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m") : String,
59
- dim: enabled ? formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m") : String,
60
- italic: enabled ? formatter("\x1b[3m", "\x1b[23m") : String,
61
- underline: enabled ? formatter("\x1b[4m", "\x1b[24m") : String,
62
- inverse: enabled ? formatter("\x1b[7m", "\x1b[27m") : String,
63
- hidden: enabled ? formatter("\x1b[8m", "\x1b[28m") : String,
64
- strikethrough: enabled ? formatter("\x1b[9m", "\x1b[29m") : String,
65
- black: enabled ? formatter("\x1b[30m", "\x1b[39m") : String,
66
- red: enabled ? formatter("\x1b[31m", "\x1b[39m") : String,
67
- green: enabled ? formatter("\x1b[32m", "\x1b[39m") : String,
68
- yellow: enabled ? formatter("\x1b[33m", "\x1b[39m") : String,
69
- blue: enabled ? formatter("\x1b[34m", "\x1b[39m") : String,
70
- magenta: enabled ? formatter("\x1b[35m", "\x1b[39m") : String,
71
- cyan: enabled ? formatter("\x1b[36m", "\x1b[39m") : String,
72
- white: enabled ? formatter("\x1b[37m", "\x1b[39m") : String,
73
- gray: enabled ? formatter("\x1b[90m", "\x1b[39m") : String,
74
- bgBlack: enabled ? formatter("\x1b[40m", "\x1b[49m") : String,
75
- bgRed: enabled ? formatter("\x1b[41m", "\x1b[49m") : String,
76
- bgGreen: enabled ? formatter("\x1b[42m", "\x1b[49m") : String,
77
- bgYellow: enabled ? formatter("\x1b[43m", "\x1b[49m") : String,
78
- bgBlue: enabled ? formatter("\x1b[44m", "\x1b[49m") : String,
79
- bgMagenta: enabled ? formatter("\x1b[45m", "\x1b[49m") : String,
80
- bgCyan: enabled ? formatter("\x1b[46m", "\x1b[49m") : String,
81
- bgWhite: enabled ? formatter("\x1b[47m", "\x1b[49m") : String,
82
- });
63
+ let createColors = (enabled = isColorSupported) => {
64
+ let init = enabled ? formatter : () => String;
65
+ return {
66
+ isColorSupported: enabled,
67
+ reset: init("\x1b[0m", "\x1b[0m"),
68
+ bold: init("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
69
+ dim: init("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
70
+ italic: init("\x1b[3m", "\x1b[23m"),
71
+ underline: init("\x1b[4m", "\x1b[24m"),
72
+ inverse: init("\x1b[7m", "\x1b[27m"),
73
+ hidden: init("\x1b[8m", "\x1b[28m"),
74
+ strikethrough: init("\x1b[9m", "\x1b[29m"),
75
+ black: init("\x1b[30m", "\x1b[39m"),
76
+ red: init("\x1b[31m", "\x1b[39m"),
77
+ green: init("\x1b[32m", "\x1b[39m"),
78
+ yellow: init("\x1b[33m", "\x1b[39m"),
79
+ blue: init("\x1b[34m", "\x1b[39m"),
80
+ magenta: init("\x1b[35m", "\x1b[39m"),
81
+ cyan: init("\x1b[36m", "\x1b[39m"),
82
+ white: init("\x1b[37m", "\x1b[39m"),
83
+ gray: init("\x1b[90m", "\x1b[39m"),
84
+ bgBlack: init("\x1b[40m", "\x1b[49m"),
85
+ bgRed: init("\x1b[41m", "\x1b[49m"),
86
+ bgGreen: init("\x1b[42m", "\x1b[49m"),
87
+ bgYellow: init("\x1b[43m", "\x1b[49m"),
88
+ bgBlue: init("\x1b[44m", "\x1b[49m"),
89
+ bgMagenta: init("\x1b[45m", "\x1b[49m"),
90
+ bgCyan: init("\x1b[46m", "\x1b[49m"),
91
+ bgWhite: init("\x1b[47m", "\x1b[49m"),
92
+ }
93
+ };
83
94
 
84
95
  picocolors.exports = createColors();
85
96
  picocolors.exports.createColors = createColors;
@@ -131,7 +142,7 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
131
142
  if (relative && !config.build.ssr) {
132
143
  return toRelative(filename, hostId);
133
144
  } else {
134
- return config.base + filename;
145
+ return joinUrlSegments(config.decodedBase, filename);
135
146
  }
136
147
  }
137
148
  function getBaseInHTML(urlRelativePath, config) {
@@ -140,6 +151,18 @@ function getBaseInHTML(urlRelativePath, config) {
140
151
  "./"
141
152
  ) : config.base;
142
153
  }
154
+ function joinUrlSegments(a, b) {
155
+ if (!a || !b) {
156
+ return a || b || "";
157
+ }
158
+ if (a[a.length - 1] === "/") {
159
+ a = a.substring(0, a.length - 1);
160
+ }
161
+ if (b[0] !== "/") {
162
+ b = "/" + b;
163
+ }
164
+ return a + b;
165
+ }
143
166
  function toAssetPathFromHtml(filename, htmlPath, config) {
144
167
  const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
145
168
  const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
@@ -155,7 +178,7 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
155
178
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
156
179
  const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
157
180
  const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
158
- const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/;
181
+ const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
159
182
  function viteLegacyPlugin(options = {}) {
160
183
  let config;
161
184
  let targets;
@@ -182,6 +205,7 @@ function viteLegacyPlugin(options = {}) {
182
205
  const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
183
206
  const modernPolyfills = /* @__PURE__ */ new Set();
184
207
  const legacyPolyfills = /* @__PURE__ */ new Set();
208
+ const outputToChunkFileNameToPolyfills = /* @__PURE__ */ new WeakMap();
185
209
  if (Array.isArray(options.modernPolyfills) && genModern) {
186
210
  options.modernPolyfills.forEach((i) => {
187
211
  modernPolyfills.add(
@@ -263,7 +287,16 @@ function viteLegacyPlugin(options = {}) {
263
287
  if (config.build.ssr) {
264
288
  return;
265
289
  }
290
+ const chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
291
+ if (chunkFileNameToPolyfills == null) {
292
+ throw new Error(
293
+ "Internal @vitejs/plugin-legacy error: discovered polyfills should exist"
294
+ );
295
+ }
266
296
  if (!isLegacyBundle(bundle, opts)) {
297
+ for (const { modern } of chunkFileNameToPolyfills.values()) {
298
+ modern.forEach((p) => modernPolyfills.add(p));
299
+ }
267
300
  if (!modernPolyfills.size) {
268
301
  return;
269
302
  }
@@ -289,6 +322,9 @@ function viteLegacyPlugin(options = {}) {
289
322
  if (!genLegacy) {
290
323
  return;
291
324
  }
325
+ for (const { legacy } of chunkFileNameToPolyfills.values()) {
326
+ legacy.forEach((p) => legacyPolyfills.add(p));
327
+ }
292
328
  if (options.polyfills !== false) {
293
329
  await detectPolyfills(
294
330
  `Promise.resolve(); Promise.all();`,
@@ -320,6 +356,9 @@ function viteLegacyPlugin(options = {}) {
320
356
  name: "vite:legacy-post-process",
321
357
  enforce: "post",
322
358
  apply: "build",
359
+ renderStart(opts) {
360
+ outputToChunkFileNameToPolyfills.set(opts, null);
361
+ },
323
362
  configResolved(_config) {
324
363
  if (_config.build.lib) {
325
364
  throw new Error("@vitejs/plugin-legacy does not support library mode.");
@@ -370,13 +409,30 @@ function viteLegacyPlugin(options = {}) {
370
409
  ];
371
410
  }
372
411
  },
373
- async renderChunk(raw, chunk, opts) {
412
+ async renderChunk(raw, chunk, opts, { chunks }) {
374
413
  if (config.build.ssr) {
375
414
  return null;
376
415
  }
416
+ let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
417
+ if (chunkFileNameToPolyfills == null) {
418
+ chunkFileNameToPolyfills = /* @__PURE__ */ new Map();
419
+ for (const fileName in chunks) {
420
+ chunkFileNameToPolyfills.set(fileName, {
421
+ modern: /* @__PURE__ */ new Set(),
422
+ legacy: /* @__PURE__ */ new Set()
423
+ });
424
+ }
425
+ outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills);
426
+ }
427
+ const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName);
428
+ if (polyfillsDiscovered == null) {
429
+ throw new Error(
430
+ `Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`
431
+ );
432
+ }
377
433
  if (!isLegacyChunk(chunk, opts)) {
378
434
  if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
379
- await detectPolyfills(raw, modernTargets, modernPolyfills);
435
+ await detectPolyfills(raw, modernTargets, polyfillsDiscovered.modern);
380
436
  }
381
437
  const ms = new MagicString__default(raw);
382
438
  if (genLegacy && chunk.isEntry) {
@@ -425,7 +481,7 @@ function viteLegacyPlugin(options = {}) {
425
481
  [
426
482
  () => ({
427
483
  plugins: [
428
- recordAndRemovePolyfillBabelPlugin(legacyPolyfills),
484
+ recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
429
485
  replaceLegacyEnvBabelPlugin(),
430
486
  wrapIIFEBabelPlugin()
431
487
  ]
package/dist/index.mjs CHANGED
@@ -11,17 +11,21 @@ function getDefaultExportFromCjs (x) {
11
11
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
12
12
  }
13
13
 
14
- var picocolors = {exports: {}};
14
+ function commonjsRequire(path) {
15
+ throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
16
+ }
15
17
 
16
- let tty = require$$0;
18
+ var picocolors = {exports: {}};
17
19
 
20
+ let argv = process.argv || [],
21
+ env = process.env;
18
22
  let isColorSupported =
19
- !("NO_COLOR" in process.env || process.argv.includes("--no-color")) &&
20
- ("FORCE_COLOR" in process.env ||
21
- process.argv.includes("--color") ||
23
+ !("NO_COLOR" in env || argv.includes("--no-color")) &&
24
+ ("FORCE_COLOR" in env ||
25
+ argv.includes("--color") ||
22
26
  process.platform === "win32" ||
23
- (tty.isatty(1) && process.env.TERM !== "dumb") ||
24
- "CI" in process.env);
27
+ (commonjsRequire != null && require$$0.isatty(1) && env.TERM !== "dumb") ||
28
+ "CI" in env);
25
29
 
26
30
  let formatter =
27
31
  (open, close, replace = open) =>
@@ -34,40 +38,47 @@ let formatter =
34
38
  };
35
39
 
36
40
  let replaceClose = (string, close, replace, index) => {
37
- let start = string.substring(0, index) + replace;
38
- let end = string.substring(index + close.length);
39
- let nextIndex = end.indexOf(close);
40
- return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
41
+ let result = "";
42
+ let cursor = 0;
43
+ do {
44
+ result += string.substring(cursor, index) + replace;
45
+ cursor = index + close.length;
46
+ index = string.indexOf(close, cursor);
47
+ } while (~index)
48
+ return result + string.substring(cursor)
41
49
  };
42
50
 
43
- let createColors = (enabled = isColorSupported) => ({
44
- isColorSupported: enabled,
45
- reset: enabled ? s => `\x1b[0m${s}\x1b[0m` : String,
46
- bold: enabled ? formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m") : String,
47
- dim: enabled ? formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m") : String,
48
- italic: enabled ? formatter("\x1b[3m", "\x1b[23m") : String,
49
- underline: enabled ? formatter("\x1b[4m", "\x1b[24m") : String,
50
- inverse: enabled ? formatter("\x1b[7m", "\x1b[27m") : String,
51
- hidden: enabled ? formatter("\x1b[8m", "\x1b[28m") : String,
52
- strikethrough: enabled ? formatter("\x1b[9m", "\x1b[29m") : String,
53
- black: enabled ? formatter("\x1b[30m", "\x1b[39m") : String,
54
- red: enabled ? formatter("\x1b[31m", "\x1b[39m") : String,
55
- green: enabled ? formatter("\x1b[32m", "\x1b[39m") : String,
56
- yellow: enabled ? formatter("\x1b[33m", "\x1b[39m") : String,
57
- blue: enabled ? formatter("\x1b[34m", "\x1b[39m") : String,
58
- magenta: enabled ? formatter("\x1b[35m", "\x1b[39m") : String,
59
- cyan: enabled ? formatter("\x1b[36m", "\x1b[39m") : String,
60
- white: enabled ? formatter("\x1b[37m", "\x1b[39m") : String,
61
- gray: enabled ? formatter("\x1b[90m", "\x1b[39m") : String,
62
- bgBlack: enabled ? formatter("\x1b[40m", "\x1b[49m") : String,
63
- bgRed: enabled ? formatter("\x1b[41m", "\x1b[49m") : String,
64
- bgGreen: enabled ? formatter("\x1b[42m", "\x1b[49m") : String,
65
- bgYellow: enabled ? formatter("\x1b[43m", "\x1b[49m") : String,
66
- bgBlue: enabled ? formatter("\x1b[44m", "\x1b[49m") : String,
67
- bgMagenta: enabled ? formatter("\x1b[45m", "\x1b[49m") : String,
68
- bgCyan: enabled ? formatter("\x1b[46m", "\x1b[49m") : String,
69
- bgWhite: enabled ? formatter("\x1b[47m", "\x1b[49m") : String,
70
- });
51
+ let createColors = (enabled = isColorSupported) => {
52
+ let init = enabled ? formatter : () => String;
53
+ return {
54
+ isColorSupported: enabled,
55
+ reset: init("\x1b[0m", "\x1b[0m"),
56
+ bold: init("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
57
+ dim: init("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
58
+ italic: init("\x1b[3m", "\x1b[23m"),
59
+ underline: init("\x1b[4m", "\x1b[24m"),
60
+ inverse: init("\x1b[7m", "\x1b[27m"),
61
+ hidden: init("\x1b[8m", "\x1b[28m"),
62
+ strikethrough: init("\x1b[9m", "\x1b[29m"),
63
+ black: init("\x1b[30m", "\x1b[39m"),
64
+ red: init("\x1b[31m", "\x1b[39m"),
65
+ green: init("\x1b[32m", "\x1b[39m"),
66
+ yellow: init("\x1b[33m", "\x1b[39m"),
67
+ blue: init("\x1b[34m", "\x1b[39m"),
68
+ magenta: init("\x1b[35m", "\x1b[39m"),
69
+ cyan: init("\x1b[36m", "\x1b[39m"),
70
+ white: init("\x1b[37m", "\x1b[39m"),
71
+ gray: init("\x1b[90m", "\x1b[39m"),
72
+ bgBlack: init("\x1b[40m", "\x1b[49m"),
73
+ bgRed: init("\x1b[41m", "\x1b[49m"),
74
+ bgGreen: init("\x1b[42m", "\x1b[49m"),
75
+ bgYellow: init("\x1b[43m", "\x1b[49m"),
76
+ bgBlue: init("\x1b[44m", "\x1b[49m"),
77
+ bgMagenta: init("\x1b[45m", "\x1b[49m"),
78
+ bgCyan: init("\x1b[46m", "\x1b[49m"),
79
+ bgWhite: init("\x1b[47m", "\x1b[49m"),
80
+ }
81
+ };
71
82
 
72
83
  picocolors.exports = createColors();
73
84
  picocolors.exports.createColors = createColors;
@@ -119,7 +130,7 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
119
130
  if (relative && !config.build.ssr) {
120
131
  return toRelative(filename, hostId);
121
132
  } else {
122
- return config.base + filename;
133
+ return joinUrlSegments(config.decodedBase, filename);
123
134
  }
124
135
  }
125
136
  function getBaseInHTML(urlRelativePath, config) {
@@ -128,6 +139,18 @@ function getBaseInHTML(urlRelativePath, config) {
128
139
  "./"
129
140
  ) : config.base;
130
141
  }
142
+ function joinUrlSegments(a, b) {
143
+ if (!a || !b) {
144
+ return a || b || "";
145
+ }
146
+ if (a[a.length - 1] === "/") {
147
+ a = a.substring(0, a.length - 1);
148
+ }
149
+ if (b[0] !== "/") {
150
+ b = "/" + b;
151
+ }
152
+ return a + b;
153
+ }
131
154
  function toAssetPathFromHtml(filename, htmlPath, config) {
132
155
  const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
133
156
  const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
@@ -143,7 +166,7 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
143
166
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
144
167
  const _require = createRequire(import.meta.url);
145
168
  const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
146
- const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/;
169
+ const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
147
170
  function viteLegacyPlugin(options = {}) {
148
171
  let config;
149
172
  let targets;
@@ -170,6 +193,7 @@ function viteLegacyPlugin(options = {}) {
170
193
  const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
171
194
  const modernPolyfills = /* @__PURE__ */ new Set();
172
195
  const legacyPolyfills = /* @__PURE__ */ new Set();
196
+ const outputToChunkFileNameToPolyfills = /* @__PURE__ */ new WeakMap();
173
197
  if (Array.isArray(options.modernPolyfills) && genModern) {
174
198
  options.modernPolyfills.forEach((i) => {
175
199
  modernPolyfills.add(
@@ -251,7 +275,16 @@ function viteLegacyPlugin(options = {}) {
251
275
  if (config.build.ssr) {
252
276
  return;
253
277
  }
278
+ const chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
279
+ if (chunkFileNameToPolyfills == null) {
280
+ throw new Error(
281
+ "Internal @vitejs/plugin-legacy error: discovered polyfills should exist"
282
+ );
283
+ }
254
284
  if (!isLegacyBundle(bundle, opts)) {
285
+ for (const { modern } of chunkFileNameToPolyfills.values()) {
286
+ modern.forEach((p) => modernPolyfills.add(p));
287
+ }
255
288
  if (!modernPolyfills.size) {
256
289
  return;
257
290
  }
@@ -277,6 +310,9 @@ function viteLegacyPlugin(options = {}) {
277
310
  if (!genLegacy) {
278
311
  return;
279
312
  }
313
+ for (const { legacy } of chunkFileNameToPolyfills.values()) {
314
+ legacy.forEach((p) => legacyPolyfills.add(p));
315
+ }
280
316
  if (options.polyfills !== false) {
281
317
  await detectPolyfills(
282
318
  `Promise.resolve(); Promise.all();`,
@@ -308,6 +344,9 @@ function viteLegacyPlugin(options = {}) {
308
344
  name: "vite:legacy-post-process",
309
345
  enforce: "post",
310
346
  apply: "build",
347
+ renderStart(opts) {
348
+ outputToChunkFileNameToPolyfills.set(opts, null);
349
+ },
311
350
  configResolved(_config) {
312
351
  if (_config.build.lib) {
313
352
  throw new Error("@vitejs/plugin-legacy does not support library mode.");
@@ -358,13 +397,30 @@ function viteLegacyPlugin(options = {}) {
358
397
  ];
359
398
  }
360
399
  },
361
- async renderChunk(raw, chunk, opts) {
400
+ async renderChunk(raw, chunk, opts, { chunks }) {
362
401
  if (config.build.ssr) {
363
402
  return null;
364
403
  }
404
+ let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
405
+ if (chunkFileNameToPolyfills == null) {
406
+ chunkFileNameToPolyfills = /* @__PURE__ */ new Map();
407
+ for (const fileName in chunks) {
408
+ chunkFileNameToPolyfills.set(fileName, {
409
+ modern: /* @__PURE__ */ new Set(),
410
+ legacy: /* @__PURE__ */ new Set()
411
+ });
412
+ }
413
+ outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills);
414
+ }
415
+ const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName);
416
+ if (polyfillsDiscovered == null) {
417
+ throw new Error(
418
+ `Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`
419
+ );
420
+ }
365
421
  if (!isLegacyChunk(chunk, opts)) {
366
422
  if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
367
- await detectPolyfills(raw, modernTargets, modernPolyfills);
423
+ await detectPolyfills(raw, modernTargets, polyfillsDiscovered.modern);
368
424
  }
369
425
  const ms = new MagicString(raw);
370
426
  if (genLegacy && chunk.isEntry) {
@@ -413,7 +469,7 @@ function viteLegacyPlugin(options = {}) {
413
469
  [
414
470
  () => ({
415
471
  plugins: [
416
- recordAndRemovePolyfillBabelPlugin(legacyPolyfills),
472
+ recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
417
473
  replaceLegacyEnvBabelPlugin(),
418
474
  wrapIIFEBabelPlugin()
419
475
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "5.4.0",
3
+ "version": "5.4.2",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -35,12 +35,12 @@
35
35
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
36
36
  "funding": "https://github.com/vitejs/vite?sponsor=1",
37
37
  "dependencies": {
38
- "@babel/core": "^7.24.5",
39
- "@babel/preset-env": "^7.24.5",
40
- "browserslist": "^4.23.0",
38
+ "@babel/core": "^7.25.2",
39
+ "@babel/preset-env": "^7.25.3",
40
+ "browserslist": "^4.23.3",
41
41
  "browserslist-to-esbuild": "^2.1.1",
42
- "core-js": "^3.37.0",
43
- "magic-string": "^0.30.10",
42
+ "core-js": "^3.38.0",
43
+ "magic-string": "^0.30.11",
44
44
  "regenerator-runtime": "^0.14.1",
45
45
  "systemjs": "^6.15.1"
46
46
  },
@@ -49,9 +49,10 @@
49
49
  "vite": "^5.0.0"
50
50
  },
51
51
  "devDependencies": {
52
- "acorn": "^8.11.3",
53
- "picocolors": "^1.0.0",
54
- "vite": "5.2.11"
52
+ "acorn": "^8.12.1",
53
+ "picocolors": "^1.0.1",
54
+ "unbuild": "^2.0.0",
55
+ "vite": "5.4.1"
55
56
  },
56
57
  "scripts": {
57
58
  "dev": "unbuild --stub",