@vitejs/plugin-legacy 5.4.1 → 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/README.md CHANGED
@@ -91,7 +91,7 @@ npm add -D terser
91
91
 
92
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 setting `modernTargets` or 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
@@ -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
 
@@ -142,7 +147,7 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
142
147
  if (relative && !config.build.ssr) {
143
148
  return toRelative(filename, hostId);
144
149
  } else {
145
- return config.base + filename;
150
+ return joinUrlSegments(config.decodedBase, filename);
146
151
  }
147
152
  }
148
153
  function getBaseInHTML(urlRelativePath, config) {
@@ -151,9 +156,21 @@ function getBaseInHTML(urlRelativePath, config) {
151
156
  "./"
152
157
  ) : config.base;
153
158
  }
159
+ function joinUrlSegments(a, b) {
160
+ if (!a || !b) {
161
+ return a || b || "";
162
+ }
163
+ if (a[a.length - 1] === "/") {
164
+ a = a.substring(0, a.length - 1);
165
+ }
166
+ if (b[0] !== "/") {
167
+ b = "/" + b;
168
+ }
169
+ return a + b;
170
+ }
154
171
  function toAssetPathFromHtml(filename, htmlPath, config) {
155
172
  const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
156
- const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
173
+ const toRelative = (filename2, _hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
157
174
  return toOutputFilePathInHtml(
158
175
  filename,
159
176
  "asset",
@@ -164,9 +181,9 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
164
181
  );
165
182
  }
166
183
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
167
- 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)));
168
185
  const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
169
- const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/;
186
+ const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
170
187
  function viteLegacyPlugin(options = {}) {
171
188
  let config;
172
189
  let targets;
@@ -288,11 +305,13 @@ function viteLegacyPlugin(options = {}) {
288
305
  if (!modernPolyfills.size) {
289
306
  return;
290
307
  }
291
- isDebug && console.log(
292
- `[@vitejs/plugin-legacy] modern polyfills:`,
293
- modernPolyfills
294
- );
295
- const polyfillChunk = await buildPolyfillChunk(
308
+ if (isDebug) {
309
+ console.log(
310
+ `[@vitejs/plugin-legacy] modern polyfills:`,
311
+ modernPolyfills
312
+ );
313
+ }
314
+ await buildPolyfillChunk(
296
315
  config.mode,
297
316
  modernPolyfills,
298
317
  bundle,
@@ -300,11 +319,9 @@ function viteLegacyPlugin(options = {}) {
300
319
  config.build,
301
320
  "es",
302
321
  opts,
303
- true
322
+ true,
323
+ genLegacy
304
324
  );
305
- if (genLegacy && polyfillChunk) {
306
- polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code;
307
- }
308
325
  return;
309
326
  }
310
327
  if (!genLegacy) {
@@ -321,10 +338,12 @@ function viteLegacyPlugin(options = {}) {
321
338
  );
322
339
  }
323
340
  if (legacyPolyfills.size || !options.externalSystemJS) {
324
- isDebug && console.log(
325
- `[@vitejs/plugin-legacy] legacy polyfills:`,
326
- legacyPolyfills
327
- );
341
+ if (isDebug) {
342
+ console.log(
343
+ `[@vitejs/plugin-legacy] legacy polyfills:`,
344
+ legacyPolyfills
345
+ );
346
+ }
328
347
  await buildPolyfillChunk(
329
348
  config.mode,
330
349
  legacyPolyfills,
@@ -353,12 +372,16 @@ function viteLegacyPlugin(options = {}) {
353
372
  }
354
373
  config = _config;
355
374
  modernTargets = options.modernTargets || modernTargetsBabel;
356
- isDebug && console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
375
+ if (isDebug) {
376
+ console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
377
+ }
357
378
  if (!genLegacy || config.build.ssr) {
358
379
  return;
359
380
  }
360
381
  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);
382
+ if (isDebug) {
383
+ console.log(`[@vitejs/plugin-legacy] targets:`, targets);
384
+ }
362
385
  const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
363
386
  if (!fileNames) {
364
387
  return path__default.posix.join(config.build.assetsDir, defaultFileName);
@@ -462,7 +485,6 @@ function viteLegacyPlugin(options = {}) {
462
485
  compact: !!config.build.minify,
463
486
  sourceMaps,
464
487
  inputSourceMap: void 0,
465
- // sourceMaps ? chunk.map : undefined, `.map` TODO: moved to OutputChunk?
466
488
  presets: [
467
489
  // forcing our plugin to run before preset-env by wrapping it in a
468
490
  // preset so we can catch the injected import statements...
@@ -657,20 +679,24 @@ function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
657
679
  ignoreBrowserslistConfig: true
658
680
  };
659
681
  }
660
- async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
661
- let { minify, assetsDir } = buildOptions;
682
+ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
683
+ let { minify, assetsDir, sourcemap } = buildOptions;
662
684
  minify = minify ? "terser" : false;
663
685
  const res = await vite.build({
664
686
  mode,
665
687
  // so that everything is resolved from here
666
- 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)))),
667
689
  configFile: false,
668
690
  logLevel: "error",
669
- plugins: [polyfillsPlugin(imports, excludeSystemJS)],
691
+ plugins: [
692
+ polyfillsPlugin(imports, excludeSystemJS),
693
+ prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()
694
+ ],
670
695
  build: {
671
696
  write: false,
672
697
  minify,
673
698
  assetsDir,
699
+ sourcemap,
674
700
  rollupOptions: {
675
701
  input: {
676
702
  polyfills: polyfillId
@@ -697,7 +723,9 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
697
723
  const _polyfillChunk = Array.isArray(res) ? res[0] : res;
698
724
  if (!("output" in _polyfillChunk))
699
725
  return;
700
- const polyfillChunk = _polyfillChunk.output[0];
726
+ const polyfillChunk = _polyfillChunk.output.find(
727
+ (chunk) => chunk.type === "chunk" && chunk.isEntry
728
+ );
701
729
  for (const key in bundle) {
702
730
  const chunk = bundle[key];
703
731
  if (chunk.type === "chunk" && chunk.facadeModuleId) {
@@ -705,7 +733,14 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
705
733
  }
706
734
  }
707
735
  bundle[polyfillChunk.fileName] = polyfillChunk;
708
- 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
+ }
709
744
  }
710
745
  const polyfillId = "\0vite/legacy-polyfills";
711
746
  function polyfillsPlugin(imports, excludeSystemJS) {
@@ -723,6 +758,26 @@ function polyfillsPlugin(imports, excludeSystemJS) {
723
758
  }
724
759
  };
725
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
+ }
726
781
  function isLegacyChunk(chunk, options) {
727
782
  return options.format === "system" && chunk.fileName.includes("-legacy");
728
783
  }
@@ -774,12 +829,16 @@ function wrapIIFEBabelPlugin() {
774
829
  };
775
830
  };
776
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
+ );
777
836
  const cspHashes = [
778
837
  safari10NoModuleFix,
779
838
  systemJSInlineCode,
780
839
  detectModernBrowserCode,
781
840
  dynamicFallbackInlineCode
782
- ].map((i) => node_crypto.createHash("sha256").update(i).digest("base64"));
841
+ ].map((i) => hash("sha256", i, "base64"));
783
842
 
784
843
  module.exports = viteLegacyPlugin;
785
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
 
@@ -130,7 +135,7 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
130
135
  if (relative && !config.build.ssr) {
131
136
  return toRelative(filename, hostId);
132
137
  } else {
133
- return config.base + filename;
138
+ return joinUrlSegments(config.decodedBase, filename);
134
139
  }
135
140
  }
136
141
  function getBaseInHTML(urlRelativePath, config) {
@@ -139,9 +144,21 @@ function getBaseInHTML(urlRelativePath, config) {
139
144
  "./"
140
145
  ) : config.base;
141
146
  }
147
+ function joinUrlSegments(a, b) {
148
+ if (!a || !b) {
149
+ return a || b || "";
150
+ }
151
+ if (a[a.length - 1] === "/") {
152
+ a = a.substring(0, a.length - 1);
153
+ }
154
+ if (b[0] !== "/") {
155
+ b = "/" + b;
156
+ }
157
+ return a + b;
158
+ }
142
159
  function toAssetPathFromHtml(filename, htmlPath, config) {
143
160
  const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
144
- const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
161
+ const toRelative = (filename2, _hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
145
162
  return toOutputFilePathInHtml(
146
163
  filename,
147
164
  "asset",
@@ -154,7 +171,7 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
154
171
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
155
172
  const _require = createRequire(import.meta.url);
156
173
  const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
157
- const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/;
174
+ const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
158
175
  function viteLegacyPlugin(options = {}) {
159
176
  let config;
160
177
  let targets;
@@ -276,11 +293,13 @@ function viteLegacyPlugin(options = {}) {
276
293
  if (!modernPolyfills.size) {
277
294
  return;
278
295
  }
279
- isDebug && console.log(
280
- `[@vitejs/plugin-legacy] modern polyfills:`,
281
- modernPolyfills
282
- );
283
- const polyfillChunk = await buildPolyfillChunk(
296
+ if (isDebug) {
297
+ console.log(
298
+ `[@vitejs/plugin-legacy] modern polyfills:`,
299
+ modernPolyfills
300
+ );
301
+ }
302
+ await buildPolyfillChunk(
284
303
  config.mode,
285
304
  modernPolyfills,
286
305
  bundle,
@@ -288,11 +307,9 @@ function viteLegacyPlugin(options = {}) {
288
307
  config.build,
289
308
  "es",
290
309
  opts,
291
- true
310
+ true,
311
+ genLegacy
292
312
  );
293
- if (genLegacy && polyfillChunk) {
294
- polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code;
295
- }
296
313
  return;
297
314
  }
298
315
  if (!genLegacy) {
@@ -309,10 +326,12 @@ function viteLegacyPlugin(options = {}) {
309
326
  );
310
327
  }
311
328
  if (legacyPolyfills.size || !options.externalSystemJS) {
312
- isDebug && console.log(
313
- `[@vitejs/plugin-legacy] legacy polyfills:`,
314
- legacyPolyfills
315
- );
329
+ if (isDebug) {
330
+ console.log(
331
+ `[@vitejs/plugin-legacy] legacy polyfills:`,
332
+ legacyPolyfills
333
+ );
334
+ }
316
335
  await buildPolyfillChunk(
317
336
  config.mode,
318
337
  legacyPolyfills,
@@ -341,12 +360,16 @@ function viteLegacyPlugin(options = {}) {
341
360
  }
342
361
  config = _config;
343
362
  modernTargets = options.modernTargets || modernTargetsBabel;
344
- isDebug && console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
363
+ if (isDebug) {
364
+ console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
365
+ }
345
366
  if (!genLegacy || config.build.ssr) {
346
367
  return;
347
368
  }
348
369
  targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
349
- isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets);
370
+ if (isDebug) {
371
+ console.log(`[@vitejs/plugin-legacy] targets:`, targets);
372
+ }
350
373
  const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
351
374
  if (!fileNames) {
352
375
  return path.posix.join(config.build.assetsDir, defaultFileName);
@@ -450,7 +473,6 @@ function viteLegacyPlugin(options = {}) {
450
473
  compact: !!config.build.minify,
451
474
  sourceMaps,
452
475
  inputSourceMap: void 0,
453
- // sourceMaps ? chunk.map : undefined, `.map` TODO: moved to OutputChunk?
454
476
  presets: [
455
477
  // forcing our plugin to run before preset-env by wrapping it in a
456
478
  // preset so we can catch the injected import statements...
@@ -645,8 +667,8 @@ function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
645
667
  ignoreBrowserslistConfig: true
646
668
  };
647
669
  }
648
- async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
649
- let { minify, assetsDir } = buildOptions;
670
+ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
671
+ let { minify, assetsDir, sourcemap } = buildOptions;
650
672
  minify = minify ? "terser" : false;
651
673
  const res = await build({
652
674
  mode,
@@ -654,11 +676,15 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
654
676
  root: path.dirname(fileURLToPath(import.meta.url)),
655
677
  configFile: false,
656
678
  logLevel: "error",
657
- plugins: [polyfillsPlugin(imports, excludeSystemJS)],
679
+ plugins: [
680
+ polyfillsPlugin(imports, excludeSystemJS),
681
+ prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()
682
+ ],
658
683
  build: {
659
684
  write: false,
660
685
  minify,
661
686
  assetsDir,
687
+ sourcemap,
662
688
  rollupOptions: {
663
689
  input: {
664
690
  polyfills: polyfillId
@@ -685,7 +711,9 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
685
711
  const _polyfillChunk = Array.isArray(res) ? res[0] : res;
686
712
  if (!("output" in _polyfillChunk))
687
713
  return;
688
- const polyfillChunk = _polyfillChunk.output[0];
714
+ const polyfillChunk = _polyfillChunk.output.find(
715
+ (chunk) => chunk.type === "chunk" && chunk.isEntry
716
+ );
689
717
  for (const key in bundle) {
690
718
  const chunk = bundle[key];
691
719
  if (chunk.type === "chunk" && chunk.facadeModuleId) {
@@ -693,7 +721,14 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
693
721
  }
694
722
  }
695
723
  bundle[polyfillChunk.fileName] = polyfillChunk;
696
- 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
+ }
697
732
  }
698
733
  const polyfillId = "\0vite/legacy-polyfills";
699
734
  function polyfillsPlugin(imports, excludeSystemJS) {
@@ -711,6 +746,26 @@ function polyfillsPlugin(imports, excludeSystemJS) {
711
746
  }
712
747
  };
713
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
+ }
714
769
  function isLegacyChunk(chunk, options) {
715
770
  return options.format === "system" && chunk.fileName.includes("-legacy");
716
771
  }
@@ -762,11 +817,15 @@ function wrapIIFEBabelPlugin() {
762
817
  };
763
818
  };
764
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
+ );
765
824
  const cspHashes = [
766
825
  safari10NoModuleFix,
767
826
  systemJSInlineCode,
768
827
  detectModernBrowserCode,
769
828
  dynamicFallbackInlineCode
770
- ].map((i) => createHash("sha256").update(i).digest("base64"));
829
+ ].map((i) => hash("sha256", i, "base64"));
771
830
 
772
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.1",
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.24.6",
39
- "@babel/preset-env": "^7.24.6",
40
- "browserslist": "^4.23.0",
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.37.1",
43
- "magic-string": "^0.30.10",
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.11.3",
53
- "picocolors": "^1.0.1",
52
+ "acorn": "^8.13.0",
53
+ "picocolors": "^1.1.1",
54
54
  "unbuild": "^2.0.0",
55
- "vite": "5.2.12"
55
+ "vite": "6.0.0-beta.5"
56
56
  },
57
57
  "scripts": {
58
58
  "dev": "unbuild --stub",