@vitejs/plugin-legacy 7.1.0 → 7.2.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 +4 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -18
- package/dist/vendor/rolldown-vite/index.js +19 -18
- package/dist/vendor/rolldown-vite/vendor/rolldown-vite/index.js +607 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ npm add -D terser
|
|
|
40
40
|
- **Type:** `string | string[] | { [key: string]: string }`
|
|
41
41
|
- **Default:** [`'last 2 versions and not dead, > 0.3%, Firefox ESR'`](https://browsersl.ist/#q=last+2+versions+and+not+dead%2C+%3E+0.3%25%2C+Firefox+ESR)
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
It's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets) when rendering **legacy chunks**.
|
|
44
44
|
|
|
45
45
|
The query is also [Browserslist compatible](https://github.com/browserslist/browserslist). See [Browserslist Best Practices](https://github.com/browserslist/browserslist#best-practices) for more details.
|
|
46
46
|
|
|
@@ -51,12 +51,14 @@ npm add -D terser
|
|
|
51
51
|
- **Type:** `string | string[]`
|
|
52
52
|
- **Default:** [`'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'`](https://browsersl.ist/#q=edge%3E%3D79%2C+firefox%3E%3D67%2C+chrome%3E%3D64%2C+safari%3E%3D12%2C+chromeAndroid%3E%3D64%2C+iOS%3E%3D12)
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
It's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets) when collecting polyfills for **modern chunks**. The value set here will override the `build.target` option.
|
|
55
55
|
|
|
56
56
|
The query is also [Browserslist compatible](https://github.com/browserslist/browserslist). See [Browserslist Best Practices](https://github.com/browserslist/browserslist#best-practices) for more details.
|
|
57
57
|
|
|
58
58
|
If it's not set, plugin-legacy will fallback to the default value.
|
|
59
59
|
|
|
60
|
+
Note that this options should not be set unless `renderLegacyChunks` is set to `false`.
|
|
61
|
+
|
|
60
62
|
### `polyfills`
|
|
61
63
|
|
|
62
64
|
- **Type:** `boolean | string[]`
|
package/dist/index.d.ts
CHANGED
|
@@ -46,4 +46,4 @@ declare function detectPolyfills(code: string, targets: any, assumptions: Record
|
|
|
46
46
|
declare const cspHashes: string[];
|
|
47
47
|
declare function viteLegacyPluginCjs(this: unknown, options: Options): Plugin[];
|
|
48
48
|
//#endregion
|
|
49
|
-
export { Options, cspHashes, viteLegacyPlugin as default, detectPolyfills, viteLegacyPluginCjs as "module.exports" };
|
|
49
|
+
export { type Options, cspHashes, viteLegacyPlugin as default, detectPolyfills, viteLegacyPluginCjs as "module.exports" };
|
package/dist/index.js
CHANGED
|
@@ -34,7 +34,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
34
34
|
|
|
35
35
|
//#endregion
|
|
36
36
|
//#region ../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js
|
|
37
|
-
var require_picocolors = __commonJS({ "../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js"(exports, module) {
|
|
37
|
+
var require_picocolors = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js": ((exports, module) => {
|
|
38
38
|
let p = process || {}, argv = p.argv || [], env = p.env || {};
|
|
39
39
|
let isColorSupported = !(!!env.NO_COLOR || argv.includes("--no-color")) && (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || (p.stdout || {}).isTTY && env.TERM !== "dumb" || !!env.CI);
|
|
40
40
|
let formatter = (open, close, replace = open) => (input) => {
|
|
@@ -99,7 +99,7 @@ var require_picocolors = __commonJS({ "../../node_modules/.pnpm/picocolors@1.1.1
|
|
|
99
99
|
};
|
|
100
100
|
module.exports = createColors();
|
|
101
101
|
module.exports.createColors = createColors;
|
|
102
|
-
} });
|
|
102
|
+
}) });
|
|
103
103
|
|
|
104
104
|
//#endregion
|
|
105
105
|
//#region src/snippets.ts
|
|
@@ -115,7 +115,7 @@ const modernChunkLegacyGuard = `export function __vite_legacy_guard(){${detectMo
|
|
|
115
115
|
|
|
116
116
|
//#endregion
|
|
117
117
|
//#region src/index.ts
|
|
118
|
-
var import_picocolors = __toESM(require_picocolors(), 1);
|
|
118
|
+
var import_picocolors = /* @__PURE__ */ __toESM(require_picocolors(), 1);
|
|
119
119
|
let babel;
|
|
120
120
|
async function loadBabel() {
|
|
121
121
|
return babel ??= import("@babel/core");
|
|
@@ -157,6 +157,14 @@ const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
|
157
157
|
const _require = createRequire(import.meta.url);
|
|
158
158
|
const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
|
|
159
159
|
const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
|
|
160
|
+
const modernTargetsEsbuild = [
|
|
161
|
+
"es2020",
|
|
162
|
+
"edge79",
|
|
163
|
+
"firefox67",
|
|
164
|
+
"chrome64",
|
|
165
|
+
"safari12"
|
|
166
|
+
];
|
|
167
|
+
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
160
168
|
function viteLegacyPlugin(options = {}) {
|
|
161
169
|
if ("rolldownVersion" in vite) {
|
|
162
170
|
const { default: viteLegacyPluginForRolldownVite } = _require("#legacy-for-rolldown-vite");
|
|
@@ -164,15 +172,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
164
172
|
}
|
|
165
173
|
let config;
|
|
166
174
|
let targets;
|
|
167
|
-
|
|
168
|
-
const modernTargetsEsbuild = [
|
|
169
|
-
"es2020",
|
|
170
|
-
"edge79",
|
|
171
|
-
"firefox67",
|
|
172
|
-
"chrome64",
|
|
173
|
-
"safari12"
|
|
174
|
-
];
|
|
175
|
-
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
175
|
+
const modernTargets = options.modernTargets || modernTargetsBabel;
|
|
176
176
|
const genLegacy = options.renderLegacyChunks !== false;
|
|
177
177
|
const genModern = options.renderModernChunks !== false;
|
|
178
178
|
if (!genLegacy && !genModern) throw new Error("`renderLegacyChunks` and `renderModernChunks` cannot be both false");
|
|
@@ -199,6 +199,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
199
199
|
legacyPolyfills.add(i);
|
|
200
200
|
});
|
|
201
201
|
let overriddenBuildTarget = false;
|
|
202
|
+
let overriddenBuildTargetOnlyModern = false;
|
|
202
203
|
let overriddenDefaultModernTargets = false;
|
|
203
204
|
const legacyConfigPlugin = {
|
|
204
205
|
name: "vite:legacy-config",
|
|
@@ -209,16 +210,17 @@ function viteLegacyPlugin(options = {}) {
|
|
|
209
210
|
if (genLegacy) {
|
|
210
211
|
overriddenBuildTarget = config$1.build.target !== void 0;
|
|
211
212
|
overriddenDefaultModernTargets = options.modernTargets !== void 0;
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
213
|
+
} else overriddenBuildTargetOnlyModern = config$1.build.target !== void 0;
|
|
214
|
+
if (options.modernTargets) {
|
|
215
|
+
const { default: browserslistToEsbuild } = await import("browserslist-to-esbuild");
|
|
216
|
+
config$1.build.target = browserslistToEsbuild(options.modernTargets);
|
|
217
|
+
} else config$1.build.target = modernTargetsEsbuild;
|
|
217
218
|
}
|
|
218
219
|
return { define: { "import.meta.env.LEGACY": env$1.command === "serve" || config$1.build?.ssr ? false : legacyEnvVarMarker } };
|
|
219
220
|
},
|
|
220
221
|
configResolved(config$1) {
|
|
221
222
|
if (overriddenBuildTarget) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy overrode 'build.target'. You should pass 'targets' as an option to this plugin with the list of legacy browsers to support instead.`));
|
|
223
|
+
if (overriddenBuildTargetOnlyModern) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy overrode 'build.target'. You should pass 'modernTargets' as an option to this plugin with the list of browsers to support instead.`));
|
|
222
224
|
if (overriddenDefaultModernTargets) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy 'modernTargets' option overrode the builtin targets of modern chunks. Some versions of browsers between legacy and modern may not be supported.`));
|
|
223
225
|
if (config$1.isWorker) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy should not be passed to 'worker.plugins'. Pass to 'plugins' instead. Note that generating legacy chunks for workers are not supported by plugin-legacy.`));
|
|
224
226
|
}
|
|
@@ -256,7 +258,6 @@ function viteLegacyPlugin(options = {}) {
|
|
|
256
258
|
configResolved(_config) {
|
|
257
259
|
if (_config.build.lib) throw new Error("@vitejs/plugin-legacy does not support library mode.");
|
|
258
260
|
config = _config;
|
|
259
|
-
modernTargets = options.modernTargets || modernTargetsBabel;
|
|
260
261
|
if (isDebug) console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
|
|
261
262
|
if (!genLegacy || config.build.ssr) return;
|
|
262
263
|
targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
|
|
@@ -33,7 +33,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
33
33
|
|
|
34
34
|
//#endregion
|
|
35
35
|
//#region ../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js
|
|
36
|
-
var require_picocolors = __commonJS({ "../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js"(exports, module) {
|
|
36
|
+
var require_picocolors = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js": ((exports, module) => {
|
|
37
37
|
let p = process || {}, argv = p.argv || [], env = p.env || {};
|
|
38
38
|
let isColorSupported = !(!!env.NO_COLOR || argv.includes("--no-color")) && (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || (p.stdout || {}).isTTY && env.TERM !== "dumb" || !!env.CI);
|
|
39
39
|
let formatter = (open, close, replace = open) => (input) => {
|
|
@@ -98,7 +98,7 @@ var require_picocolors = __commonJS({ "../../node_modules/.pnpm/picocolors@1.1.1
|
|
|
98
98
|
};
|
|
99
99
|
module.exports = createColors();
|
|
100
100
|
module.exports.createColors = createColors;
|
|
101
|
-
} });
|
|
101
|
+
}) });
|
|
102
102
|
|
|
103
103
|
//#endregion
|
|
104
104
|
//#region src/snippets.ts
|
|
@@ -114,7 +114,7 @@ const modernChunkLegacyGuard = `export function __vite_legacy_guard(){${detectMo
|
|
|
114
114
|
|
|
115
115
|
//#endregion
|
|
116
116
|
//#region src/index.ts
|
|
117
|
-
var import_picocolors = __toESM(require_picocolors(), 1);
|
|
117
|
+
var import_picocolors = /* @__PURE__ */ __toESM(require_picocolors(), 1);
|
|
118
118
|
let babel;
|
|
119
119
|
async function loadBabel() {
|
|
120
120
|
return babel ??= import("@babel/core");
|
|
@@ -156,19 +156,19 @@ const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
|
156
156
|
const _require = createRequire(import.meta.url);
|
|
157
157
|
const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
|
|
158
158
|
const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
|
|
159
|
+
const modernTargetsEsbuild = [
|
|
160
|
+
"es2020",
|
|
161
|
+
"edge79",
|
|
162
|
+
"firefox67",
|
|
163
|
+
"chrome64",
|
|
164
|
+
"safari12"
|
|
165
|
+
];
|
|
166
|
+
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
159
167
|
const outputOptionsForLegacyChunks = /* @__PURE__ */ new WeakSet();
|
|
160
168
|
function viteLegacyPlugin(options = {}) {
|
|
161
169
|
let config;
|
|
162
170
|
let targets;
|
|
163
|
-
|
|
164
|
-
const modernTargetsEsbuild = [
|
|
165
|
-
"es2020",
|
|
166
|
-
"edge79",
|
|
167
|
-
"firefox67",
|
|
168
|
-
"chrome64",
|
|
169
|
-
"safari12"
|
|
170
|
-
];
|
|
171
|
-
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
171
|
+
const modernTargets = options.modernTargets || modernTargetsBabel;
|
|
172
172
|
const genLegacy = options.renderLegacyChunks !== false;
|
|
173
173
|
const genModern = options.renderModernChunks !== false;
|
|
174
174
|
if (!genLegacy && !genModern) throw new Error("`renderLegacyChunks` and `renderModernChunks` cannot be both false");
|
|
@@ -195,6 +195,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
195
195
|
legacyPolyfills.add(i);
|
|
196
196
|
});
|
|
197
197
|
let overriddenBuildTarget = false;
|
|
198
|
+
let overriddenBuildTargetOnlyModern = false;
|
|
198
199
|
let overriddenDefaultModernTargets = false;
|
|
199
200
|
const legacyConfigPlugin = {
|
|
200
201
|
name: "vite:legacy-config",
|
|
@@ -205,16 +206,17 @@ function viteLegacyPlugin(options = {}) {
|
|
|
205
206
|
if (genLegacy) {
|
|
206
207
|
overriddenBuildTarget = config$1.build.target !== void 0;
|
|
207
208
|
overriddenDefaultModernTargets = options.modernTargets !== void 0;
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
209
|
+
} else overriddenBuildTargetOnlyModern = config$1.build.target !== void 0;
|
|
210
|
+
if (options.modernTargets) {
|
|
211
|
+
const { default: browserslistToEsbuild } = await import("browserslist-to-esbuild");
|
|
212
|
+
config$1.build.target = browserslistToEsbuild(options.modernTargets);
|
|
213
|
+
} else config$1.build.target = modernTargetsEsbuild;
|
|
213
214
|
}
|
|
214
215
|
return { define: { "import.meta.env.LEGACY": env$1.command === "serve" || config$1.build?.ssr ? false : legacyEnvVarMarker } };
|
|
215
216
|
},
|
|
216
217
|
configResolved(config$1) {
|
|
217
218
|
if (overriddenBuildTarget) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy overrode 'build.target'. You should pass 'targets' as an option to this plugin with the list of legacy browsers to support instead.`));
|
|
219
|
+
if (overriddenBuildTargetOnlyModern) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy overrode 'build.target'. You should pass 'modernTargets' as an option to this plugin with the list of browsers to support instead.`));
|
|
218
220
|
if (overriddenDefaultModernTargets) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy 'modernTargets' option overrode the builtin targets of modern chunks. Some versions of browsers between legacy and modern may not be supported.`));
|
|
219
221
|
if (config$1.isWorker) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy should not be passed to 'worker.plugins'. Pass to 'plugins' instead. Note that generating legacy chunks for workers are not supported by plugin-legacy.`));
|
|
220
222
|
}
|
|
@@ -252,7 +254,6 @@ function viteLegacyPlugin(options = {}) {
|
|
|
252
254
|
configResolved(_config) {
|
|
253
255
|
if (_config.build.lib) throw new Error("@vitejs/plugin-legacy does not support library mode.");
|
|
254
256
|
config = _config;
|
|
255
|
-
modernTargets = options.modernTargets || modernTargetsBabel;
|
|
256
257
|
if (isDebug) console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
|
|
257
258
|
if (!genLegacy || config.build.ssr) return;
|
|
258
259
|
targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
|
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { build, normalizePath } from "vite";
|
|
6
|
+
import MagicString from "magic-string";
|
|
7
|
+
import browserslist from "browserslist";
|
|
8
|
+
|
|
9
|
+
//#region rolldown:runtime
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __commonJS = (cb, mod) => function() {
|
|
17
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
21
|
+
key = keys[i];
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
23
|
+
get: ((k) => from[k]).bind(null, key),
|
|
24
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
30
|
+
value: mod,
|
|
31
|
+
enumerable: true
|
|
32
|
+
}) : target, mod));
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region ../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js
|
|
36
|
+
var require_picocolors = __commonJS({ "../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js"(exports, module) {
|
|
37
|
+
let p = process || {}, argv = p.argv || [], env = p.env || {};
|
|
38
|
+
let isColorSupported = !(!!env.NO_COLOR || argv.includes("--no-color")) && (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || (p.stdout || {}).isTTY && env.TERM !== "dumb" || !!env.CI);
|
|
39
|
+
let formatter = (open, close, replace = open) => (input) => {
|
|
40
|
+
let string = "" + input, index = string.indexOf(close, open.length);
|
|
41
|
+
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
|
|
42
|
+
};
|
|
43
|
+
let replaceClose = (string, close, replace, index) => {
|
|
44
|
+
let result = "", cursor = 0;
|
|
45
|
+
do {
|
|
46
|
+
result += string.substring(cursor, index) + replace;
|
|
47
|
+
cursor = index + close.length;
|
|
48
|
+
index = string.indexOf(close, cursor);
|
|
49
|
+
} while (~index);
|
|
50
|
+
return result + string.substring(cursor);
|
|
51
|
+
};
|
|
52
|
+
let createColors = (enabled = isColorSupported) => {
|
|
53
|
+
let f = enabled ? formatter : () => String;
|
|
54
|
+
return {
|
|
55
|
+
isColorSupported: enabled,
|
|
56
|
+
reset: f("\x1B[0m", "\x1B[0m"),
|
|
57
|
+
bold: f("\x1B[1m", "\x1B[22m", "\x1B[22m\x1B[1m"),
|
|
58
|
+
dim: f("\x1B[2m", "\x1B[22m", "\x1B[22m\x1B[2m"),
|
|
59
|
+
italic: f("\x1B[3m", "\x1B[23m"),
|
|
60
|
+
underline: f("\x1B[4m", "\x1B[24m"),
|
|
61
|
+
inverse: f("\x1B[7m", "\x1B[27m"),
|
|
62
|
+
hidden: f("\x1B[8m", "\x1B[28m"),
|
|
63
|
+
strikethrough: f("\x1B[9m", "\x1B[29m"),
|
|
64
|
+
black: f("\x1B[30m", "\x1B[39m"),
|
|
65
|
+
red: f("\x1B[31m", "\x1B[39m"),
|
|
66
|
+
green: f("\x1B[32m", "\x1B[39m"),
|
|
67
|
+
yellow: f("\x1B[33m", "\x1B[39m"),
|
|
68
|
+
blue: f("\x1B[34m", "\x1B[39m"),
|
|
69
|
+
magenta: f("\x1B[35m", "\x1B[39m"),
|
|
70
|
+
cyan: f("\x1B[36m", "\x1B[39m"),
|
|
71
|
+
white: f("\x1B[37m", "\x1B[39m"),
|
|
72
|
+
gray: f("\x1B[90m", "\x1B[39m"),
|
|
73
|
+
bgBlack: f("\x1B[40m", "\x1B[49m"),
|
|
74
|
+
bgRed: f("\x1B[41m", "\x1B[49m"),
|
|
75
|
+
bgGreen: f("\x1B[42m", "\x1B[49m"),
|
|
76
|
+
bgYellow: f("\x1B[43m", "\x1B[49m"),
|
|
77
|
+
bgBlue: f("\x1B[44m", "\x1B[49m"),
|
|
78
|
+
bgMagenta: f("\x1B[45m", "\x1B[49m"),
|
|
79
|
+
bgCyan: f("\x1B[46m", "\x1B[49m"),
|
|
80
|
+
bgWhite: f("\x1B[47m", "\x1B[49m"),
|
|
81
|
+
blackBright: f("\x1B[90m", "\x1B[39m"),
|
|
82
|
+
redBright: f("\x1B[91m", "\x1B[39m"),
|
|
83
|
+
greenBright: f("\x1B[92m", "\x1B[39m"),
|
|
84
|
+
yellowBright: f("\x1B[93m", "\x1B[39m"),
|
|
85
|
+
blueBright: f("\x1B[94m", "\x1B[39m"),
|
|
86
|
+
magentaBright: f("\x1B[95m", "\x1B[39m"),
|
|
87
|
+
cyanBright: f("\x1B[96m", "\x1B[39m"),
|
|
88
|
+
whiteBright: f("\x1B[97m", "\x1B[39m"),
|
|
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")
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
module.exports = createColors();
|
|
100
|
+
module.exports.createColors = createColors;
|
|
101
|
+
} });
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/snippets.ts
|
|
105
|
+
const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();`;
|
|
106
|
+
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
107
|
+
const legacyEntryId = "vite-legacy-entry";
|
|
108
|
+
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
|
|
109
|
+
const detectModernBrowserVarName = "__vite_is_modern_browser";
|
|
110
|
+
const detectModernBrowserDetector = `import.meta.url;import("_").catch(()=>1);(async function*(){})().next()`;
|
|
111
|
+
const detectModernBrowserCode = `${detectModernBrowserDetector};window.${detectModernBrowserVarName}=true`;
|
|
112
|
+
const dynamicFallbackInlineCode = `!function(){if(window.${detectModernBrowserVarName})return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}();`;
|
|
113
|
+
const modernChunkLegacyGuard = `export function __vite_legacy_guard(){${detectModernBrowserDetector}};`;
|
|
114
|
+
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/index.ts
|
|
117
|
+
var import_picocolors = __toESM(require_picocolors(), 1);
|
|
118
|
+
let babel;
|
|
119
|
+
async function loadBabel() {
|
|
120
|
+
return babel ??= import("@babel/core");
|
|
121
|
+
}
|
|
122
|
+
const { loadConfig: browserslistLoadConfig } = browserslist;
|
|
123
|
+
function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRelative) {
|
|
124
|
+
const { renderBuiltUrl } = config.experimental;
|
|
125
|
+
let relative = config.base === "" || config.base === "./";
|
|
126
|
+
if (renderBuiltUrl) {
|
|
127
|
+
const result = renderBuiltUrl(filename, {
|
|
128
|
+
hostId,
|
|
129
|
+
hostType,
|
|
130
|
+
type,
|
|
131
|
+
ssr: !!config.build.ssr
|
|
132
|
+
});
|
|
133
|
+
if (typeof result === "object") {
|
|
134
|
+
if (result.runtime) throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
|
|
135
|
+
if (typeof result.relative === "boolean") relative = result.relative;
|
|
136
|
+
} else if (result) return result;
|
|
137
|
+
}
|
|
138
|
+
if (relative && !config.build.ssr) return toRelative(filename, hostId);
|
|
139
|
+
else return joinUrlSegments(config.decodedBase, filename);
|
|
140
|
+
}
|
|
141
|
+
function getBaseInHTML(urlRelativePath, config) {
|
|
142
|
+
return config.base === "./" || config.base === "" ? path.posix.join(path.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
|
|
143
|
+
}
|
|
144
|
+
function joinUrlSegments(a, b) {
|
|
145
|
+
if (!a || !b) return a || b || "";
|
|
146
|
+
if (a.endsWith("/")) a = a.substring(0, a.length - 1);
|
|
147
|
+
if (b[0] !== "/") b = "/" + b;
|
|
148
|
+
return a + b;
|
|
149
|
+
}
|
|
150
|
+
function toAssetPathFromHtml(filename, htmlPath, config) {
|
|
151
|
+
const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
|
|
152
|
+
const toRelative = (filename$1, _hostId) => getBaseInHTML(relativeUrlPath, config) + filename$1;
|
|
153
|
+
return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
|
|
154
|
+
}
|
|
155
|
+
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
156
|
+
const _require = createRequire(import.meta.url);
|
|
157
|
+
const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
|
|
158
|
+
const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
|
|
159
|
+
const outputOptionsForLegacyChunks = /* @__PURE__ */ new WeakSet();
|
|
160
|
+
function viteLegacyPlugin(options = {}) {
|
|
161
|
+
let config;
|
|
162
|
+
let targets;
|
|
163
|
+
let modernTargets;
|
|
164
|
+
const modernTargetsEsbuild = [
|
|
165
|
+
"es2020",
|
|
166
|
+
"edge79",
|
|
167
|
+
"firefox67",
|
|
168
|
+
"chrome64",
|
|
169
|
+
"safari12"
|
|
170
|
+
];
|
|
171
|
+
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
172
|
+
const genLegacy = options.renderLegacyChunks !== false;
|
|
173
|
+
const genModern = options.renderModernChunks !== false;
|
|
174
|
+
if (!genLegacy && !genModern) throw new Error("`renderLegacyChunks` and `renderModernChunks` cannot be both false");
|
|
175
|
+
const debugFlags = (process.env.DEBUG || "").split(",");
|
|
176
|
+
const isDebug = debugFlags.includes("vite:*") || debugFlags.includes("vite:legacy");
|
|
177
|
+
const assumptions = options.assumptions || {};
|
|
178
|
+
const facadeToLegacyChunkMap = /* @__PURE__ */ new Map();
|
|
179
|
+
const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
|
|
180
|
+
const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
|
|
181
|
+
const modernPolyfills = /* @__PURE__ */ new Set();
|
|
182
|
+
const legacyPolyfills = /* @__PURE__ */ new Set();
|
|
183
|
+
const outputToChunkFileNameToPolyfills = /* @__PURE__ */ new WeakMap();
|
|
184
|
+
if (Array.isArray(options.modernPolyfills) && genModern) options.modernPolyfills.forEach((i) => {
|
|
185
|
+
modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
186
|
+
});
|
|
187
|
+
if (Array.isArray(options.additionalModernPolyfills)) options.additionalModernPolyfills.forEach((i) => {
|
|
188
|
+
modernPolyfills.add(i);
|
|
189
|
+
});
|
|
190
|
+
if (Array.isArray(options.polyfills)) options.polyfills.forEach((i) => {
|
|
191
|
+
if (i.startsWith(`regenerator`)) legacyPolyfills.add(`regenerator-runtime/runtime.js`);
|
|
192
|
+
else legacyPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
193
|
+
});
|
|
194
|
+
if (Array.isArray(options.additionalLegacyPolyfills)) options.additionalLegacyPolyfills.forEach((i) => {
|
|
195
|
+
legacyPolyfills.add(i);
|
|
196
|
+
});
|
|
197
|
+
let overriddenBuildTarget = false;
|
|
198
|
+
let overriddenDefaultModernTargets = false;
|
|
199
|
+
const legacyConfigPlugin = {
|
|
200
|
+
name: "vite:legacy-config",
|
|
201
|
+
async config(config$1, env$1) {
|
|
202
|
+
if (env$1.command === "build" && !config$1.build?.ssr) {
|
|
203
|
+
if (!config$1.build) config$1.build = {};
|
|
204
|
+
if (genLegacy && !config$1.build.cssTarget) config$1.build.cssTarget = "chrome61";
|
|
205
|
+
if (genLegacy) {
|
|
206
|
+
overriddenBuildTarget = config$1.build.target !== void 0;
|
|
207
|
+
overriddenDefaultModernTargets = options.modernTargets !== void 0;
|
|
208
|
+
if (options.modernTargets) {
|
|
209
|
+
const { default: browserslistToEsbuild } = await import("browserslist-to-esbuild");
|
|
210
|
+
config$1.build.target = browserslistToEsbuild(options.modernTargets);
|
|
211
|
+
} else config$1.build.target = modernTargetsEsbuild;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return { define: { "import.meta.env.LEGACY": env$1.command === "serve" || config$1.build?.ssr ? false : legacyEnvVarMarker } };
|
|
215
|
+
},
|
|
216
|
+
configResolved(config$1) {
|
|
217
|
+
if (overriddenBuildTarget) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy overrode 'build.target'. You should pass 'targets' as an option to this plugin with the list of legacy browsers to support instead.`));
|
|
218
|
+
if (overriddenDefaultModernTargets) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy 'modernTargets' option overrode the builtin targets of modern chunks. Some versions of browsers between legacy and modern may not be supported.`));
|
|
219
|
+
if (config$1.isWorker) config$1.logger.warn(import_picocolors.default.yellow(`plugin-legacy should not be passed to 'worker.plugins'. Pass to 'plugins' instead. Note that generating legacy chunks for workers are not supported by plugin-legacy.`));
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const legacyGenerateBundlePlugin = {
|
|
223
|
+
name: "vite:legacy-generate-polyfill-chunk",
|
|
224
|
+
apply: "build",
|
|
225
|
+
async generateBundle(opts, bundle) {
|
|
226
|
+
if (config.build.ssr) return;
|
|
227
|
+
const chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
228
|
+
if (chunkFileNameToPolyfills == null) throw new Error("Internal @vitejs/plugin-legacy error: discovered polyfills should exist");
|
|
229
|
+
if (!isLegacyBundle(bundle)) {
|
|
230
|
+
for (const { modern } of chunkFileNameToPolyfills.values()) modern.forEach((p$1) => modernPolyfills.add(p$1));
|
|
231
|
+
if (!modernPolyfills.size) return;
|
|
232
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
233
|
+
await buildPolyfillChunk(this, config.mode, modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true, genLegacy);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (!genLegacy) return;
|
|
237
|
+
for (const { legacy } of chunkFileNameToPolyfills.values()) legacy.forEach((p$1) => legacyPolyfills.add(p$1));
|
|
238
|
+
if (options.polyfills !== false) await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, assumptions, legacyPolyfills);
|
|
239
|
+
if (legacyPolyfills.size || !options.externalSystemJS) {
|
|
240
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
241
|
+
await buildPolyfillChunk(this, config.mode, legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
const legacyPostPlugin = {
|
|
246
|
+
name: "vite:legacy-post-process",
|
|
247
|
+
enforce: "post",
|
|
248
|
+
apply: "build",
|
|
249
|
+
renderStart(opts) {
|
|
250
|
+
outputToChunkFileNameToPolyfills.set(opts, null);
|
|
251
|
+
},
|
|
252
|
+
configResolved(_config) {
|
|
253
|
+
if (_config.build.lib) throw new Error("@vitejs/plugin-legacy does not support library mode.");
|
|
254
|
+
config = _config;
|
|
255
|
+
modernTargets = options.modernTargets || modernTargetsBabel;
|
|
256
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
|
|
257
|
+
if (!genLegacy || config.build.ssr) return;
|
|
258
|
+
targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
|
|
259
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] targets:`, targets);
|
|
260
|
+
const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
|
|
261
|
+
if (!fileNames) return path.posix.join(config.build.assetsDir, defaultFileName);
|
|
262
|
+
return (chunkInfo) => {
|
|
263
|
+
let fileName = typeof fileNames === "function" ? fileNames(chunkInfo) : fileNames;
|
|
264
|
+
if (fileName.includes("[name]")) fileName = fileName.replace("[name]", "[name]-legacy");
|
|
265
|
+
else if (nonLeadingHashInFileNameRE.test(fileName)) fileName = fileName.replace(prefixedHashInFileNameRE, "-legacy$&");
|
|
266
|
+
else fileName = fileName.replace(/(.+?)\.(.+)/, "$1-legacy.$2");
|
|
267
|
+
return fileName;
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
const createLegacyOutput = (options$1 = {}) => {
|
|
271
|
+
return {
|
|
272
|
+
...options$1,
|
|
273
|
+
format: "esm",
|
|
274
|
+
entryFileNames: getLegacyOutputFileName(options$1.entryFileNames),
|
|
275
|
+
chunkFileNames: getLegacyOutputFileName(options$1.chunkFileNames),
|
|
276
|
+
minify: false
|
|
277
|
+
};
|
|
278
|
+
};
|
|
279
|
+
const { rollupOptions } = config.build;
|
|
280
|
+
const { output } = rollupOptions;
|
|
281
|
+
if (Array.isArray(output)) rollupOptions.output = [...output.map(createLegacyOutput), ...genModern ? output : []];
|
|
282
|
+
else rollupOptions.output = [createLegacyOutput(output), ...genModern ? [output || {}] : []];
|
|
283
|
+
_config.isOutputOptionsForLegacyChunks = (opts) => outputOptionsForLegacyChunks.has(opts);
|
|
284
|
+
},
|
|
285
|
+
async renderChunk(raw, chunk, opts, { chunks }) {
|
|
286
|
+
if (config.build.ssr) return null;
|
|
287
|
+
let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
288
|
+
if (chunkFileNameToPolyfills == null) {
|
|
289
|
+
chunkFileNameToPolyfills = /* @__PURE__ */ new Map();
|
|
290
|
+
for (const fileName in chunks) chunkFileNameToPolyfills.set(fileName, {
|
|
291
|
+
modern: /* @__PURE__ */ new Set(),
|
|
292
|
+
legacy: /* @__PURE__ */ new Set()
|
|
293
|
+
});
|
|
294
|
+
outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills);
|
|
295
|
+
}
|
|
296
|
+
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName);
|
|
297
|
+
if (polyfillsDiscovered == null) throw new Error(`Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`);
|
|
298
|
+
if (!isLegacyChunk(chunk)) {
|
|
299
|
+
if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) await detectPolyfills(raw, modernTargets, assumptions, polyfillsDiscovered.modern);
|
|
300
|
+
const ms = new MagicString(raw);
|
|
301
|
+
if (genLegacy && chunk.isEntry) ms.prepend(modernChunkLegacyGuard);
|
|
302
|
+
if (raw.includes(legacyEnvVarMarker)) {
|
|
303
|
+
const re = new RegExp(legacyEnvVarMarker, "g");
|
|
304
|
+
let match;
|
|
305
|
+
while (match = re.exec(raw)) ms.overwrite(match.index, match.index + legacyEnvVarMarker.length, `false`);
|
|
306
|
+
}
|
|
307
|
+
if (config.build.sourcemap) return {
|
|
308
|
+
code: ms.toString(),
|
|
309
|
+
map: ms.generateMap({ hires: "boundary" })
|
|
310
|
+
};
|
|
311
|
+
return { code: ms.toString() };
|
|
312
|
+
}
|
|
313
|
+
if (!genLegacy) return null;
|
|
314
|
+
outputOptionsForLegacyChunks.add(opts);
|
|
315
|
+
const needPolyfills = options.polyfills !== false && !Array.isArray(options.polyfills);
|
|
316
|
+
const sourceMaps = !!config.build.sourcemap;
|
|
317
|
+
const babel$1 = await loadBabel();
|
|
318
|
+
const resultSystem = babel$1.transform(raw, {
|
|
319
|
+
babelrc: false,
|
|
320
|
+
configFile: false,
|
|
321
|
+
ast: true,
|
|
322
|
+
code: false,
|
|
323
|
+
sourceMaps,
|
|
324
|
+
plugins: [(await import("@babel/plugin-transform-dynamic-import")).default, (await import("@babel/plugin-transform-modules-systemjs")).default]
|
|
325
|
+
});
|
|
326
|
+
const babelTransformOptions = {
|
|
327
|
+
babelrc: false,
|
|
328
|
+
configFile: false,
|
|
329
|
+
cloneInputAst: false,
|
|
330
|
+
compact: !!config.build.minify,
|
|
331
|
+
sourceMaps,
|
|
332
|
+
inputSourceMap: void 0,
|
|
333
|
+
targets,
|
|
334
|
+
assumptions,
|
|
335
|
+
browserslistConfigFile: false,
|
|
336
|
+
presets: [[() => ({ plugins: [
|
|
337
|
+
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
|
|
338
|
+
replaceLegacyEnvBabelPlugin(),
|
|
339
|
+
wrapIIFEBabelPlugin()
|
|
340
|
+
] })], [(await import("@babel/preset-env")).default, {
|
|
341
|
+
bugfixes: true,
|
|
342
|
+
modules: false,
|
|
343
|
+
useBuiltIns: needPolyfills ? "usage" : false,
|
|
344
|
+
corejs: needPolyfills ? {
|
|
345
|
+
version: _require("core-js/package.json").version,
|
|
346
|
+
proposals: false
|
|
347
|
+
} : void 0,
|
|
348
|
+
shippedProposals: true
|
|
349
|
+
}]]
|
|
350
|
+
};
|
|
351
|
+
let result;
|
|
352
|
+
if (resultSystem) result = babel$1.transformFromAstSync(resultSystem.ast, void 0, babelTransformOptions);
|
|
353
|
+
else result = babel$1.transform(raw, babelTransformOptions);
|
|
354
|
+
if (result) return {
|
|
355
|
+
code: result.code,
|
|
356
|
+
map: result.map
|
|
357
|
+
};
|
|
358
|
+
return null;
|
|
359
|
+
},
|
|
360
|
+
transformIndexHtml(html, { chunk }) {
|
|
361
|
+
if (config.build.ssr) return;
|
|
362
|
+
if (!chunk) return;
|
|
363
|
+
if (chunk.fileName.includes("-legacy")) {
|
|
364
|
+
facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
|
|
365
|
+
if (genModern) return;
|
|
366
|
+
}
|
|
367
|
+
if (!genModern) html = html.replace(/<script type="module".*?<\/script>/g, "");
|
|
368
|
+
const tags = [];
|
|
369
|
+
const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
|
|
370
|
+
if (genModern) {
|
|
371
|
+
const modernPolyfillFilename = facadeToModernPolyfillMap.get(chunk.facadeModuleId);
|
|
372
|
+
if (modernPolyfillFilename) tags.push({
|
|
373
|
+
tag: "script",
|
|
374
|
+
attrs: {
|
|
375
|
+
type: "module",
|
|
376
|
+
crossorigin: true,
|
|
377
|
+
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
else if (modernPolyfills.size) throw new Error(`No corresponding modern polyfill chunk found for ${htmlFilename}`);
|
|
381
|
+
}
|
|
382
|
+
if (!genLegacy) return {
|
|
383
|
+
html,
|
|
384
|
+
tags
|
|
385
|
+
};
|
|
386
|
+
if (genModern) tags.push({
|
|
387
|
+
tag: "script",
|
|
388
|
+
attrs: { nomodule: genModern },
|
|
389
|
+
children: safari10NoModuleFix,
|
|
390
|
+
injectTo: "body"
|
|
391
|
+
});
|
|
392
|
+
const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(chunk.facadeModuleId);
|
|
393
|
+
if (legacyPolyfillFilename) tags.push({
|
|
394
|
+
tag: "script",
|
|
395
|
+
attrs: {
|
|
396
|
+
nomodule: genModern,
|
|
397
|
+
crossorigin: true,
|
|
398
|
+
id: legacyPolyfillId,
|
|
399
|
+
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
400
|
+
},
|
|
401
|
+
injectTo: "body"
|
|
402
|
+
});
|
|
403
|
+
else if (legacyPolyfills.size) throw new Error(`No corresponding legacy polyfill chunk found for ${htmlFilename}`);
|
|
404
|
+
const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
|
|
405
|
+
if (legacyEntryFilename) tags.push({
|
|
406
|
+
tag: "script",
|
|
407
|
+
attrs: {
|
|
408
|
+
nomodule: genModern,
|
|
409
|
+
crossorigin: true,
|
|
410
|
+
id: legacyEntryId,
|
|
411
|
+
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
412
|
+
},
|
|
413
|
+
children: systemJSInlineCode,
|
|
414
|
+
injectTo: "body"
|
|
415
|
+
});
|
|
416
|
+
else throw new Error(`No corresponding legacy entry chunk found for ${htmlFilename}`);
|
|
417
|
+
if (legacyPolyfillFilename && legacyEntryFilename && genModern) {
|
|
418
|
+
tags.push({
|
|
419
|
+
tag: "script",
|
|
420
|
+
attrs: { type: "module" },
|
|
421
|
+
children: detectModernBrowserCode,
|
|
422
|
+
injectTo: "head"
|
|
423
|
+
});
|
|
424
|
+
tags.push({
|
|
425
|
+
tag: "script",
|
|
426
|
+
attrs: { type: "module" },
|
|
427
|
+
children: dynamicFallbackInlineCode,
|
|
428
|
+
injectTo: "head"
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
html,
|
|
433
|
+
tags
|
|
434
|
+
};
|
|
435
|
+
},
|
|
436
|
+
generateBundle(_opts, bundle) {
|
|
437
|
+
if (config.build.ssr) return;
|
|
438
|
+
if (isLegacyBundle(bundle) && genModern) {
|
|
439
|
+
for (const name in bundle) if (bundle[name].type === "asset" && !name.endsWith(".map") && !name.includes("-legacy")) delete bundle[name];
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
return [
|
|
444
|
+
legacyConfigPlugin,
|
|
445
|
+
legacyGenerateBundlePlugin,
|
|
446
|
+
legacyPostPlugin
|
|
447
|
+
];
|
|
448
|
+
}
|
|
449
|
+
async function detectPolyfills(code, targets, assumptions, list) {
|
|
450
|
+
const babel$1 = await loadBabel();
|
|
451
|
+
const result = babel$1.transform(code, {
|
|
452
|
+
ast: true,
|
|
453
|
+
code: false,
|
|
454
|
+
babelrc: false,
|
|
455
|
+
configFile: false,
|
|
456
|
+
compact: false,
|
|
457
|
+
targets,
|
|
458
|
+
assumptions,
|
|
459
|
+
browserslistConfigFile: false,
|
|
460
|
+
plugins: [[(await import("babel-plugin-polyfill-corejs3")).default, {
|
|
461
|
+
method: "usage-global",
|
|
462
|
+
version: _require("core-js/package.json").version,
|
|
463
|
+
shippedProposals: true
|
|
464
|
+
}], [(await import("babel-plugin-polyfill-regenerator")).default, { method: "usage-global" }]]
|
|
465
|
+
});
|
|
466
|
+
for (const node of result.ast.program.body) if (node.type === "ImportDeclaration") {
|
|
467
|
+
const source = node.source.value;
|
|
468
|
+
if (source.startsWith("core-js/") || source.startsWith("regenerator-runtime/")) list.add(source);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
async function buildPolyfillChunk(ctx, mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
|
|
472
|
+
let { minify, assetsDir, sourcemap } = buildOptions;
|
|
473
|
+
minify = minify ? "terser" : false;
|
|
474
|
+
const res = await build({
|
|
475
|
+
mode,
|
|
476
|
+
root: path.dirname(fileURLToPath(import.meta.url)),
|
|
477
|
+
configFile: false,
|
|
478
|
+
logLevel: "error",
|
|
479
|
+
plugins: [polyfillsPlugin(imports, excludeSystemJS), prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()],
|
|
480
|
+
build: {
|
|
481
|
+
write: false,
|
|
482
|
+
minify,
|
|
483
|
+
assetsDir,
|
|
484
|
+
sourcemap,
|
|
485
|
+
rollupOptions: {
|
|
486
|
+
input: { polyfills: polyfillId },
|
|
487
|
+
output: {
|
|
488
|
+
format,
|
|
489
|
+
hashCharacters: rollupOutputOptions.hashCharacters,
|
|
490
|
+
entryFileNames: rollupOutputOptions.entryFileNames
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
},
|
|
494
|
+
esbuild: false,
|
|
495
|
+
optimizeDeps: { esbuildOptions: { target: "es5" } }
|
|
496
|
+
});
|
|
497
|
+
const _polyfillChunk = Array.isArray(res) ? res[0] : res;
|
|
498
|
+
if (!("output" in _polyfillChunk)) return;
|
|
499
|
+
const polyfillChunk = _polyfillChunk.output.find((chunk) => chunk.type === "chunk" && chunk.isEntry);
|
|
500
|
+
for (const key in bundle) {
|
|
501
|
+
const chunk = bundle[key];
|
|
502
|
+
if (chunk.type === "chunk" && chunk.facadeModuleId) facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
|
|
503
|
+
}
|
|
504
|
+
ctx.emitFile({
|
|
505
|
+
type: "asset",
|
|
506
|
+
fileName: polyfillChunk.fileName,
|
|
507
|
+
source: polyfillChunk.code
|
|
508
|
+
});
|
|
509
|
+
if (polyfillChunk.sourcemapFileName) {
|
|
510
|
+
const polyfillChunkMapAsset = _polyfillChunk.output.find((chunk) => chunk.type === "asset" && chunk.fileName === polyfillChunk.sourcemapFileName);
|
|
511
|
+
if (polyfillChunkMapAsset) ctx.emitFile({
|
|
512
|
+
type: "asset",
|
|
513
|
+
fileName: polyfillChunkMapAsset.fileName,
|
|
514
|
+
source: polyfillChunkMapAsset.source
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const polyfillId = "\0vite/legacy-polyfills";
|
|
519
|
+
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
520
|
+
return {
|
|
521
|
+
name: "vite:legacy-polyfills",
|
|
522
|
+
resolveId(id) {
|
|
523
|
+
if (id === polyfillId) return id;
|
|
524
|
+
},
|
|
525
|
+
load(id) {
|
|
526
|
+
if (id === polyfillId) return [...imports].map((i) => `import ${JSON.stringify(i)};`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
function prependModenChunkLegacyGuardPlugin() {
|
|
531
|
+
let sourceMapEnabled;
|
|
532
|
+
return {
|
|
533
|
+
name: "vite:legacy-prepend-moden-chunk-legacy-guard",
|
|
534
|
+
configResolved(config) {
|
|
535
|
+
sourceMapEnabled = !!config.build.sourcemap;
|
|
536
|
+
},
|
|
537
|
+
renderChunk(code) {
|
|
538
|
+
if (!sourceMapEnabled) return modernChunkLegacyGuard + code;
|
|
539
|
+
const ms = new MagicString(code);
|
|
540
|
+
ms.prepend(modernChunkLegacyGuard);
|
|
541
|
+
return {
|
|
542
|
+
code: ms.toString(),
|
|
543
|
+
map: ms.generateMap({ hires: "boundary" })
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
function isLegacyChunk(chunk) {
|
|
549
|
+
return chunk.fileName.includes("-legacy");
|
|
550
|
+
}
|
|
551
|
+
function isLegacyBundle(bundle) {
|
|
552
|
+
const entryChunk = Object.values(bundle).find((output) => output.type === "chunk" && output.isEntry);
|
|
553
|
+
return !!entryChunk && entryChunk.fileName.includes("-legacy");
|
|
554
|
+
}
|
|
555
|
+
function recordAndRemovePolyfillBabelPlugin(polyfills) {
|
|
556
|
+
return ({ types: t }) => ({
|
|
557
|
+
name: "vite-remove-polyfill-import",
|
|
558
|
+
post({ path: path$1 }) {
|
|
559
|
+
path$1.get("body").forEach((p$1) => {
|
|
560
|
+
if (t.isImportDeclaration(p$1.node)) {
|
|
561
|
+
polyfills.add(p$1.node.source.value);
|
|
562
|
+
p$1.remove();
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
function replaceLegacyEnvBabelPlugin() {
|
|
569
|
+
return ({ types: t }) => ({
|
|
570
|
+
name: "vite-replace-env-legacy",
|
|
571
|
+
visitor: { Identifier(path$1) {
|
|
572
|
+
if (path$1.node.name === legacyEnvVarMarker) path$1.replaceWith(t.booleanLiteral(true));
|
|
573
|
+
} }
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
function wrapIIFEBabelPlugin() {
|
|
577
|
+
return ({ types: t, template }) => {
|
|
578
|
+
const buildIIFE = template(";(function(){%%body%%})();");
|
|
579
|
+
return {
|
|
580
|
+
name: "vite-wrap-iife",
|
|
581
|
+
post({ path: path$1 }) {
|
|
582
|
+
if (!this.isWrapped) {
|
|
583
|
+
this.isWrapped = true;
|
|
584
|
+
path$1.replaceWith(t.program(buildIIFE({ body: path$1.node.body })));
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
const cspHashes = [
|
|
591
|
+
safari10NoModuleFix,
|
|
592
|
+
systemJSInlineCode,
|
|
593
|
+
detectModernBrowserCode,
|
|
594
|
+
dynamicFallbackInlineCode
|
|
595
|
+
].map((i) => crypto.hash("sha256", i, "base64"));
|
|
596
|
+
var src_default = viteLegacyPlugin;
|
|
597
|
+
function viteLegacyPluginCjs(options) {
|
|
598
|
+
return viteLegacyPlugin.call(this, options);
|
|
599
|
+
}
|
|
600
|
+
Object.assign(viteLegacyPluginCjs, {
|
|
601
|
+
cspHashes,
|
|
602
|
+
default: viteLegacyPluginCjs,
|
|
603
|
+
detectPolyfills
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
//#endregion
|
|
607
|
+
export { cspHashes, src_default as default, detectPolyfills, viteLegacyPluginCjs as "module.exports" };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitejs/plugin-legacy",
|
|
3
|
-
"version": "7.1
|
|
3
|
+
"version": "7.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Evan You",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"babel-plugin-polyfill-regenerator": "^0.6.5",
|
|
43
43
|
"browserslist": "^4.25.1",
|
|
44
44
|
"browserslist-to-esbuild": "^2.1.1",
|
|
45
|
-
"core-js": "^3.
|
|
45
|
+
"core-js": "^3.45.0",
|
|
46
46
|
"magic-string": "^0.30.17",
|
|
47
47
|
"regenerator-runtime": "^0.14.1",
|
|
48
48
|
"systemjs": "^6.15.1"
|
|
@@ -52,12 +52,12 @@
|
|
|
52
52
|
"vite": "^7.0.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@vitejs/plugin-legacy-for-rolldown-vite": "https://pkg.pr.new/vitejs/rolldown-vite/@vitejs/plugin-legacy@
|
|
55
|
+
"@vitejs/plugin-legacy-for-rolldown-vite": "https://pkg.pr.new/vitejs/rolldown-vite/@vitejs/plugin-legacy@a2594ec",
|
|
56
56
|
"acorn": "^8.15.0",
|
|
57
57
|
"fdir": "^6.4.6",
|
|
58
58
|
"picocolors": "^1.1.1",
|
|
59
|
-
"tsdown": "^0.
|
|
60
|
-
"vite": "7.0
|
|
59
|
+
"tsdown": "^0.13.3",
|
|
60
|
+
"vite": "7.1.0"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"dev": "tsdown --watch",
|