@vitejs/plugin-legacy 4.0.5 → 4.1.1

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
@@ -117,6 +117,13 @@ npm add -D terser
117
117
 
118
118
  Defaults to `false`. Enabling this option will exclude `systemjs/dist/s.min.js` inside polyfills-legacy chunk.
119
119
 
120
+ ### `renderModernChunks`
121
+
122
+ - **Type:** `boolean`
123
+ - **Default:** `true`
124
+
125
+ Set to `false` to only output the legacy bundles that support all target browsers.
126
+
120
127
  ## Browsers that supports ESM but does not support widely-available features
121
128
 
122
129
  The legacy plugin offers a way to use widely-available features natively in the modern build, while falling back to the legacy build in browsers with native ESM but without those features supported (e.g. Legacy Edge). This feature works by injecting a runtime check and loading the legacy bundle with SystemJs runtime if needed. There are the following drawbacks:
package/dist/index.cjs CHANGED
@@ -157,6 +157,12 @@ function viteLegacyPlugin(options = {}) {
157
157
  let config;
158
158
  let targets;
159
159
  const genLegacy = options.renderLegacyChunks !== false;
160
+ const genModern = options.renderModernChunks !== false;
161
+ if (!genLegacy && !genModern) {
162
+ throw new Error(
163
+ "`renderLegacyChunks` and `renderModernChunks` cannot be both false"
164
+ );
165
+ }
160
166
  const debugFlags = (process.env.DEBUG || "").split(",");
161
167
  const isDebug = debugFlags.includes("vite:*") || debugFlags.includes("vite:legacy");
162
168
  const facadeToLegacyChunkMap = /* @__PURE__ */ new Map();
@@ -164,7 +170,7 @@ function viteLegacyPlugin(options = {}) {
164
170
  const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
165
171
  const modernPolyfills = /* @__PURE__ */ new Set();
166
172
  const legacyPolyfills = /* @__PURE__ */ new Set();
167
- if (Array.isArray(options.modernPolyfills)) {
173
+ if (Array.isArray(options.modernPolyfills) && genModern) {
168
174
  options.modernPolyfills.forEach((i) => {
169
175
  modernPolyfills.add(
170
176
  i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
@@ -319,9 +325,15 @@ function viteLegacyPlugin(options = {}) {
319
325
  const { rollupOptions } = config.build;
320
326
  const { output } = rollupOptions;
321
327
  if (Array.isArray(output)) {
322
- rollupOptions.output = [...output.map(createLegacyOutput), ...output];
328
+ rollupOptions.output = [
329
+ ...output.map(createLegacyOutput),
330
+ ...genModern ? output : []
331
+ ];
323
332
  } else {
324
- rollupOptions.output = [createLegacyOutput(output), output || {}];
333
+ rollupOptions.output = [
334
+ createLegacyOutput(output),
335
+ ...genModern ? [output || {}] : []
336
+ ];
325
337
  }
326
338
  },
327
339
  async renderChunk(raw, chunk, opts) {
@@ -329,7 +341,7 @@ function viteLegacyPlugin(options = {}) {
329
341
  return null;
330
342
  }
331
343
  if (!isLegacyChunk(chunk, opts)) {
332
- if (options.modernPolyfills && !Array.isArray(options.modernPolyfills)) {
344
+ if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
333
345
  await detectPolyfills(raw, { esmodules: true }, modernPolyfills);
334
346
  }
335
347
  const ms = new MagicString__default(raw);
@@ -405,40 +417,49 @@ function viteLegacyPlugin(options = {}) {
405
417
  return;
406
418
  if (chunk.fileName.includes("-legacy")) {
407
419
  facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
408
- return;
420
+ if (genModern) {
421
+ return;
422
+ }
423
+ }
424
+ if (!genModern) {
425
+ html = html.replace(/<script type="module".*?<\/script>/g, "");
409
426
  }
410
427
  const tags = [];
411
428
  const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
412
- const modernPolyfillFilename = facadeToModernPolyfillMap.get(
413
- chunk.facadeModuleId
414
- );
415
- if (modernPolyfillFilename) {
416
- tags.push({
417
- tag: "script",
418
- attrs: {
419
- type: "module",
420
- crossorigin: true,
421
- src: toAssetPathFromHtml(
422
- modernPolyfillFilename,
423
- chunk.facadeModuleId,
424
- config
425
- )
426
- }
427
- });
428
- } else if (modernPolyfills.size) {
429
- throw new Error(
430
- `No corresponding modern polyfill chunk found for ${htmlFilename}`
429
+ if (genModern) {
430
+ const modernPolyfillFilename = facadeToModernPolyfillMap.get(
431
+ chunk.facadeModuleId
431
432
  );
433
+ if (modernPolyfillFilename) {
434
+ tags.push({
435
+ tag: "script",
436
+ attrs: {
437
+ type: "module",
438
+ crossorigin: true,
439
+ src: toAssetPathFromHtml(
440
+ modernPolyfillFilename,
441
+ chunk.facadeModuleId,
442
+ config
443
+ )
444
+ }
445
+ });
446
+ } else if (modernPolyfills.size) {
447
+ throw new Error(
448
+ `No corresponding modern polyfill chunk found for ${htmlFilename}`
449
+ );
450
+ }
432
451
  }
433
452
  if (!genLegacy) {
434
453
  return { html, tags };
435
454
  }
436
- tags.push({
437
- tag: "script",
438
- attrs: { nomodule: true },
439
- children: safari10NoModuleFix,
440
- injectTo: "body"
441
- });
455
+ if (genModern) {
456
+ tags.push({
457
+ tag: "script",
458
+ attrs: { nomodule: genModern },
459
+ children: safari10NoModuleFix,
460
+ injectTo: "body"
461
+ });
462
+ }
442
463
  const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(
443
464
  chunk.facadeModuleId
444
465
  );
@@ -446,7 +467,7 @@ function viteLegacyPlugin(options = {}) {
446
467
  tags.push({
447
468
  tag: "script",
448
469
  attrs: {
449
- nomodule: true,
470
+ nomodule: genModern,
450
471
  crossorigin: true,
451
472
  id: legacyPolyfillId,
452
473
  src: toAssetPathFromHtml(
@@ -469,7 +490,7 @@ function viteLegacyPlugin(options = {}) {
469
490
  tags.push({
470
491
  tag: "script",
471
492
  attrs: {
472
- nomodule: true,
493
+ nomodule: genModern,
473
494
  crossorigin: true,
474
495
  // we set the entry path on the element as an attribute so that the
475
496
  // script content will stay consistent - which allows using a constant
@@ -489,7 +510,7 @@ function viteLegacyPlugin(options = {}) {
489
510
  `No corresponding legacy entry chunk found for ${htmlFilename}`
490
511
  );
491
512
  }
492
- if (genLegacy && legacyPolyfillFilename && legacyEntryFilename) {
513
+ if (legacyPolyfillFilename && legacyEntryFilename && genModern) {
493
514
  tags.push({
494
515
  tag: "script",
495
516
  attrs: { type: "module" },
@@ -512,7 +533,7 @@ function viteLegacyPlugin(options = {}) {
512
533
  if (config.build.ssr) {
513
534
  return;
514
535
  }
515
- if (isLegacyBundle(bundle, opts)) {
536
+ if (isLegacyBundle(bundle, opts) && genModern) {
516
537
  for (const name in bundle) {
517
538
  if (bundle[name].type === "asset" && !/.+\.map$/.test(name)) {
518
539
  delete bundle[name];
package/dist/index.d.ts CHANGED
@@ -28,6 +28,10 @@ interface Options {
28
28
  * default: false
29
29
  */
30
30
  externalSystemJS?: boolean;
31
+ /**
32
+ * default: true
33
+ */
34
+ renderModernChunks?: boolean;
31
35
  }
32
36
 
33
37
  declare function viteLegacyPlugin(options?: Options): Plugin[];
package/dist/index.mjs CHANGED
@@ -146,6 +146,12 @@ function viteLegacyPlugin(options = {}) {
146
146
  let config;
147
147
  let targets;
148
148
  const genLegacy = options.renderLegacyChunks !== false;
149
+ const genModern = options.renderModernChunks !== false;
150
+ if (!genLegacy && !genModern) {
151
+ throw new Error(
152
+ "`renderLegacyChunks` and `renderModernChunks` cannot be both false"
153
+ );
154
+ }
149
155
  const debugFlags = (process.env.DEBUG || "").split(",");
150
156
  const isDebug = debugFlags.includes("vite:*") || debugFlags.includes("vite:legacy");
151
157
  const facadeToLegacyChunkMap = /* @__PURE__ */ new Map();
@@ -153,7 +159,7 @@ function viteLegacyPlugin(options = {}) {
153
159
  const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
154
160
  const modernPolyfills = /* @__PURE__ */ new Set();
155
161
  const legacyPolyfills = /* @__PURE__ */ new Set();
156
- if (Array.isArray(options.modernPolyfills)) {
162
+ if (Array.isArray(options.modernPolyfills) && genModern) {
157
163
  options.modernPolyfills.forEach((i) => {
158
164
  modernPolyfills.add(
159
165
  i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
@@ -308,9 +314,15 @@ function viteLegacyPlugin(options = {}) {
308
314
  const { rollupOptions } = config.build;
309
315
  const { output } = rollupOptions;
310
316
  if (Array.isArray(output)) {
311
- rollupOptions.output = [...output.map(createLegacyOutput), ...output];
317
+ rollupOptions.output = [
318
+ ...output.map(createLegacyOutput),
319
+ ...genModern ? output : []
320
+ ];
312
321
  } else {
313
- rollupOptions.output = [createLegacyOutput(output), output || {}];
322
+ rollupOptions.output = [
323
+ createLegacyOutput(output),
324
+ ...genModern ? [output || {}] : []
325
+ ];
314
326
  }
315
327
  },
316
328
  async renderChunk(raw, chunk, opts) {
@@ -318,7 +330,7 @@ function viteLegacyPlugin(options = {}) {
318
330
  return null;
319
331
  }
320
332
  if (!isLegacyChunk(chunk, opts)) {
321
- if (options.modernPolyfills && !Array.isArray(options.modernPolyfills)) {
333
+ if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
322
334
  await detectPolyfills(raw, { esmodules: true }, modernPolyfills);
323
335
  }
324
336
  const ms = new MagicString(raw);
@@ -394,40 +406,49 @@ function viteLegacyPlugin(options = {}) {
394
406
  return;
395
407
  if (chunk.fileName.includes("-legacy")) {
396
408
  facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
397
- return;
409
+ if (genModern) {
410
+ return;
411
+ }
412
+ }
413
+ if (!genModern) {
414
+ html = html.replace(/<script type="module".*?<\/script>/g, "");
398
415
  }
399
416
  const tags = [];
400
417
  const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
401
- const modernPolyfillFilename = facadeToModernPolyfillMap.get(
402
- chunk.facadeModuleId
403
- );
404
- if (modernPolyfillFilename) {
405
- tags.push({
406
- tag: "script",
407
- attrs: {
408
- type: "module",
409
- crossorigin: true,
410
- src: toAssetPathFromHtml(
411
- modernPolyfillFilename,
412
- chunk.facadeModuleId,
413
- config
414
- )
415
- }
416
- });
417
- } else if (modernPolyfills.size) {
418
- throw new Error(
419
- `No corresponding modern polyfill chunk found for ${htmlFilename}`
418
+ if (genModern) {
419
+ const modernPolyfillFilename = facadeToModernPolyfillMap.get(
420
+ chunk.facadeModuleId
420
421
  );
422
+ if (modernPolyfillFilename) {
423
+ tags.push({
424
+ tag: "script",
425
+ attrs: {
426
+ type: "module",
427
+ crossorigin: true,
428
+ src: toAssetPathFromHtml(
429
+ modernPolyfillFilename,
430
+ chunk.facadeModuleId,
431
+ config
432
+ )
433
+ }
434
+ });
435
+ } else if (modernPolyfills.size) {
436
+ throw new Error(
437
+ `No corresponding modern polyfill chunk found for ${htmlFilename}`
438
+ );
439
+ }
421
440
  }
422
441
  if (!genLegacy) {
423
442
  return { html, tags };
424
443
  }
425
- tags.push({
426
- tag: "script",
427
- attrs: { nomodule: true },
428
- children: safari10NoModuleFix,
429
- injectTo: "body"
430
- });
444
+ if (genModern) {
445
+ tags.push({
446
+ tag: "script",
447
+ attrs: { nomodule: genModern },
448
+ children: safari10NoModuleFix,
449
+ injectTo: "body"
450
+ });
451
+ }
431
452
  const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(
432
453
  chunk.facadeModuleId
433
454
  );
@@ -435,7 +456,7 @@ function viteLegacyPlugin(options = {}) {
435
456
  tags.push({
436
457
  tag: "script",
437
458
  attrs: {
438
- nomodule: true,
459
+ nomodule: genModern,
439
460
  crossorigin: true,
440
461
  id: legacyPolyfillId,
441
462
  src: toAssetPathFromHtml(
@@ -458,7 +479,7 @@ function viteLegacyPlugin(options = {}) {
458
479
  tags.push({
459
480
  tag: "script",
460
481
  attrs: {
461
- nomodule: true,
482
+ nomodule: genModern,
462
483
  crossorigin: true,
463
484
  // we set the entry path on the element as an attribute so that the
464
485
  // script content will stay consistent - which allows using a constant
@@ -478,7 +499,7 @@ function viteLegacyPlugin(options = {}) {
478
499
  `No corresponding legacy entry chunk found for ${htmlFilename}`
479
500
  );
480
501
  }
481
- if (genLegacy && legacyPolyfillFilename && legacyEntryFilename) {
502
+ if (legacyPolyfillFilename && legacyEntryFilename && genModern) {
482
503
  tags.push({
483
504
  tag: "script",
484
505
  attrs: { type: "module" },
@@ -501,7 +522,7 @@ function viteLegacyPlugin(options = {}) {
501
522
  if (config.build.ssr) {
502
523
  return;
503
524
  }
504
- if (isLegacyBundle(bundle, opts)) {
525
+ if (isLegacyBundle(bundle, opts) && genModern) {
505
526
  for (const name in bundle) {
506
527
  if (bundle[name].type === "asset" && !/.+\.map$/.test(name)) {
507
528
  delete bundle[name];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "4.0.5",
3
+ "version": "4.1.1",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -42,11 +42,11 @@
42
42
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
43
43
  "funding": "https://github.com/vitejs/vite?sponsor=1",
44
44
  "dependencies": {
45
- "@babel/core": "^7.22.5",
46
- "@babel/preset-env": "^7.22.5",
45
+ "@babel/core": "^7.22.9",
46
+ "@babel/preset-env": "^7.22.9",
47
47
  "browserslist": "^4.21.9",
48
- "core-js": "^3.31.0",
49
- "magic-string": "^0.30.0",
48
+ "core-js": "^3.31.1",
49
+ "magic-string": "^0.30.1",
50
50
  "regenerator-runtime": "^0.13.11",
51
51
  "systemjs": "^6.14.1"
52
52
  },
@@ -55,7 +55,7 @@
55
55
  "vite": "^4.0.0"
56
56
  },
57
57
  "devDependencies": {
58
- "acorn": "^8.9.0",
58
+ "acorn": "^8.10.0",
59
59
  "picocolors": "^1.0.0",
60
60
  "vite": "workspace:*"
61
61
  }