@vitejs/plugin-legacy 6.1.1 → 7.0.0-beta.0
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/dist/index.d.ts +43 -41
- package/dist/index.js +587 -0
- package/package.json +11 -20
- package/dist/index.cjs +0 -864
- package/dist/index.d.cts +0 -47
- package/dist/index.d.mts +0 -47
- package/dist/index.mjs +0 -849
package/dist/index.d.ts
CHANGED
|
@@ -1,47 +1,49 @@
|
|
|
1
|
-
import { Plugin } from
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
2
|
|
|
3
|
+
//#region src/types.d.ts
|
|
3
4
|
interface Options {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
5
|
+
/**
|
|
6
|
+
* default: 'defaults'
|
|
7
|
+
*/
|
|
8
|
+
targets?: string | string[] | Record<string, string>;
|
|
9
|
+
/**
|
|
10
|
+
* default: 'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'
|
|
11
|
+
*/
|
|
12
|
+
modernTargets?: string | string[];
|
|
13
|
+
/**
|
|
14
|
+
* default: true
|
|
15
|
+
*/
|
|
16
|
+
polyfills?: boolean | string[];
|
|
17
|
+
additionalLegacyPolyfills?: string[];
|
|
18
|
+
additionalModernPolyfills?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* default: false
|
|
21
|
+
*/
|
|
22
|
+
modernPolyfills?: boolean | string[];
|
|
23
|
+
/**
|
|
24
|
+
* default: true
|
|
25
|
+
*/
|
|
26
|
+
renderLegacyChunks?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* default: false
|
|
29
|
+
*/
|
|
30
|
+
externalSystemJS?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* default: true
|
|
33
|
+
*/
|
|
34
|
+
renderModernChunks?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* @see https://babeljs.io/docs/assumptions
|
|
37
|
+
*
|
|
38
|
+
* default: {}
|
|
39
|
+
*/
|
|
40
|
+
assumptions?: Record<string, boolean>;
|
|
41
|
+
} //#endregion
|
|
42
|
+
//#region src/index.d.ts
|
|
42
43
|
declare function viteLegacyPlugin(options?: Options): Plugin[];
|
|
43
44
|
declare function detectPolyfills(code: string, targets: any, assumptions: Record<string, boolean>, list: Set<string>): Promise<void>;
|
|
44
45
|
declare const cspHashes: string[];
|
|
46
|
+
declare function viteLegacyPluginCjs(this: unknown, options: Options): Plugin[];
|
|
45
47
|
|
|
46
|
-
|
|
47
|
-
export
|
|
48
|
+
//#endregion
|
|
49
|
+
export { Options, cspHashes, viteLegacyPlugin as default, detectPolyfills, viteLegacyPluginCjs as "module.exports" };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
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};if(location.protocol!="file:"){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
|
+
function viteLegacyPlugin(options = {}) {
|
|
160
|
+
let config;
|
|
161
|
+
let targets;
|
|
162
|
+
let modernTargets;
|
|
163
|
+
const modernTargetsEsbuild = [
|
|
164
|
+
"es2020",
|
|
165
|
+
"edge79",
|
|
166
|
+
"firefox67",
|
|
167
|
+
"chrome64",
|
|
168
|
+
"safari12"
|
|
169
|
+
];
|
|
170
|
+
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
171
|
+
const genLegacy = options.renderLegacyChunks !== false;
|
|
172
|
+
const genModern = options.renderModernChunks !== false;
|
|
173
|
+
if (!genLegacy && !genModern) throw new Error("`renderLegacyChunks` and `renderModernChunks` cannot be both false");
|
|
174
|
+
const debugFlags = (process.env.DEBUG || "").split(",");
|
|
175
|
+
const isDebug = debugFlags.includes("vite:*") || debugFlags.includes("vite:legacy");
|
|
176
|
+
const assumptions = options.assumptions || {};
|
|
177
|
+
const facadeToLegacyChunkMap = new Map();
|
|
178
|
+
const facadeToLegacyPolyfillMap = new Map();
|
|
179
|
+
const facadeToModernPolyfillMap = new Map();
|
|
180
|
+
const modernPolyfills = new Set();
|
|
181
|
+
const legacyPolyfills = new Set();
|
|
182
|
+
const outputToChunkFileNameToPolyfills = new WeakMap();
|
|
183
|
+
if (Array.isArray(options.modernPolyfills) && genModern) options.modernPolyfills.forEach((i) => {
|
|
184
|
+
modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
185
|
+
});
|
|
186
|
+
if (Array.isArray(options.additionalModernPolyfills)) options.additionalModernPolyfills.forEach((i) => {
|
|
187
|
+
modernPolyfills.add(i);
|
|
188
|
+
});
|
|
189
|
+
if (Array.isArray(options.polyfills)) options.polyfills.forEach((i) => {
|
|
190
|
+
if (i.startsWith(`regenerator`)) legacyPolyfills.add(`regenerator-runtime/runtime.js`);
|
|
191
|
+
else legacyPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
192
|
+
});
|
|
193
|
+
if (Array.isArray(options.additionalLegacyPolyfills)) options.additionalLegacyPolyfills.forEach((i) => {
|
|
194
|
+
legacyPolyfills.add(i);
|
|
195
|
+
});
|
|
196
|
+
let overriddenBuildTarget = false;
|
|
197
|
+
let overriddenDefaultModernTargets = false;
|
|
198
|
+
const legacyConfigPlugin = {
|
|
199
|
+
name: "vite:legacy-config",
|
|
200
|
+
async config(config$1, env$1) {
|
|
201
|
+
if (env$1.command === "build" && !config$1.build?.ssr) {
|
|
202
|
+
if (!config$1.build) config$1.build = {};
|
|
203
|
+
if (!config$1.build.cssTarget) config$1.build.cssTarget = "chrome61";
|
|
204
|
+
if (genLegacy) {
|
|
205
|
+
overriddenBuildTarget = config$1.build.target !== void 0;
|
|
206
|
+
overriddenDefaultModernTargets = options.modernTargets !== void 0;
|
|
207
|
+
if (options.modernTargets) {
|
|
208
|
+
const { default: browserslistToEsbuild } = await import("browserslist-to-esbuild");
|
|
209
|
+
config$1.build.target = browserslistToEsbuild(options.modernTargets);
|
|
210
|
+
} else config$1.build.target = modernTargetsEsbuild;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return { define: { "import.meta.env.LEGACY": env$1.command === "serve" || config$1.build?.ssr ? false : legacyEnvVarMarker } };
|
|
214
|
+
},
|
|
215
|
+
configResolved(config$1) {
|
|
216
|
+
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.`));
|
|
217
|
+
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.`));
|
|
218
|
+
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.`));
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const legacyGenerateBundlePlugin = {
|
|
222
|
+
name: "vite:legacy-generate-polyfill-chunk",
|
|
223
|
+
apply: "build",
|
|
224
|
+
async generateBundle(opts, bundle) {
|
|
225
|
+
if (config.build.ssr) return;
|
|
226
|
+
const chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
227
|
+
if (chunkFileNameToPolyfills == null) throw new Error("Internal @vitejs/plugin-legacy error: discovered polyfills should exist");
|
|
228
|
+
if (!isLegacyBundle(bundle, opts)) {
|
|
229
|
+
for (const { modern } of chunkFileNameToPolyfills.values()) modern.forEach((p$1) => modernPolyfills.add(p$1));
|
|
230
|
+
if (!modernPolyfills.size) return;
|
|
231
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
232
|
+
await buildPolyfillChunk(config.mode, modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true, genLegacy);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (!genLegacy) return;
|
|
236
|
+
for (const { legacy } of chunkFileNameToPolyfills.values()) legacy.forEach((p$1) => legacyPolyfills.add(p$1));
|
|
237
|
+
if (options.polyfills !== false) await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, assumptions, legacyPolyfills);
|
|
238
|
+
if (legacyPolyfills.size || !options.externalSystemJS) {
|
|
239
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
240
|
+
await buildPolyfillChunk(config.mode, legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
const legacyPostPlugin = {
|
|
245
|
+
name: "vite:legacy-post-process",
|
|
246
|
+
enforce: "post",
|
|
247
|
+
apply: "build",
|
|
248
|
+
renderStart(opts) {
|
|
249
|
+
outputToChunkFileNameToPolyfills.set(opts, null);
|
|
250
|
+
},
|
|
251
|
+
configResolved(_config) {
|
|
252
|
+
if (_config.build.lib) throw new Error("@vitejs/plugin-legacy does not support library mode.");
|
|
253
|
+
config = _config;
|
|
254
|
+
modernTargets = options.modernTargets || modernTargetsBabel;
|
|
255
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
|
|
256
|
+
if (!genLegacy || config.build.ssr) return;
|
|
257
|
+
targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
|
|
258
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] targets:`, targets);
|
|
259
|
+
const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
|
|
260
|
+
if (!fileNames) return path.posix.join(config.build.assetsDir, defaultFileName);
|
|
261
|
+
return (chunkInfo) => {
|
|
262
|
+
let fileName = typeof fileNames === "function" ? fileNames(chunkInfo) : fileNames;
|
|
263
|
+
if (fileName.includes("[name]")) fileName = fileName.replace("[name]", "[name]-legacy");
|
|
264
|
+
else if (nonLeadingHashInFileNameRE.test(fileName)) fileName = fileName.replace(prefixedHashInFileNameRE, "-legacy$&");
|
|
265
|
+
else fileName = fileName.replace(/(.+?)\.(.+)/, "$1-legacy.$2");
|
|
266
|
+
return fileName;
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
const createLegacyOutput = (options$1 = {}) => {
|
|
270
|
+
return {
|
|
271
|
+
...options$1,
|
|
272
|
+
format: "system",
|
|
273
|
+
entryFileNames: getLegacyOutputFileName(options$1.entryFileNames),
|
|
274
|
+
chunkFileNames: getLegacyOutputFileName(options$1.chunkFileNames)
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
const { rollupOptions } = config.build;
|
|
278
|
+
const { output } = rollupOptions;
|
|
279
|
+
if (Array.isArray(output)) rollupOptions.output = [...output.map(createLegacyOutput), ...genModern ? output : []];
|
|
280
|
+
else rollupOptions.output = [createLegacyOutput(output), ...genModern ? [output || {}] : []];
|
|
281
|
+
},
|
|
282
|
+
async renderChunk(raw, chunk, opts, { chunks }) {
|
|
283
|
+
if (config.build.ssr) return null;
|
|
284
|
+
let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
285
|
+
if (chunkFileNameToPolyfills == null) {
|
|
286
|
+
chunkFileNameToPolyfills = new Map();
|
|
287
|
+
for (const fileName in chunks) chunkFileNameToPolyfills.set(fileName, {
|
|
288
|
+
modern: new Set(),
|
|
289
|
+
legacy: new Set()
|
|
290
|
+
});
|
|
291
|
+
outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills);
|
|
292
|
+
}
|
|
293
|
+
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName);
|
|
294
|
+
if (polyfillsDiscovered == null) throw new Error(`Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`);
|
|
295
|
+
if (!isLegacyChunk(chunk, opts)) {
|
|
296
|
+
if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) await detectPolyfills(raw, modernTargets, assumptions, polyfillsDiscovered.modern);
|
|
297
|
+
const ms = new MagicString(raw);
|
|
298
|
+
if (genLegacy && chunk.isEntry) ms.prepend(modernChunkLegacyGuard);
|
|
299
|
+
if (raw.includes(legacyEnvVarMarker)) {
|
|
300
|
+
const re = new RegExp(legacyEnvVarMarker, "g");
|
|
301
|
+
let match;
|
|
302
|
+
while (match = re.exec(raw)) ms.overwrite(match.index, match.index + legacyEnvVarMarker.length, `false`);
|
|
303
|
+
}
|
|
304
|
+
if (config.build.sourcemap) return {
|
|
305
|
+
code: ms.toString(),
|
|
306
|
+
map: ms.generateMap({ hires: "boundary" })
|
|
307
|
+
};
|
|
308
|
+
return { code: ms.toString() };
|
|
309
|
+
}
|
|
310
|
+
if (!genLegacy) return null;
|
|
311
|
+
opts.__vite_skip_esbuild__ = true;
|
|
312
|
+
opts.__vite_force_terser__ = true;
|
|
313
|
+
opts.__vite_skip_asset_emit__ = true;
|
|
314
|
+
const needPolyfills = options.polyfills !== false && !Array.isArray(options.polyfills);
|
|
315
|
+
const sourceMaps = !!config.build.sourcemap;
|
|
316
|
+
const babel$1 = await loadBabel();
|
|
317
|
+
const result = babel$1.transform(raw, {
|
|
318
|
+
babelrc: false,
|
|
319
|
+
configFile: false,
|
|
320
|
+
compact: !!config.build.minify,
|
|
321
|
+
sourceMaps,
|
|
322
|
+
inputSourceMap: void 0,
|
|
323
|
+
assumptions,
|
|
324
|
+
presets: [[() => ({ plugins: [
|
|
325
|
+
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
|
|
326
|
+
replaceLegacyEnvBabelPlugin(),
|
|
327
|
+
wrapIIFEBabelPlugin()
|
|
328
|
+
] })], [(await import("@babel/preset-env")).default, createBabelPresetEnvOptions(targets, { needPolyfills })]]
|
|
329
|
+
});
|
|
330
|
+
if (result) return {
|
|
331
|
+
code: result.code,
|
|
332
|
+
map: result.map
|
|
333
|
+
};
|
|
334
|
+
return null;
|
|
335
|
+
},
|
|
336
|
+
transformIndexHtml(html, { chunk }) {
|
|
337
|
+
if (config.build.ssr) return;
|
|
338
|
+
if (!chunk) return;
|
|
339
|
+
if (chunk.fileName.includes("-legacy")) {
|
|
340
|
+
facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
|
|
341
|
+
if (genModern) return;
|
|
342
|
+
}
|
|
343
|
+
if (!genModern) html = html.replace(/<script type="module".*?<\/script>/g, "");
|
|
344
|
+
const tags = [];
|
|
345
|
+
const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
|
|
346
|
+
if (genModern) {
|
|
347
|
+
const modernPolyfillFilename = facadeToModernPolyfillMap.get(chunk.facadeModuleId);
|
|
348
|
+
if (modernPolyfillFilename) tags.push({
|
|
349
|
+
tag: "script",
|
|
350
|
+
attrs: {
|
|
351
|
+
type: "module",
|
|
352
|
+
crossorigin: true,
|
|
353
|
+
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
else if (modernPolyfills.size) throw new Error(`No corresponding modern polyfill chunk found for ${htmlFilename}`);
|
|
357
|
+
}
|
|
358
|
+
if (!genLegacy) return {
|
|
359
|
+
html,
|
|
360
|
+
tags
|
|
361
|
+
};
|
|
362
|
+
if (genModern) tags.push({
|
|
363
|
+
tag: "script",
|
|
364
|
+
attrs: { nomodule: genModern },
|
|
365
|
+
children: safari10NoModuleFix,
|
|
366
|
+
injectTo: "body"
|
|
367
|
+
});
|
|
368
|
+
const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(chunk.facadeModuleId);
|
|
369
|
+
if (legacyPolyfillFilename) tags.push({
|
|
370
|
+
tag: "script",
|
|
371
|
+
attrs: {
|
|
372
|
+
nomodule: genModern,
|
|
373
|
+
crossorigin: true,
|
|
374
|
+
id: legacyPolyfillId,
|
|
375
|
+
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
376
|
+
},
|
|
377
|
+
injectTo: "body"
|
|
378
|
+
});
|
|
379
|
+
else if (legacyPolyfills.size) throw new Error(`No corresponding legacy polyfill chunk found for ${htmlFilename}`);
|
|
380
|
+
const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
|
|
381
|
+
if (legacyEntryFilename) tags.push({
|
|
382
|
+
tag: "script",
|
|
383
|
+
attrs: {
|
|
384
|
+
nomodule: genModern,
|
|
385
|
+
crossorigin: true,
|
|
386
|
+
id: legacyEntryId,
|
|
387
|
+
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
388
|
+
},
|
|
389
|
+
children: systemJSInlineCode,
|
|
390
|
+
injectTo: "body"
|
|
391
|
+
});
|
|
392
|
+
else throw new Error(`No corresponding legacy entry chunk found for ${htmlFilename}`);
|
|
393
|
+
if (legacyPolyfillFilename && legacyEntryFilename && genModern) {
|
|
394
|
+
tags.push({
|
|
395
|
+
tag: "script",
|
|
396
|
+
attrs: { type: "module" },
|
|
397
|
+
children: detectModernBrowserCode,
|
|
398
|
+
injectTo: "head"
|
|
399
|
+
});
|
|
400
|
+
tags.push({
|
|
401
|
+
tag: "script",
|
|
402
|
+
attrs: { type: "module" },
|
|
403
|
+
children: dynamicFallbackInlineCode,
|
|
404
|
+
injectTo: "head"
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
html,
|
|
409
|
+
tags
|
|
410
|
+
};
|
|
411
|
+
},
|
|
412
|
+
generateBundle(opts, bundle) {
|
|
413
|
+
if (config.build.ssr) return;
|
|
414
|
+
if (isLegacyBundle(bundle, opts) && genModern) {
|
|
415
|
+
for (const name in bundle) if (bundle[name].type === "asset" && !/.+\.map$/.test(name)) delete bundle[name];
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
return [
|
|
420
|
+
legacyConfigPlugin,
|
|
421
|
+
legacyGenerateBundlePlugin,
|
|
422
|
+
legacyPostPlugin
|
|
423
|
+
];
|
|
424
|
+
}
|
|
425
|
+
async function detectPolyfills(code, targets, assumptions, list) {
|
|
426
|
+
const babel$1 = await loadBabel();
|
|
427
|
+
const result = babel$1.transform(code, {
|
|
428
|
+
ast: true,
|
|
429
|
+
babelrc: false,
|
|
430
|
+
configFile: false,
|
|
431
|
+
compact: false,
|
|
432
|
+
assumptions,
|
|
433
|
+
presets: [[(await import("@babel/preset-env")).default, createBabelPresetEnvOptions(targets, {})]]
|
|
434
|
+
});
|
|
435
|
+
for (const node of result.ast.program.body) if (node.type === "ImportDeclaration") {
|
|
436
|
+
const source = node.source.value;
|
|
437
|
+
if (source.startsWith("core-js/") || source.startsWith("regenerator-runtime/")) list.add(source);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
|
|
441
|
+
return {
|
|
442
|
+
targets,
|
|
443
|
+
bugfixes: true,
|
|
444
|
+
loose: false,
|
|
445
|
+
modules: false,
|
|
446
|
+
useBuiltIns: needPolyfills ? "usage" : false,
|
|
447
|
+
corejs: needPolyfills ? {
|
|
448
|
+
version: _require("core-js/package.json").version,
|
|
449
|
+
proposals: false
|
|
450
|
+
} : void 0,
|
|
451
|
+
shippedProposals: true,
|
|
452
|
+
ignoreBrowserslistConfig: true
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
|
|
456
|
+
let { minify, assetsDir, sourcemap } = buildOptions;
|
|
457
|
+
minify = minify ? "terser" : false;
|
|
458
|
+
const res = await build({
|
|
459
|
+
mode,
|
|
460
|
+
root: path.dirname(fileURLToPath(import.meta.url)),
|
|
461
|
+
configFile: false,
|
|
462
|
+
logLevel: "error",
|
|
463
|
+
plugins: [polyfillsPlugin(imports, excludeSystemJS), prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()],
|
|
464
|
+
build: {
|
|
465
|
+
write: false,
|
|
466
|
+
minify,
|
|
467
|
+
assetsDir,
|
|
468
|
+
sourcemap,
|
|
469
|
+
rollupOptions: {
|
|
470
|
+
input: { polyfills: polyfillId },
|
|
471
|
+
output: {
|
|
472
|
+
format,
|
|
473
|
+
hashCharacters: rollupOutputOptions.hashCharacters,
|
|
474
|
+
entryFileNames: rollupOutputOptions.entryFileNames,
|
|
475
|
+
sourcemapBaseUrl: rollupOutputOptions.sourcemapBaseUrl
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
},
|
|
479
|
+
esbuild: false,
|
|
480
|
+
optimizeDeps: { esbuildOptions: { target: "es5" } }
|
|
481
|
+
});
|
|
482
|
+
const _polyfillChunk = Array.isArray(res) ? res[0] : res;
|
|
483
|
+
if (!("output" in _polyfillChunk)) return;
|
|
484
|
+
const polyfillChunk = _polyfillChunk.output.find((chunk) => chunk.type === "chunk" && chunk.isEntry);
|
|
485
|
+
for (const key in bundle) {
|
|
486
|
+
const chunk = bundle[key];
|
|
487
|
+
if (chunk.type === "chunk" && chunk.facadeModuleId) facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
|
|
488
|
+
}
|
|
489
|
+
bundle[polyfillChunk.fileName] = polyfillChunk;
|
|
490
|
+
if (polyfillChunk.sourcemapFileName) {
|
|
491
|
+
const polyfillChunkMapAsset = _polyfillChunk.output.find((chunk) => chunk.type === "asset" && chunk.fileName === polyfillChunk.sourcemapFileName);
|
|
492
|
+
if (polyfillChunkMapAsset) bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const polyfillId = "\0vite/legacy-polyfills";
|
|
496
|
+
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
497
|
+
return {
|
|
498
|
+
name: "vite:legacy-polyfills",
|
|
499
|
+
resolveId(id) {
|
|
500
|
+
if (id === polyfillId) return id;
|
|
501
|
+
},
|
|
502
|
+
load(id) {
|
|
503
|
+
if (id === polyfillId) return [...imports].map((i) => `import ${JSON.stringify(i)};`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
function prependModenChunkLegacyGuardPlugin() {
|
|
508
|
+
let sourceMapEnabled;
|
|
509
|
+
return {
|
|
510
|
+
name: "vite:legacy-prepend-moden-chunk-legacy-guard",
|
|
511
|
+
configResolved(config) {
|
|
512
|
+
sourceMapEnabled = !!config.build.sourcemap;
|
|
513
|
+
},
|
|
514
|
+
renderChunk(code) {
|
|
515
|
+
if (!sourceMapEnabled) return modernChunkLegacyGuard + code;
|
|
516
|
+
const ms = new MagicString(code);
|
|
517
|
+
ms.prepend(modernChunkLegacyGuard);
|
|
518
|
+
return {
|
|
519
|
+
code: ms.toString(),
|
|
520
|
+
map: ms.generateMap({ hires: "boundary" })
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
function isLegacyChunk(chunk, options) {
|
|
526
|
+
return options.format === "system" && chunk.fileName.includes("-legacy");
|
|
527
|
+
}
|
|
528
|
+
function isLegacyBundle(bundle, options) {
|
|
529
|
+
if (options.format === "system") {
|
|
530
|
+
const entryChunk = Object.values(bundle).find((output) => output.type === "chunk" && output.isEntry);
|
|
531
|
+
return !!entryChunk && entryChunk.fileName.includes("-legacy");
|
|
532
|
+
}
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
function recordAndRemovePolyfillBabelPlugin(polyfills) {
|
|
536
|
+
return ({ types: t }) => ({
|
|
537
|
+
name: "vite-remove-polyfill-import",
|
|
538
|
+
post({ path: path$1 }) {
|
|
539
|
+
path$1.get("body").forEach((p$1) => {
|
|
540
|
+
if (t.isImportDeclaration(p$1.node)) {
|
|
541
|
+
polyfills.add(p$1.node.source.value);
|
|
542
|
+
p$1.remove();
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
function replaceLegacyEnvBabelPlugin() {
|
|
549
|
+
return ({ types: t }) => ({
|
|
550
|
+
name: "vite-replace-env-legacy",
|
|
551
|
+
visitor: { Identifier(path$1) {
|
|
552
|
+
if (path$1.node.name === legacyEnvVarMarker) path$1.replaceWith(t.booleanLiteral(true));
|
|
553
|
+
} }
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
function wrapIIFEBabelPlugin() {
|
|
557
|
+
return ({ types: t, template }) => {
|
|
558
|
+
const buildIIFE = template(";(function(){%%body%%})();");
|
|
559
|
+
return {
|
|
560
|
+
name: "vite-wrap-iife",
|
|
561
|
+
post({ path: path$1 }) {
|
|
562
|
+
if (!this.isWrapped) {
|
|
563
|
+
this.isWrapped = true;
|
|
564
|
+
path$1.replaceWith(t.program(buildIIFE({ body: path$1.node.body })));
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
const cspHashes = [
|
|
571
|
+
safari10NoModuleFix,
|
|
572
|
+
systemJSInlineCode,
|
|
573
|
+
detectModernBrowserCode,
|
|
574
|
+
dynamicFallbackInlineCode
|
|
575
|
+
].map((i) => crypto.hash("sha256", i, "base64"));
|
|
576
|
+
var src_default = viteLegacyPlugin;
|
|
577
|
+
function viteLegacyPluginCjs(options) {
|
|
578
|
+
return viteLegacyPlugin.call(this, options);
|
|
579
|
+
}
|
|
580
|
+
Object.assign(viteLegacyPluginCjs, {
|
|
581
|
+
cspHashes,
|
|
582
|
+
default: viteLegacyPluginCjs,
|
|
583
|
+
detectPolyfills
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
//#endregion
|
|
587
|
+
export { cspHashes, src_default as default, detectPolyfills, viteLegacyPluginCjs as "module.exports" };
|