@vitejs/plugin-legacy 5.4.2 → 5.4.3

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
3
+ Copyright (c) 2019-present, VoidZero Inc. and Vite contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.cjs CHANGED
@@ -3,55 +3,40 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const path = require('node:path');
6
- const node_crypto = require('node:crypto');
6
+ const crypto = require('node:crypto');
7
7
  const node_module = require('node:module');
8
8
  const node_url = require('node:url');
9
9
  const vite = require('vite');
10
10
  const MagicString = require('magic-string');
11
- const require$$0 = require('tty');
12
11
  const browserslist = require('browserslist');
13
12
 
14
13
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
15
14
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
16
15
 
17
16
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
17
+ const crypto__default = /*#__PURE__*/_interopDefaultCompat(crypto);
18
18
  const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
19
- const require$$0__default = /*#__PURE__*/_interopDefaultCompat(require$$0);
20
19
  const browserslist__default = /*#__PURE__*/_interopDefaultCompat(browserslist);
21
20
 
22
21
  function getDefaultExportFromCjs (x) {
23
22
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
24
23
  }
25
24
 
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
- }
29
-
30
25
  var picocolors = {exports: {}};
31
26
 
32
- let argv = process.argv || [],
33
- env = process.env;
27
+ let p = process || {}, argv = p.argv || [], env = p.env || {};
34
28
  let isColorSupported =
35
- !("NO_COLOR" in env || argv.includes("--no-color")) &&
36
- ("FORCE_COLOR" in env ||
37
- argv.includes("--color") ||
38
- process.platform === "win32" ||
39
- (commonjsRequire != null && require$$0__default.isatty(1) && env.TERM !== "dumb") ||
40
- "CI" in env);
29
+ !(!!env.NO_COLOR || argv.includes("--no-color")) &&
30
+ (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || ((p.stdout || {}).isTTY && env.TERM !== "dumb") || !!env.CI);
41
31
 
42
- let formatter =
43
- (open, close, replace = open) =>
32
+ let formatter = (open, close, replace = open) =>
44
33
  input => {
45
- let string = "" + input;
46
- let index = string.indexOf(close, open.length);
47
- return ~index
48
- ? open + replaceClose(string, close, replace, index) + close
49
- : open + string + close
34
+ let string = "" + input, index = string.indexOf(close, open.length);
35
+ return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close
50
36
  };
51
37
 
52
38
  let replaceClose = (string, close, replace, index) => {
53
- let result = "";
54
- let cursor = 0;
39
+ let result = "", cursor = 0;
55
40
  do {
56
41
  result += string.substring(cursor, index) + replace;
57
42
  cursor = index + close.length;
@@ -61,34 +46,54 @@ let replaceClose = (string, close, replace, index) => {
61
46
  };
62
47
 
63
48
  let createColors = (enabled = isColorSupported) => {
64
- let init = enabled ? formatter : () => String;
49
+ let f = enabled ? formatter : () => String;
65
50
  return {
66
51
  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"),
52
+ reset: f("\x1b[0m", "\x1b[0m"),
53
+ bold: f("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
54
+ dim: f("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
55
+ italic: f("\x1b[3m", "\x1b[23m"),
56
+ underline: f("\x1b[4m", "\x1b[24m"),
57
+ inverse: f("\x1b[7m", "\x1b[27m"),
58
+ hidden: f("\x1b[8m", "\x1b[28m"),
59
+ strikethrough: f("\x1b[9m", "\x1b[29m"),
60
+
61
+ black: f("\x1b[30m", "\x1b[39m"),
62
+ red: f("\x1b[31m", "\x1b[39m"),
63
+ green: f("\x1b[32m", "\x1b[39m"),
64
+ yellow: f("\x1b[33m", "\x1b[39m"),
65
+ blue: f("\x1b[34m", "\x1b[39m"),
66
+ magenta: f("\x1b[35m", "\x1b[39m"),
67
+ cyan: f("\x1b[36m", "\x1b[39m"),
68
+ white: f("\x1b[37m", "\x1b[39m"),
69
+ gray: f("\x1b[90m", "\x1b[39m"),
70
+
71
+ bgBlack: f("\x1b[40m", "\x1b[49m"),
72
+ bgRed: f("\x1b[41m", "\x1b[49m"),
73
+ bgGreen: f("\x1b[42m", "\x1b[49m"),
74
+ bgYellow: f("\x1b[43m", "\x1b[49m"),
75
+ bgBlue: f("\x1b[44m", "\x1b[49m"),
76
+ bgMagenta: f("\x1b[45m", "\x1b[49m"),
77
+ bgCyan: f("\x1b[46m", "\x1b[49m"),
78
+ bgWhite: f("\x1b[47m", "\x1b[49m"),
79
+
80
+ blackBright: f("\x1b[90m", "\x1b[39m"),
81
+ redBright: f("\x1b[91m", "\x1b[39m"),
82
+ greenBright: f("\x1b[92m", "\x1b[39m"),
83
+ yellowBright: f("\x1b[93m", "\x1b[39m"),
84
+ blueBright: f("\x1b[94m", "\x1b[39m"),
85
+ magentaBright: f("\x1b[95m", "\x1b[39m"),
86
+ cyanBright: f("\x1b[96m", "\x1b[39m"),
87
+ whiteBright: f("\x1b[97m", "\x1b[39m"),
88
+
89
+ bgBlackBright: f("\x1b[100m", "\x1b[49m"),
90
+ bgRedBright: f("\x1b[101m", "\x1b[49m"),
91
+ bgGreenBright: f("\x1b[102m", "\x1b[49m"),
92
+ bgYellowBright: f("\x1b[103m", "\x1b[49m"),
93
+ bgBlueBright: f("\x1b[104m", "\x1b[49m"),
94
+ bgMagentaBright: f("\x1b[105m", "\x1b[49m"),
95
+ bgCyanBright: f("\x1b[106m", "\x1b[49m"),
96
+ bgWhiteBright: f("\x1b[107m", "\x1b[49m"),
92
97
  }
93
98
  };
94
99
 
@@ -165,7 +170,7 @@ function joinUrlSegments(a, b) {
165
170
  }
166
171
  function toAssetPathFromHtml(filename, htmlPath, config) {
167
172
  const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
168
- const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
173
+ const toRelative = (filename2, _hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
169
174
  return toOutputFilePathInHtml(
170
175
  filename,
171
176
  "asset",
@@ -176,7 +181,7 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
176
181
  );
177
182
  }
178
183
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
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)));
184
+ const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
180
185
  const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
181
186
  const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
182
187
  function viteLegacyPlugin(options = {}) {
@@ -300,11 +305,13 @@ function viteLegacyPlugin(options = {}) {
300
305
  if (!modernPolyfills.size) {
301
306
  return;
302
307
  }
303
- isDebug && console.log(
304
- `[@vitejs/plugin-legacy] modern polyfills:`,
305
- modernPolyfills
306
- );
307
- const polyfillChunk = await buildPolyfillChunk(
308
+ if (isDebug) {
309
+ console.log(
310
+ `[@vitejs/plugin-legacy] modern polyfills:`,
311
+ modernPolyfills
312
+ );
313
+ }
314
+ await buildPolyfillChunk(
308
315
  config.mode,
309
316
  modernPolyfills,
310
317
  bundle,
@@ -312,11 +319,9 @@ function viteLegacyPlugin(options = {}) {
312
319
  config.build,
313
320
  "es",
314
321
  opts,
315
- true
322
+ true,
323
+ genLegacy
316
324
  );
317
- if (genLegacy && polyfillChunk) {
318
- polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code;
319
- }
320
325
  return;
321
326
  }
322
327
  if (!genLegacy) {
@@ -333,10 +338,12 @@ function viteLegacyPlugin(options = {}) {
333
338
  );
334
339
  }
335
340
  if (legacyPolyfills.size || !options.externalSystemJS) {
336
- isDebug && console.log(
337
- `[@vitejs/plugin-legacy] legacy polyfills:`,
338
- legacyPolyfills
339
- );
341
+ if (isDebug) {
342
+ console.log(
343
+ `[@vitejs/plugin-legacy] legacy polyfills:`,
344
+ legacyPolyfills
345
+ );
346
+ }
340
347
  await buildPolyfillChunk(
341
348
  config.mode,
342
349
  legacyPolyfills,
@@ -365,12 +372,16 @@ function viteLegacyPlugin(options = {}) {
365
372
  }
366
373
  config = _config;
367
374
  modernTargets = options.modernTargets || modernTargetsBabel;
368
- isDebug && console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
375
+ if (isDebug) {
376
+ console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
377
+ }
369
378
  if (!genLegacy || config.build.ssr) {
370
379
  return;
371
380
  }
372
381
  targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
373
- isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets);
382
+ if (isDebug) {
383
+ console.log(`[@vitejs/plugin-legacy] targets:`, targets);
384
+ }
374
385
  const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
375
386
  if (!fileNames) {
376
387
  return path__default.posix.join(config.build.assetsDir, defaultFileName);
@@ -474,7 +485,6 @@ function viteLegacyPlugin(options = {}) {
474
485
  compact: !!config.build.minify,
475
486
  sourceMaps,
476
487
  inputSourceMap: void 0,
477
- // sourceMaps ? chunk.map : undefined, `.map` TODO: moved to OutputChunk?
478
488
  presets: [
479
489
  // forcing our plugin to run before preset-env by wrapping it in a
480
490
  // preset so we can catch the injected import statements...
@@ -669,20 +679,24 @@ function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
669
679
  ignoreBrowserslistConfig: true
670
680
  };
671
681
  }
672
- async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
673
- let { minify, assetsDir } = buildOptions;
682
+ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
683
+ let { minify, assetsDir, sourcemap } = buildOptions;
674
684
  minify = minify ? "terser" : false;
675
685
  const res = await vite.build({
676
686
  mode,
677
687
  // so that everything is resolved from here
678
- root: path__default.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)))),
688
+ root: path__default.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)))),
679
689
  configFile: false,
680
690
  logLevel: "error",
681
- plugins: [polyfillsPlugin(imports, excludeSystemJS)],
691
+ plugins: [
692
+ polyfillsPlugin(imports, excludeSystemJS),
693
+ prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()
694
+ ],
682
695
  build: {
683
696
  write: false,
684
697
  minify,
685
698
  assetsDir,
699
+ sourcemap,
686
700
  rollupOptions: {
687
701
  input: {
688
702
  polyfills: polyfillId
@@ -709,7 +723,9 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
709
723
  const _polyfillChunk = Array.isArray(res) ? res[0] : res;
710
724
  if (!("output" in _polyfillChunk))
711
725
  return;
712
- const polyfillChunk = _polyfillChunk.output[0];
726
+ const polyfillChunk = _polyfillChunk.output.find(
727
+ (chunk) => chunk.type === "chunk" && chunk.isEntry
728
+ );
713
729
  for (const key in bundle) {
714
730
  const chunk = bundle[key];
715
731
  if (chunk.type === "chunk" && chunk.facadeModuleId) {
@@ -717,7 +733,14 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
717
733
  }
718
734
  }
719
735
  bundle[polyfillChunk.fileName] = polyfillChunk;
720
- return polyfillChunk;
736
+ if (polyfillChunk.sourcemapFileName) {
737
+ const polyfillChunkMapAsset = _polyfillChunk.output.find(
738
+ (chunk) => chunk.type === "asset" && chunk.fileName === polyfillChunk.sourcemapFileName
739
+ );
740
+ if (polyfillChunkMapAsset) {
741
+ bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset;
742
+ }
743
+ }
721
744
  }
722
745
  const polyfillId = "\0vite/legacy-polyfills";
723
746
  function polyfillsPlugin(imports, excludeSystemJS) {
@@ -735,6 +758,26 @@ function polyfillsPlugin(imports, excludeSystemJS) {
735
758
  }
736
759
  };
737
760
  }
761
+ function prependModenChunkLegacyGuardPlugin() {
762
+ let sourceMapEnabled;
763
+ return {
764
+ name: "vite:legacy-prepend-moden-chunk-legacy-guard",
765
+ configResolved(config) {
766
+ sourceMapEnabled = !!config.build.sourcemap;
767
+ },
768
+ renderChunk(code) {
769
+ if (!sourceMapEnabled) {
770
+ return modernChunkLegacyGuard + code;
771
+ }
772
+ const ms = new MagicString__default(code);
773
+ ms.prepend(modernChunkLegacyGuard);
774
+ return {
775
+ code: ms.toString(),
776
+ map: ms.generateMap({ hires: "boundary" })
777
+ };
778
+ }
779
+ };
780
+ }
738
781
  function isLegacyChunk(chunk, options) {
739
782
  return options.format === "system" && chunk.fileName.includes("-legacy");
740
783
  }
@@ -786,12 +829,16 @@ function wrapIIFEBabelPlugin() {
786
829
  };
787
830
  };
788
831
  }
832
+ const hash = (
833
+ // eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
834
+ crypto__default.hash ?? ((algorithm, data, outputEncoding) => crypto__default.createHash(algorithm).update(data).digest(outputEncoding))
835
+ );
789
836
  const cspHashes = [
790
837
  safari10NoModuleFix,
791
838
  systemJSInlineCode,
792
839
  detectModernBrowserCode,
793
840
  dynamicFallbackInlineCode
794
- ].map((i) => node_crypto.createHash("sha256").update(i).digest("base64"));
841
+ ].map((i) => hash("sha256", i, "base64"));
795
842
 
796
843
  module.exports = viteLegacyPlugin;
797
844
  module.exports.cspHashes = cspHashes;
package/dist/index.d.cts CHANGED
@@ -4,9 +4,7 @@ interface Options {
4
4
  /**
5
5
  * default: 'defaults'
6
6
  */
7
- targets?: string | string[] | {
8
- [key: string]: string;
9
- };
7
+ targets?: string | string[] | Record<string, string>;
10
8
  /**
11
9
  * default: 'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'
12
10
  */
package/dist/index.d.mts CHANGED
@@ -4,9 +4,7 @@ interface Options {
4
4
  /**
5
5
  * default: 'defaults'
6
6
  */
7
- targets?: string | string[] | {
8
- [key: string]: string;
9
- };
7
+ targets?: string | string[] | Record<string, string>;
10
8
  /**
11
9
  * default: 'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'
12
10
  */
package/dist/index.d.ts CHANGED
@@ -4,9 +4,7 @@ interface Options {
4
4
  /**
5
5
  * default: 'defaults'
6
6
  */
7
- targets?: string | string[] | {
8
- [key: string]: string;
9
- };
7
+ targets?: string | string[] | Record<string, string>;
10
8
  /**
11
9
  * default: 'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'
12
10
  */
package/dist/index.mjs CHANGED
@@ -1,45 +1,30 @@
1
1
  import path from 'node:path';
2
- import { createHash } from 'node:crypto';
2
+ import crypto from 'node:crypto';
3
3
  import { createRequire } from 'node:module';
4
4
  import { fileURLToPath } from 'node:url';
5
5
  import { build, normalizePath } from 'vite';
6
6
  import MagicString from 'magic-string';
7
- import require$$0 from 'tty';
8
7
  import browserslist from 'browserslist';
9
8
 
10
9
  function getDefaultExportFromCjs (x) {
11
10
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
12
11
  }
13
12
 
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
- }
17
-
18
13
  var picocolors = {exports: {}};
19
14
 
20
- let argv = process.argv || [],
21
- env = process.env;
15
+ let p = process || {}, argv = p.argv || [], env = p.env || {};
22
16
  let isColorSupported =
23
- !("NO_COLOR" in env || argv.includes("--no-color")) &&
24
- ("FORCE_COLOR" in env ||
25
- argv.includes("--color") ||
26
- process.platform === "win32" ||
27
- (commonjsRequire != null && require$$0.isatty(1) && env.TERM !== "dumb") ||
28
- "CI" in env);
17
+ !(!!env.NO_COLOR || argv.includes("--no-color")) &&
18
+ (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || ((p.stdout || {}).isTTY && env.TERM !== "dumb") || !!env.CI);
29
19
 
30
- let formatter =
31
- (open, close, replace = open) =>
20
+ let formatter = (open, close, replace = open) =>
32
21
  input => {
33
- let string = "" + input;
34
- let index = string.indexOf(close, open.length);
35
- return ~index
36
- ? open + replaceClose(string, close, replace, index) + close
37
- : open + string + close
22
+ let string = "" + input, index = string.indexOf(close, open.length);
23
+ return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close
38
24
  };
39
25
 
40
26
  let replaceClose = (string, close, replace, index) => {
41
- let result = "";
42
- let cursor = 0;
27
+ let result = "", cursor = 0;
43
28
  do {
44
29
  result += string.substring(cursor, index) + replace;
45
30
  cursor = index + close.length;
@@ -49,34 +34,54 @@ let replaceClose = (string, close, replace, index) => {
49
34
  };
50
35
 
51
36
  let createColors = (enabled = isColorSupported) => {
52
- let init = enabled ? formatter : () => String;
37
+ let f = enabled ? formatter : () => String;
53
38
  return {
54
39
  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"),
40
+ reset: f("\x1b[0m", "\x1b[0m"),
41
+ bold: f("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
42
+ dim: f("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
43
+ italic: f("\x1b[3m", "\x1b[23m"),
44
+ underline: f("\x1b[4m", "\x1b[24m"),
45
+ inverse: f("\x1b[7m", "\x1b[27m"),
46
+ hidden: f("\x1b[8m", "\x1b[28m"),
47
+ strikethrough: f("\x1b[9m", "\x1b[29m"),
48
+
49
+ black: f("\x1b[30m", "\x1b[39m"),
50
+ red: f("\x1b[31m", "\x1b[39m"),
51
+ green: f("\x1b[32m", "\x1b[39m"),
52
+ yellow: f("\x1b[33m", "\x1b[39m"),
53
+ blue: f("\x1b[34m", "\x1b[39m"),
54
+ magenta: f("\x1b[35m", "\x1b[39m"),
55
+ cyan: f("\x1b[36m", "\x1b[39m"),
56
+ white: f("\x1b[37m", "\x1b[39m"),
57
+ gray: f("\x1b[90m", "\x1b[39m"),
58
+
59
+ bgBlack: f("\x1b[40m", "\x1b[49m"),
60
+ bgRed: f("\x1b[41m", "\x1b[49m"),
61
+ bgGreen: f("\x1b[42m", "\x1b[49m"),
62
+ bgYellow: f("\x1b[43m", "\x1b[49m"),
63
+ bgBlue: f("\x1b[44m", "\x1b[49m"),
64
+ bgMagenta: f("\x1b[45m", "\x1b[49m"),
65
+ bgCyan: f("\x1b[46m", "\x1b[49m"),
66
+ bgWhite: f("\x1b[47m", "\x1b[49m"),
67
+
68
+ blackBright: f("\x1b[90m", "\x1b[39m"),
69
+ redBright: f("\x1b[91m", "\x1b[39m"),
70
+ greenBright: f("\x1b[92m", "\x1b[39m"),
71
+ yellowBright: f("\x1b[93m", "\x1b[39m"),
72
+ blueBright: f("\x1b[94m", "\x1b[39m"),
73
+ magentaBright: f("\x1b[95m", "\x1b[39m"),
74
+ cyanBright: f("\x1b[96m", "\x1b[39m"),
75
+ whiteBright: f("\x1b[97m", "\x1b[39m"),
76
+
77
+ bgBlackBright: f("\x1b[100m", "\x1b[49m"),
78
+ bgRedBright: f("\x1b[101m", "\x1b[49m"),
79
+ bgGreenBright: f("\x1b[102m", "\x1b[49m"),
80
+ bgYellowBright: f("\x1b[103m", "\x1b[49m"),
81
+ bgBlueBright: f("\x1b[104m", "\x1b[49m"),
82
+ bgMagentaBright: f("\x1b[105m", "\x1b[49m"),
83
+ bgCyanBright: f("\x1b[106m", "\x1b[49m"),
84
+ bgWhiteBright: f("\x1b[107m", "\x1b[49m"),
80
85
  }
81
86
  };
82
87
 
@@ -153,7 +158,7 @@ function joinUrlSegments(a, b) {
153
158
  }
154
159
  function toAssetPathFromHtml(filename, htmlPath, config) {
155
160
  const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
156
- const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
161
+ const toRelative = (filename2, _hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
157
162
  return toOutputFilePathInHtml(
158
163
  filename,
159
164
  "asset",
@@ -288,11 +293,13 @@ function viteLegacyPlugin(options = {}) {
288
293
  if (!modernPolyfills.size) {
289
294
  return;
290
295
  }
291
- isDebug && console.log(
292
- `[@vitejs/plugin-legacy] modern polyfills:`,
293
- modernPolyfills
294
- );
295
- const polyfillChunk = await buildPolyfillChunk(
296
+ if (isDebug) {
297
+ console.log(
298
+ `[@vitejs/plugin-legacy] modern polyfills:`,
299
+ modernPolyfills
300
+ );
301
+ }
302
+ await buildPolyfillChunk(
296
303
  config.mode,
297
304
  modernPolyfills,
298
305
  bundle,
@@ -300,11 +307,9 @@ function viteLegacyPlugin(options = {}) {
300
307
  config.build,
301
308
  "es",
302
309
  opts,
303
- true
310
+ true,
311
+ genLegacy
304
312
  );
305
- if (genLegacy && polyfillChunk) {
306
- polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code;
307
- }
308
313
  return;
309
314
  }
310
315
  if (!genLegacy) {
@@ -321,10 +326,12 @@ function viteLegacyPlugin(options = {}) {
321
326
  );
322
327
  }
323
328
  if (legacyPolyfills.size || !options.externalSystemJS) {
324
- isDebug && console.log(
325
- `[@vitejs/plugin-legacy] legacy polyfills:`,
326
- legacyPolyfills
327
- );
329
+ if (isDebug) {
330
+ console.log(
331
+ `[@vitejs/plugin-legacy] legacy polyfills:`,
332
+ legacyPolyfills
333
+ );
334
+ }
328
335
  await buildPolyfillChunk(
329
336
  config.mode,
330
337
  legacyPolyfills,
@@ -353,12 +360,16 @@ function viteLegacyPlugin(options = {}) {
353
360
  }
354
361
  config = _config;
355
362
  modernTargets = options.modernTargets || modernTargetsBabel;
356
- isDebug && console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
363
+ if (isDebug) {
364
+ console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
365
+ }
357
366
  if (!genLegacy || config.build.ssr) {
358
367
  return;
359
368
  }
360
369
  targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
361
- isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets);
370
+ if (isDebug) {
371
+ console.log(`[@vitejs/plugin-legacy] targets:`, targets);
372
+ }
362
373
  const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
363
374
  if (!fileNames) {
364
375
  return path.posix.join(config.build.assetsDir, defaultFileName);
@@ -462,7 +473,6 @@ function viteLegacyPlugin(options = {}) {
462
473
  compact: !!config.build.minify,
463
474
  sourceMaps,
464
475
  inputSourceMap: void 0,
465
- // sourceMaps ? chunk.map : undefined, `.map` TODO: moved to OutputChunk?
466
476
  presets: [
467
477
  // forcing our plugin to run before preset-env by wrapping it in a
468
478
  // preset so we can catch the injected import statements...
@@ -657,8 +667,8 @@ function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
657
667
  ignoreBrowserslistConfig: true
658
668
  };
659
669
  }
660
- async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
661
- let { minify, assetsDir } = buildOptions;
670
+ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
671
+ let { minify, assetsDir, sourcemap } = buildOptions;
662
672
  minify = minify ? "terser" : false;
663
673
  const res = await build({
664
674
  mode,
@@ -666,11 +676,15 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
666
676
  root: path.dirname(fileURLToPath(import.meta.url)),
667
677
  configFile: false,
668
678
  logLevel: "error",
669
- plugins: [polyfillsPlugin(imports, excludeSystemJS)],
679
+ plugins: [
680
+ polyfillsPlugin(imports, excludeSystemJS),
681
+ prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()
682
+ ],
670
683
  build: {
671
684
  write: false,
672
685
  minify,
673
686
  assetsDir,
687
+ sourcemap,
674
688
  rollupOptions: {
675
689
  input: {
676
690
  polyfills: polyfillId
@@ -697,7 +711,9 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
697
711
  const _polyfillChunk = Array.isArray(res) ? res[0] : res;
698
712
  if (!("output" in _polyfillChunk))
699
713
  return;
700
- const polyfillChunk = _polyfillChunk.output[0];
714
+ const polyfillChunk = _polyfillChunk.output.find(
715
+ (chunk) => chunk.type === "chunk" && chunk.isEntry
716
+ );
701
717
  for (const key in bundle) {
702
718
  const chunk = bundle[key];
703
719
  if (chunk.type === "chunk" && chunk.facadeModuleId) {
@@ -705,7 +721,14 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
705
721
  }
706
722
  }
707
723
  bundle[polyfillChunk.fileName] = polyfillChunk;
708
- return polyfillChunk;
724
+ if (polyfillChunk.sourcemapFileName) {
725
+ const polyfillChunkMapAsset = _polyfillChunk.output.find(
726
+ (chunk) => chunk.type === "asset" && chunk.fileName === polyfillChunk.sourcemapFileName
727
+ );
728
+ if (polyfillChunkMapAsset) {
729
+ bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset;
730
+ }
731
+ }
709
732
  }
710
733
  const polyfillId = "\0vite/legacy-polyfills";
711
734
  function polyfillsPlugin(imports, excludeSystemJS) {
@@ -723,6 +746,26 @@ function polyfillsPlugin(imports, excludeSystemJS) {
723
746
  }
724
747
  };
725
748
  }
749
+ function prependModenChunkLegacyGuardPlugin() {
750
+ let sourceMapEnabled;
751
+ return {
752
+ name: "vite:legacy-prepend-moden-chunk-legacy-guard",
753
+ configResolved(config) {
754
+ sourceMapEnabled = !!config.build.sourcemap;
755
+ },
756
+ renderChunk(code) {
757
+ if (!sourceMapEnabled) {
758
+ return modernChunkLegacyGuard + code;
759
+ }
760
+ const ms = new MagicString(code);
761
+ ms.prepend(modernChunkLegacyGuard);
762
+ return {
763
+ code: ms.toString(),
764
+ map: ms.generateMap({ hires: "boundary" })
765
+ };
766
+ }
767
+ };
768
+ }
726
769
  function isLegacyChunk(chunk, options) {
727
770
  return options.format === "system" && chunk.fileName.includes("-legacy");
728
771
  }
@@ -774,11 +817,15 @@ function wrapIIFEBabelPlugin() {
774
817
  };
775
818
  };
776
819
  }
820
+ const hash = (
821
+ // eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
822
+ crypto.hash ?? ((algorithm, data, outputEncoding) => crypto.createHash(algorithm).update(data).digest(outputEncoding))
823
+ );
777
824
  const cspHashes = [
778
825
  safari10NoModuleFix,
779
826
  systemJSInlineCode,
780
827
  detectModernBrowserCode,
781
828
  dynamicFallbackInlineCode
782
- ].map((i) => createHash("sha256").update(i).digest("base64"));
829
+ ].map((i) => hash("sha256", i, "base64"));
783
830
 
784
831
  export { cspHashes, viteLegacyPlugin as default, detectPolyfills };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "5.4.2",
3
+ "version": "5.4.3",
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.25.2",
39
- "@babel/preset-env": "^7.25.3",
40
- "browserslist": "^4.23.3",
38
+ "@babel/core": "^7.25.8",
39
+ "@babel/preset-env": "^7.25.8",
40
+ "browserslist": "^4.24.0",
41
41
  "browserslist-to-esbuild": "^2.1.1",
42
- "core-js": "^3.38.0",
43
- "magic-string": "^0.30.11",
42
+ "core-js": "^3.38.1",
43
+ "magic-string": "^0.30.12",
44
44
  "regenerator-runtime": "^0.14.1",
45
45
  "systemjs": "^6.15.1"
46
46
  },
@@ -49,10 +49,10 @@
49
49
  "vite": "^5.0.0"
50
50
  },
51
51
  "devDependencies": {
52
- "acorn": "^8.12.1",
53
- "picocolors": "^1.0.1",
52
+ "acorn": "^8.13.0",
53
+ "picocolors": "^1.1.1",
54
54
  "unbuild": "^2.0.0",
55
- "vite": "5.4.1"
55
+ "vite": "6.0.0-beta.5"
56
56
  },
57
57
  "scripts": {
58
58
  "dev": "unbuild --stub",