@vitejs/plugin-legacy 6.1.0 → 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 -46
- package/dist/index.mjs +0 -849
package/dist/index.mjs
DELETED
|
@@ -1,849 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import crypto from 'node:crypto';
|
|
3
|
-
import { createRequire } from 'node:module';
|
|
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
|
-
function getDefaultExportFromCjs (x) {
|
|
10
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
var picocolors = {exports: {}};
|
|
14
|
-
|
|
15
|
-
var hasRequiredPicocolors;
|
|
16
|
-
|
|
17
|
-
function requirePicocolors () {
|
|
18
|
-
if (hasRequiredPicocolors) return picocolors.exports;
|
|
19
|
-
hasRequiredPicocolors = 1;
|
|
20
|
-
let p = process || {}, argv = p.argv || [], env = p.env || {};
|
|
21
|
-
let isColorSupported =
|
|
22
|
-
!(!!env.NO_COLOR || argv.includes("--no-color")) &&
|
|
23
|
-
(!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || ((p.stdout || {}).isTTY && env.TERM !== "dumb") || !!env.CI);
|
|
24
|
-
|
|
25
|
-
let formatter = (open, close, replace = open) =>
|
|
26
|
-
input => {
|
|
27
|
-
let string = "" + input, index = string.indexOf(close, open.length);
|
|
28
|
-
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
let replaceClose = (string, close, replace, index) => {
|
|
32
|
-
let result = "", cursor = 0;
|
|
33
|
-
do {
|
|
34
|
-
result += string.substring(cursor, index) + replace;
|
|
35
|
-
cursor = index + close.length;
|
|
36
|
-
index = string.indexOf(close, cursor);
|
|
37
|
-
} while (~index)
|
|
38
|
-
return result + string.substring(cursor)
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
let createColors = (enabled = isColorSupported) => {
|
|
42
|
-
let f = enabled ? formatter : () => String;
|
|
43
|
-
return {
|
|
44
|
-
isColorSupported: enabled,
|
|
45
|
-
reset: f("\x1b[0m", "\x1b[0m"),
|
|
46
|
-
bold: f("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
|
|
47
|
-
dim: f("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
|
|
48
|
-
italic: f("\x1b[3m", "\x1b[23m"),
|
|
49
|
-
underline: f("\x1b[4m", "\x1b[24m"),
|
|
50
|
-
inverse: f("\x1b[7m", "\x1b[27m"),
|
|
51
|
-
hidden: f("\x1b[8m", "\x1b[28m"),
|
|
52
|
-
strikethrough: f("\x1b[9m", "\x1b[29m"),
|
|
53
|
-
|
|
54
|
-
black: f("\x1b[30m", "\x1b[39m"),
|
|
55
|
-
red: f("\x1b[31m", "\x1b[39m"),
|
|
56
|
-
green: f("\x1b[32m", "\x1b[39m"),
|
|
57
|
-
yellow: f("\x1b[33m", "\x1b[39m"),
|
|
58
|
-
blue: f("\x1b[34m", "\x1b[39m"),
|
|
59
|
-
magenta: f("\x1b[35m", "\x1b[39m"),
|
|
60
|
-
cyan: f("\x1b[36m", "\x1b[39m"),
|
|
61
|
-
white: f("\x1b[37m", "\x1b[39m"),
|
|
62
|
-
gray: f("\x1b[90m", "\x1b[39m"),
|
|
63
|
-
|
|
64
|
-
bgBlack: f("\x1b[40m", "\x1b[49m"),
|
|
65
|
-
bgRed: f("\x1b[41m", "\x1b[49m"),
|
|
66
|
-
bgGreen: f("\x1b[42m", "\x1b[49m"),
|
|
67
|
-
bgYellow: f("\x1b[43m", "\x1b[49m"),
|
|
68
|
-
bgBlue: f("\x1b[44m", "\x1b[49m"),
|
|
69
|
-
bgMagenta: f("\x1b[45m", "\x1b[49m"),
|
|
70
|
-
bgCyan: f("\x1b[46m", "\x1b[49m"),
|
|
71
|
-
bgWhite: f("\x1b[47m", "\x1b[49m"),
|
|
72
|
-
|
|
73
|
-
blackBright: f("\x1b[90m", "\x1b[39m"),
|
|
74
|
-
redBright: f("\x1b[91m", "\x1b[39m"),
|
|
75
|
-
greenBright: f("\x1b[92m", "\x1b[39m"),
|
|
76
|
-
yellowBright: f("\x1b[93m", "\x1b[39m"),
|
|
77
|
-
blueBright: f("\x1b[94m", "\x1b[39m"),
|
|
78
|
-
magentaBright: f("\x1b[95m", "\x1b[39m"),
|
|
79
|
-
cyanBright: f("\x1b[96m", "\x1b[39m"),
|
|
80
|
-
whiteBright: f("\x1b[97m", "\x1b[39m"),
|
|
81
|
-
|
|
82
|
-
bgBlackBright: f("\x1b[100m", "\x1b[49m"),
|
|
83
|
-
bgRedBright: f("\x1b[101m", "\x1b[49m"),
|
|
84
|
-
bgGreenBright: f("\x1b[102m", "\x1b[49m"),
|
|
85
|
-
bgYellowBright: f("\x1b[103m", "\x1b[49m"),
|
|
86
|
-
bgBlueBright: f("\x1b[104m", "\x1b[49m"),
|
|
87
|
-
bgMagentaBright: f("\x1b[105m", "\x1b[49m"),
|
|
88
|
-
bgCyanBright: f("\x1b[106m", "\x1b[49m"),
|
|
89
|
-
bgWhiteBright: f("\x1b[107m", "\x1b[49m"),
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
picocolors.exports = createColors();
|
|
94
|
-
picocolors.exports.createColors = createColors;
|
|
95
|
-
return picocolors.exports;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
var picocolorsExports = /*@__PURE__*/ requirePicocolors();
|
|
99
|
-
const colors = /*@__PURE__*/getDefaultExportFromCjs(picocolorsExports);
|
|
100
|
-
|
|
101
|
-
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()}}();`;
|
|
102
|
-
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
103
|
-
const legacyEntryId = "vite-legacy-entry";
|
|
104
|
-
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
|
|
105
|
-
const detectModernBrowserVarName = "__vite_is_modern_browser";
|
|
106
|
-
const detectModernBrowserDetector = `import.meta.url;import("_").catch(()=>1);(async function*(){})().next()`;
|
|
107
|
-
const detectModernBrowserCode = `${detectModernBrowserDetector};if(location.protocol!="file:"){window.${detectModernBrowserVarName}=true}`;
|
|
108
|
-
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)}();`;
|
|
109
|
-
const modernChunkLegacyGuard = `export function __vite_legacy_guard(){${detectModernBrowserDetector}};`;
|
|
110
|
-
|
|
111
|
-
let babel;
|
|
112
|
-
async function loadBabel() {
|
|
113
|
-
return babel ??= import('@babel/core');
|
|
114
|
-
}
|
|
115
|
-
const { loadConfig: browserslistLoadConfig } = browserslist;
|
|
116
|
-
function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRelative) {
|
|
117
|
-
const { renderBuiltUrl } = config.experimental;
|
|
118
|
-
let relative = config.base === "" || config.base === "./";
|
|
119
|
-
if (renderBuiltUrl) {
|
|
120
|
-
const result = renderBuiltUrl(filename, {
|
|
121
|
-
hostId,
|
|
122
|
-
hostType,
|
|
123
|
-
type,
|
|
124
|
-
ssr: !!config.build.ssr
|
|
125
|
-
});
|
|
126
|
-
if (typeof result === "object") {
|
|
127
|
-
if (result.runtime) {
|
|
128
|
-
throw new Error(
|
|
129
|
-
`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
if (typeof result.relative === "boolean") {
|
|
133
|
-
relative = result.relative;
|
|
134
|
-
}
|
|
135
|
-
} else if (result) {
|
|
136
|
-
return result;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (relative && !config.build.ssr) {
|
|
140
|
-
return toRelative(filename, hostId);
|
|
141
|
-
} else {
|
|
142
|
-
return joinUrlSegments(config.decodedBase, filename);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
function getBaseInHTML(urlRelativePath, config) {
|
|
146
|
-
return config.base === "./" || config.base === "" ? path.posix.join(
|
|
147
|
-
path.posix.relative(urlRelativePath, "").slice(0, -2),
|
|
148
|
-
"./"
|
|
149
|
-
) : config.base;
|
|
150
|
-
}
|
|
151
|
-
function joinUrlSegments(a, b) {
|
|
152
|
-
if (!a || !b) {
|
|
153
|
-
return a || b || "";
|
|
154
|
-
}
|
|
155
|
-
if (a.endsWith("/")) {
|
|
156
|
-
a = a.substring(0, a.length - 1);
|
|
157
|
-
}
|
|
158
|
-
if (b[0] !== "/") {
|
|
159
|
-
b = "/" + b;
|
|
160
|
-
}
|
|
161
|
-
return a + b;
|
|
162
|
-
}
|
|
163
|
-
function toAssetPathFromHtml(filename, htmlPath, config) {
|
|
164
|
-
const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
|
|
165
|
-
const toRelative = (filename2, _hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
|
|
166
|
-
return toOutputFilePathInHtml(
|
|
167
|
-
filename,
|
|
168
|
-
"asset",
|
|
169
|
-
htmlPath,
|
|
170
|
-
"html",
|
|
171
|
-
config,
|
|
172
|
-
toRelative
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
176
|
-
const _require = createRequire(import.meta.url);
|
|
177
|
-
const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
|
|
178
|
-
const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/;
|
|
179
|
-
function viteLegacyPlugin(options = {}) {
|
|
180
|
-
let config;
|
|
181
|
-
let targets;
|
|
182
|
-
let modernTargets;
|
|
183
|
-
const modernTargetsEsbuild = [
|
|
184
|
-
"es2020",
|
|
185
|
-
"edge79",
|
|
186
|
-
"firefox67",
|
|
187
|
-
"chrome64",
|
|
188
|
-
"safari12"
|
|
189
|
-
];
|
|
190
|
-
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
191
|
-
const genLegacy = options.renderLegacyChunks !== false;
|
|
192
|
-
const genModern = options.renderModernChunks !== false;
|
|
193
|
-
if (!genLegacy && !genModern) {
|
|
194
|
-
throw new Error(
|
|
195
|
-
"`renderLegacyChunks` and `renderModernChunks` cannot be both false"
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
const debugFlags = (process.env.DEBUG || "").split(",");
|
|
199
|
-
const isDebug = debugFlags.includes("vite:*") || debugFlags.includes("vite:legacy");
|
|
200
|
-
const assumptions = options.assumptions || {};
|
|
201
|
-
const facadeToLegacyChunkMap = /* @__PURE__ */ new Map();
|
|
202
|
-
const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
|
|
203
|
-
const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
|
|
204
|
-
const modernPolyfills = /* @__PURE__ */ new Set();
|
|
205
|
-
const legacyPolyfills = /* @__PURE__ */ new Set();
|
|
206
|
-
const outputToChunkFileNameToPolyfills = /* @__PURE__ */ new WeakMap();
|
|
207
|
-
if (Array.isArray(options.modernPolyfills) && genModern) {
|
|
208
|
-
options.modernPolyfills.forEach((i) => {
|
|
209
|
-
modernPolyfills.add(
|
|
210
|
-
i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
|
|
211
|
-
);
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
if (Array.isArray(options.additionalModernPolyfills)) {
|
|
215
|
-
options.additionalModernPolyfills.forEach((i) => {
|
|
216
|
-
modernPolyfills.add(i);
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
if (Array.isArray(options.polyfills)) {
|
|
220
|
-
options.polyfills.forEach((i) => {
|
|
221
|
-
if (i.startsWith(`regenerator`)) {
|
|
222
|
-
legacyPolyfills.add(`regenerator-runtime/runtime.js`);
|
|
223
|
-
} else {
|
|
224
|
-
legacyPolyfills.add(
|
|
225
|
-
i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
if (Array.isArray(options.additionalLegacyPolyfills)) {
|
|
231
|
-
options.additionalLegacyPolyfills.forEach((i) => {
|
|
232
|
-
legacyPolyfills.add(i);
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
let overriddenBuildTarget = false;
|
|
236
|
-
let overriddenDefaultModernTargets = false;
|
|
237
|
-
const legacyConfigPlugin = {
|
|
238
|
-
name: "vite:legacy-config",
|
|
239
|
-
async config(config2, env) {
|
|
240
|
-
if (env.command === "build" && !config2.build?.ssr) {
|
|
241
|
-
if (!config2.build) {
|
|
242
|
-
config2.build = {};
|
|
243
|
-
}
|
|
244
|
-
if (!config2.build.cssTarget) {
|
|
245
|
-
config2.build.cssTarget = "chrome61";
|
|
246
|
-
}
|
|
247
|
-
if (genLegacy) {
|
|
248
|
-
overriddenBuildTarget = config2.build.target !== void 0;
|
|
249
|
-
overriddenDefaultModernTargets = options.modernTargets !== void 0;
|
|
250
|
-
if (options.modernTargets) {
|
|
251
|
-
const { default: browserslistToEsbuild } = await import('browserslist-to-esbuild');
|
|
252
|
-
config2.build.target = browserslistToEsbuild(options.modernTargets);
|
|
253
|
-
} else {
|
|
254
|
-
config2.build.target = modernTargetsEsbuild;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return {
|
|
259
|
-
define: {
|
|
260
|
-
"import.meta.env.LEGACY": env.command === "serve" || config2.build?.ssr ? false : legacyEnvVarMarker
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
},
|
|
264
|
-
configResolved(config2) {
|
|
265
|
-
if (overriddenBuildTarget) {
|
|
266
|
-
config2.logger.warn(
|
|
267
|
-
colors.yellow(
|
|
268
|
-
`plugin-legacy overrode 'build.target'. You should pass 'targets' as an option to this plugin with the list of legacy browsers to support instead.`
|
|
269
|
-
)
|
|
270
|
-
);
|
|
271
|
-
}
|
|
272
|
-
if (overriddenDefaultModernTargets) {
|
|
273
|
-
config2.logger.warn(
|
|
274
|
-
colors.yellow(
|
|
275
|
-
`plugin-legacy 'modernTargets' option overrode the builtin targets of modern chunks. Some versions of browsers between legacy and modern may not be supported.`
|
|
276
|
-
)
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
if (config2.isWorker) {
|
|
280
|
-
config2.logger.warn(
|
|
281
|
-
colors.yellow(
|
|
282
|
-
`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.`
|
|
283
|
-
)
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
const legacyGenerateBundlePlugin = {
|
|
289
|
-
name: "vite:legacy-generate-polyfill-chunk",
|
|
290
|
-
apply: "build",
|
|
291
|
-
async generateBundle(opts, bundle) {
|
|
292
|
-
if (config.build.ssr) {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
const chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
296
|
-
if (chunkFileNameToPolyfills == null) {
|
|
297
|
-
throw new Error(
|
|
298
|
-
"Internal @vitejs/plugin-legacy error: discovered polyfills should exist"
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
if (!isLegacyBundle(bundle, opts)) {
|
|
302
|
-
for (const { modern } of chunkFileNameToPolyfills.values()) {
|
|
303
|
-
modern.forEach((p) => modernPolyfills.add(p));
|
|
304
|
-
}
|
|
305
|
-
if (!modernPolyfills.size) {
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
if (isDebug) {
|
|
309
|
-
console.log(
|
|
310
|
-
`[@vitejs/plugin-legacy] modern polyfills:`,
|
|
311
|
-
modernPolyfills
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
await buildPolyfillChunk(
|
|
315
|
-
config.mode,
|
|
316
|
-
modernPolyfills,
|
|
317
|
-
bundle,
|
|
318
|
-
facadeToModernPolyfillMap,
|
|
319
|
-
config.build,
|
|
320
|
-
"es",
|
|
321
|
-
opts,
|
|
322
|
-
true,
|
|
323
|
-
genLegacy
|
|
324
|
-
);
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
if (!genLegacy) {
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
for (const { legacy } of chunkFileNameToPolyfills.values()) {
|
|
331
|
-
legacy.forEach((p) => legacyPolyfills.add(p));
|
|
332
|
-
}
|
|
333
|
-
if (options.polyfills !== false) {
|
|
334
|
-
await detectPolyfills(
|
|
335
|
-
`Promise.resolve(); Promise.all();`,
|
|
336
|
-
targets,
|
|
337
|
-
assumptions,
|
|
338
|
-
legacyPolyfills
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
if (legacyPolyfills.size || !options.externalSystemJS) {
|
|
342
|
-
if (isDebug) {
|
|
343
|
-
console.log(
|
|
344
|
-
`[@vitejs/plugin-legacy] legacy polyfills:`,
|
|
345
|
-
legacyPolyfills
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
await buildPolyfillChunk(
|
|
349
|
-
config.mode,
|
|
350
|
-
legacyPolyfills,
|
|
351
|
-
bundle,
|
|
352
|
-
facadeToLegacyPolyfillMap,
|
|
353
|
-
// force using terser for legacy polyfill minification, since esbuild
|
|
354
|
-
// isn't legacy-safe
|
|
355
|
-
config.build,
|
|
356
|
-
"iife",
|
|
357
|
-
opts,
|
|
358
|
-
options.externalSystemJS
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
};
|
|
363
|
-
const legacyPostPlugin = {
|
|
364
|
-
name: "vite:legacy-post-process",
|
|
365
|
-
enforce: "post",
|
|
366
|
-
apply: "build",
|
|
367
|
-
renderStart(opts) {
|
|
368
|
-
outputToChunkFileNameToPolyfills.set(opts, null);
|
|
369
|
-
},
|
|
370
|
-
configResolved(_config) {
|
|
371
|
-
if (_config.build.lib) {
|
|
372
|
-
throw new Error("@vitejs/plugin-legacy does not support library mode.");
|
|
373
|
-
}
|
|
374
|
-
config = _config;
|
|
375
|
-
modernTargets = options.modernTargets || modernTargetsBabel;
|
|
376
|
-
if (isDebug) {
|
|
377
|
-
console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
|
|
378
|
-
}
|
|
379
|
-
if (!genLegacy || config.build.ssr) {
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
|
|
383
|
-
if (isDebug) {
|
|
384
|
-
console.log(`[@vitejs/plugin-legacy] targets:`, targets);
|
|
385
|
-
}
|
|
386
|
-
const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
|
|
387
|
-
if (!fileNames) {
|
|
388
|
-
return path.posix.join(config.build.assetsDir, defaultFileName);
|
|
389
|
-
}
|
|
390
|
-
return (chunkInfo) => {
|
|
391
|
-
let fileName = typeof fileNames === "function" ? fileNames(chunkInfo) : fileNames;
|
|
392
|
-
if (fileName.includes("[name]")) {
|
|
393
|
-
fileName = fileName.replace("[name]", "[name]-legacy");
|
|
394
|
-
} else if (nonLeadingHashInFileNameRE.test(fileName)) {
|
|
395
|
-
fileName = fileName.replace(prefixedHashInFileNameRE, "-legacy$&");
|
|
396
|
-
} else {
|
|
397
|
-
fileName = fileName.replace(/(.+?)\.(.+)/, "$1-legacy.$2");
|
|
398
|
-
}
|
|
399
|
-
return fileName;
|
|
400
|
-
};
|
|
401
|
-
};
|
|
402
|
-
const createLegacyOutput = (options2 = {}) => {
|
|
403
|
-
return {
|
|
404
|
-
...options2,
|
|
405
|
-
format: "system",
|
|
406
|
-
entryFileNames: getLegacyOutputFileName(options2.entryFileNames),
|
|
407
|
-
chunkFileNames: getLegacyOutputFileName(options2.chunkFileNames)
|
|
408
|
-
};
|
|
409
|
-
};
|
|
410
|
-
const { rollupOptions } = config.build;
|
|
411
|
-
const { output } = rollupOptions;
|
|
412
|
-
if (Array.isArray(output)) {
|
|
413
|
-
rollupOptions.output = [
|
|
414
|
-
...output.map(createLegacyOutput),
|
|
415
|
-
...genModern ? output : []
|
|
416
|
-
];
|
|
417
|
-
} else {
|
|
418
|
-
rollupOptions.output = [
|
|
419
|
-
createLegacyOutput(output),
|
|
420
|
-
...genModern ? [output || {}] : []
|
|
421
|
-
];
|
|
422
|
-
}
|
|
423
|
-
},
|
|
424
|
-
async renderChunk(raw, chunk, opts, { chunks }) {
|
|
425
|
-
if (config.build.ssr) {
|
|
426
|
-
return null;
|
|
427
|
-
}
|
|
428
|
-
let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
429
|
-
if (chunkFileNameToPolyfills == null) {
|
|
430
|
-
chunkFileNameToPolyfills = /* @__PURE__ */ new Map();
|
|
431
|
-
for (const fileName in chunks) {
|
|
432
|
-
chunkFileNameToPolyfills.set(fileName, {
|
|
433
|
-
modern: /* @__PURE__ */ new Set(),
|
|
434
|
-
legacy: /* @__PURE__ */ new Set()
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills);
|
|
438
|
-
}
|
|
439
|
-
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName);
|
|
440
|
-
if (polyfillsDiscovered == null) {
|
|
441
|
-
throw new Error(
|
|
442
|
-
`Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
if (!isLegacyChunk(chunk, opts)) {
|
|
446
|
-
if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
|
|
447
|
-
await detectPolyfills(
|
|
448
|
-
raw,
|
|
449
|
-
modernTargets,
|
|
450
|
-
assumptions,
|
|
451
|
-
polyfillsDiscovered.modern
|
|
452
|
-
);
|
|
453
|
-
}
|
|
454
|
-
const ms = new MagicString(raw);
|
|
455
|
-
if (genLegacy && chunk.isEntry) {
|
|
456
|
-
ms.prepend(modernChunkLegacyGuard);
|
|
457
|
-
}
|
|
458
|
-
if (raw.includes(legacyEnvVarMarker)) {
|
|
459
|
-
const re = new RegExp(legacyEnvVarMarker, "g");
|
|
460
|
-
let match;
|
|
461
|
-
while (match = re.exec(raw)) {
|
|
462
|
-
ms.overwrite(
|
|
463
|
-
match.index,
|
|
464
|
-
match.index + legacyEnvVarMarker.length,
|
|
465
|
-
`false`
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
if (config.build.sourcemap) {
|
|
470
|
-
return {
|
|
471
|
-
code: ms.toString(),
|
|
472
|
-
map: ms.generateMap({ hires: "boundary" })
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
return {
|
|
476
|
-
code: ms.toString()
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
if (!genLegacy) {
|
|
480
|
-
return null;
|
|
481
|
-
}
|
|
482
|
-
opts.__vite_skip_esbuild__ = true;
|
|
483
|
-
opts.__vite_force_terser__ = true;
|
|
484
|
-
opts.__vite_skip_asset_emit__ = true;
|
|
485
|
-
const needPolyfills = options.polyfills !== false && !Array.isArray(options.polyfills);
|
|
486
|
-
const sourceMaps = !!config.build.sourcemap;
|
|
487
|
-
const babel2 = await loadBabel();
|
|
488
|
-
const result = babel2.transform(raw, {
|
|
489
|
-
babelrc: false,
|
|
490
|
-
configFile: false,
|
|
491
|
-
compact: !!config.build.minify,
|
|
492
|
-
sourceMaps,
|
|
493
|
-
inputSourceMap: void 0,
|
|
494
|
-
assumptions,
|
|
495
|
-
presets: [
|
|
496
|
-
// forcing our plugin to run before preset-env by wrapping it in a
|
|
497
|
-
// preset so we can catch the injected import statements...
|
|
498
|
-
[
|
|
499
|
-
() => ({
|
|
500
|
-
plugins: [
|
|
501
|
-
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
|
|
502
|
-
replaceLegacyEnvBabelPlugin(),
|
|
503
|
-
wrapIIFEBabelPlugin()
|
|
504
|
-
]
|
|
505
|
-
})
|
|
506
|
-
],
|
|
507
|
-
[
|
|
508
|
-
(await import('@babel/preset-env')).default,
|
|
509
|
-
createBabelPresetEnvOptions(targets, { needPolyfills })
|
|
510
|
-
]
|
|
511
|
-
]
|
|
512
|
-
});
|
|
513
|
-
if (result) return { code: result.code, map: result.map };
|
|
514
|
-
return null;
|
|
515
|
-
},
|
|
516
|
-
transformIndexHtml(html, { chunk }) {
|
|
517
|
-
if (config.build.ssr) return;
|
|
518
|
-
if (!chunk) return;
|
|
519
|
-
if (chunk.fileName.includes("-legacy")) {
|
|
520
|
-
facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
|
|
521
|
-
if (genModern) {
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
if (!genModern) {
|
|
526
|
-
html = html.replace(/<script type="module".*?<\/script>/g, "");
|
|
527
|
-
}
|
|
528
|
-
const tags = [];
|
|
529
|
-
const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
|
|
530
|
-
if (genModern) {
|
|
531
|
-
const modernPolyfillFilename = facadeToModernPolyfillMap.get(
|
|
532
|
-
chunk.facadeModuleId
|
|
533
|
-
);
|
|
534
|
-
if (modernPolyfillFilename) {
|
|
535
|
-
tags.push({
|
|
536
|
-
tag: "script",
|
|
537
|
-
attrs: {
|
|
538
|
-
type: "module",
|
|
539
|
-
crossorigin: true,
|
|
540
|
-
src: toAssetPathFromHtml(
|
|
541
|
-
modernPolyfillFilename,
|
|
542
|
-
chunk.facadeModuleId,
|
|
543
|
-
config
|
|
544
|
-
)
|
|
545
|
-
}
|
|
546
|
-
});
|
|
547
|
-
} else if (modernPolyfills.size) {
|
|
548
|
-
throw new Error(
|
|
549
|
-
`No corresponding modern polyfill chunk found for ${htmlFilename}`
|
|
550
|
-
);
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
if (!genLegacy) {
|
|
554
|
-
return { html, tags };
|
|
555
|
-
}
|
|
556
|
-
if (genModern) {
|
|
557
|
-
tags.push({
|
|
558
|
-
tag: "script",
|
|
559
|
-
attrs: { nomodule: genModern },
|
|
560
|
-
children: safari10NoModuleFix,
|
|
561
|
-
injectTo: "body"
|
|
562
|
-
});
|
|
563
|
-
}
|
|
564
|
-
const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(
|
|
565
|
-
chunk.facadeModuleId
|
|
566
|
-
);
|
|
567
|
-
if (legacyPolyfillFilename) {
|
|
568
|
-
tags.push({
|
|
569
|
-
tag: "script",
|
|
570
|
-
attrs: {
|
|
571
|
-
nomodule: genModern,
|
|
572
|
-
crossorigin: true,
|
|
573
|
-
id: legacyPolyfillId,
|
|
574
|
-
src: toAssetPathFromHtml(
|
|
575
|
-
legacyPolyfillFilename,
|
|
576
|
-
chunk.facadeModuleId,
|
|
577
|
-
config
|
|
578
|
-
)
|
|
579
|
-
},
|
|
580
|
-
injectTo: "body"
|
|
581
|
-
});
|
|
582
|
-
} else if (legacyPolyfills.size) {
|
|
583
|
-
throw new Error(
|
|
584
|
-
`No corresponding legacy polyfill chunk found for ${htmlFilename}`
|
|
585
|
-
);
|
|
586
|
-
}
|
|
587
|
-
const legacyEntryFilename = facadeToLegacyChunkMap.get(
|
|
588
|
-
chunk.facadeModuleId
|
|
589
|
-
);
|
|
590
|
-
if (legacyEntryFilename) {
|
|
591
|
-
tags.push({
|
|
592
|
-
tag: "script",
|
|
593
|
-
attrs: {
|
|
594
|
-
nomodule: genModern,
|
|
595
|
-
crossorigin: true,
|
|
596
|
-
// we set the entry path on the element as an attribute so that the
|
|
597
|
-
// script content will stay consistent - which allows using a constant
|
|
598
|
-
// hash value for CSP.
|
|
599
|
-
id: legacyEntryId,
|
|
600
|
-
"data-src": toAssetPathFromHtml(
|
|
601
|
-
legacyEntryFilename,
|
|
602
|
-
chunk.facadeModuleId,
|
|
603
|
-
config
|
|
604
|
-
)
|
|
605
|
-
},
|
|
606
|
-
children: systemJSInlineCode,
|
|
607
|
-
injectTo: "body"
|
|
608
|
-
});
|
|
609
|
-
} else {
|
|
610
|
-
throw new Error(
|
|
611
|
-
`No corresponding legacy entry chunk found for ${htmlFilename}`
|
|
612
|
-
);
|
|
613
|
-
}
|
|
614
|
-
if (legacyPolyfillFilename && legacyEntryFilename && genModern) {
|
|
615
|
-
tags.push({
|
|
616
|
-
tag: "script",
|
|
617
|
-
attrs: { type: "module" },
|
|
618
|
-
children: detectModernBrowserCode,
|
|
619
|
-
injectTo: "head"
|
|
620
|
-
});
|
|
621
|
-
tags.push({
|
|
622
|
-
tag: "script",
|
|
623
|
-
attrs: { type: "module" },
|
|
624
|
-
children: dynamicFallbackInlineCode,
|
|
625
|
-
injectTo: "head"
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
return {
|
|
629
|
-
html,
|
|
630
|
-
tags
|
|
631
|
-
};
|
|
632
|
-
},
|
|
633
|
-
generateBundle(opts, bundle) {
|
|
634
|
-
if (config.build.ssr) {
|
|
635
|
-
return;
|
|
636
|
-
}
|
|
637
|
-
if (isLegacyBundle(bundle, opts) && genModern) {
|
|
638
|
-
for (const name in bundle) {
|
|
639
|
-
if (bundle[name].type === "asset" && !/.+\.map$/.test(name)) {
|
|
640
|
-
delete bundle[name];
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
};
|
|
646
|
-
return [legacyConfigPlugin, legacyGenerateBundlePlugin, legacyPostPlugin];
|
|
647
|
-
}
|
|
648
|
-
async function detectPolyfills(code, targets, assumptions, list) {
|
|
649
|
-
const babel2 = await loadBabel();
|
|
650
|
-
const result = babel2.transform(code, {
|
|
651
|
-
ast: true,
|
|
652
|
-
babelrc: false,
|
|
653
|
-
configFile: false,
|
|
654
|
-
compact: false,
|
|
655
|
-
assumptions,
|
|
656
|
-
presets: [
|
|
657
|
-
[
|
|
658
|
-
(await import('@babel/preset-env')).default,
|
|
659
|
-
createBabelPresetEnvOptions(targets, {})
|
|
660
|
-
]
|
|
661
|
-
]
|
|
662
|
-
});
|
|
663
|
-
for (const node of result.ast.program.body) {
|
|
664
|
-
if (node.type === "ImportDeclaration") {
|
|
665
|
-
const source = node.source.value;
|
|
666
|
-
if (source.startsWith("core-js/") || source.startsWith("regenerator-runtime/")) {
|
|
667
|
-
list.add(source);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
|
|
673
|
-
return {
|
|
674
|
-
targets,
|
|
675
|
-
bugfixes: true,
|
|
676
|
-
loose: false,
|
|
677
|
-
modules: false,
|
|
678
|
-
useBuiltIns: needPolyfills ? "usage" : false,
|
|
679
|
-
corejs: needPolyfills ? {
|
|
680
|
-
version: _require("core-js/package.json").version,
|
|
681
|
-
proposals: false
|
|
682
|
-
} : void 0,
|
|
683
|
-
shippedProposals: true,
|
|
684
|
-
ignoreBrowserslistConfig: true
|
|
685
|
-
};
|
|
686
|
-
}
|
|
687
|
-
async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
|
|
688
|
-
let { minify, assetsDir, sourcemap } = buildOptions;
|
|
689
|
-
minify = minify ? "terser" : false;
|
|
690
|
-
const res = await build({
|
|
691
|
-
mode,
|
|
692
|
-
// so that everything is resolved from here
|
|
693
|
-
root: path.dirname(fileURLToPath(import.meta.url)),
|
|
694
|
-
configFile: false,
|
|
695
|
-
logLevel: "error",
|
|
696
|
-
plugins: [
|
|
697
|
-
polyfillsPlugin(imports, excludeSystemJS),
|
|
698
|
-
prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()
|
|
699
|
-
],
|
|
700
|
-
build: {
|
|
701
|
-
write: false,
|
|
702
|
-
minify,
|
|
703
|
-
assetsDir,
|
|
704
|
-
sourcemap,
|
|
705
|
-
rollupOptions: {
|
|
706
|
-
input: {
|
|
707
|
-
polyfills: polyfillId
|
|
708
|
-
},
|
|
709
|
-
output: {
|
|
710
|
-
format,
|
|
711
|
-
hashCharacters: rollupOutputOptions.hashCharacters,
|
|
712
|
-
entryFileNames: rollupOutputOptions.entryFileNames,
|
|
713
|
-
sourcemapBaseUrl: rollupOutputOptions.sourcemapBaseUrl
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
},
|
|
717
|
-
// Don't run esbuild for transpilation or minification
|
|
718
|
-
// because we don't want to transpile code.
|
|
719
|
-
esbuild: false,
|
|
720
|
-
optimizeDeps: {
|
|
721
|
-
esbuildOptions: {
|
|
722
|
-
// If a value above 'es5' is set, esbuild injects helper functions which uses es2015 features.
|
|
723
|
-
// This limits the input code not to include es2015+ codes.
|
|
724
|
-
// But core-js is the only dependency which includes commonjs code
|
|
725
|
-
// and core-js doesn't include es2015+ codes.
|
|
726
|
-
target: "es5"
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
});
|
|
730
|
-
const _polyfillChunk = Array.isArray(res) ? res[0] : res;
|
|
731
|
-
if (!("output" in _polyfillChunk)) return;
|
|
732
|
-
const polyfillChunk = _polyfillChunk.output.find(
|
|
733
|
-
(chunk) => chunk.type === "chunk" && chunk.isEntry
|
|
734
|
-
);
|
|
735
|
-
for (const key in bundle) {
|
|
736
|
-
const chunk = bundle[key];
|
|
737
|
-
if (chunk.type === "chunk" && chunk.facadeModuleId) {
|
|
738
|
-
facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
bundle[polyfillChunk.fileName] = polyfillChunk;
|
|
742
|
-
if (polyfillChunk.sourcemapFileName) {
|
|
743
|
-
const polyfillChunkMapAsset = _polyfillChunk.output.find(
|
|
744
|
-
(chunk) => chunk.type === "asset" && chunk.fileName === polyfillChunk.sourcemapFileName
|
|
745
|
-
);
|
|
746
|
-
if (polyfillChunkMapAsset) {
|
|
747
|
-
bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset;
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
const polyfillId = "\0vite/legacy-polyfills";
|
|
752
|
-
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
753
|
-
return {
|
|
754
|
-
name: "vite:legacy-polyfills",
|
|
755
|
-
resolveId(id) {
|
|
756
|
-
if (id === polyfillId) {
|
|
757
|
-
return id;
|
|
758
|
-
}
|
|
759
|
-
},
|
|
760
|
-
load(id) {
|
|
761
|
-
if (id === polyfillId) {
|
|
762
|
-
return [...imports].map((i) => `import ${JSON.stringify(i)};`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
};
|
|
766
|
-
}
|
|
767
|
-
function prependModenChunkLegacyGuardPlugin() {
|
|
768
|
-
let sourceMapEnabled;
|
|
769
|
-
return {
|
|
770
|
-
name: "vite:legacy-prepend-moden-chunk-legacy-guard",
|
|
771
|
-
configResolved(config) {
|
|
772
|
-
sourceMapEnabled = !!config.build.sourcemap;
|
|
773
|
-
},
|
|
774
|
-
renderChunk(code) {
|
|
775
|
-
if (!sourceMapEnabled) {
|
|
776
|
-
return modernChunkLegacyGuard + code;
|
|
777
|
-
}
|
|
778
|
-
const ms = new MagicString(code);
|
|
779
|
-
ms.prepend(modernChunkLegacyGuard);
|
|
780
|
-
return {
|
|
781
|
-
code: ms.toString(),
|
|
782
|
-
map: ms.generateMap({ hires: "boundary" })
|
|
783
|
-
};
|
|
784
|
-
}
|
|
785
|
-
};
|
|
786
|
-
}
|
|
787
|
-
function isLegacyChunk(chunk, options) {
|
|
788
|
-
return options.format === "system" && chunk.fileName.includes("-legacy");
|
|
789
|
-
}
|
|
790
|
-
function isLegacyBundle(bundle, options) {
|
|
791
|
-
if (options.format === "system") {
|
|
792
|
-
const entryChunk = Object.values(bundle).find(
|
|
793
|
-
(output) => output.type === "chunk" && output.isEntry
|
|
794
|
-
);
|
|
795
|
-
return !!entryChunk && entryChunk.fileName.includes("-legacy");
|
|
796
|
-
}
|
|
797
|
-
return false;
|
|
798
|
-
}
|
|
799
|
-
function recordAndRemovePolyfillBabelPlugin(polyfills) {
|
|
800
|
-
return ({ types: t }) => ({
|
|
801
|
-
name: "vite-remove-polyfill-import",
|
|
802
|
-
post({ path: path2 }) {
|
|
803
|
-
path2.get("body").forEach((p) => {
|
|
804
|
-
if (t.isImportDeclaration(p.node)) {
|
|
805
|
-
polyfills.add(p.node.source.value);
|
|
806
|
-
p.remove();
|
|
807
|
-
}
|
|
808
|
-
});
|
|
809
|
-
}
|
|
810
|
-
});
|
|
811
|
-
}
|
|
812
|
-
function replaceLegacyEnvBabelPlugin() {
|
|
813
|
-
return ({ types: t }) => ({
|
|
814
|
-
name: "vite-replace-env-legacy",
|
|
815
|
-
visitor: {
|
|
816
|
-
Identifier(path2) {
|
|
817
|
-
if (path2.node.name === legacyEnvVarMarker) {
|
|
818
|
-
path2.replaceWith(t.booleanLiteral(true));
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
});
|
|
823
|
-
}
|
|
824
|
-
function wrapIIFEBabelPlugin() {
|
|
825
|
-
return ({ types: t, template }) => {
|
|
826
|
-
const buildIIFE = template(";(function(){%%body%%})();");
|
|
827
|
-
return {
|
|
828
|
-
name: "vite-wrap-iife",
|
|
829
|
-
post({ path: path2 }) {
|
|
830
|
-
if (!this.isWrapped) {
|
|
831
|
-
this.isWrapped = true;
|
|
832
|
-
path2.replaceWith(t.program(buildIIFE({ body: path2.node.body })));
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
};
|
|
836
|
-
};
|
|
837
|
-
}
|
|
838
|
-
const hash = (
|
|
839
|
-
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
|
|
840
|
-
crypto.hash ?? ((algorithm, data, outputEncoding) => crypto.createHash(algorithm).update(data).digest(outputEncoding))
|
|
841
|
-
);
|
|
842
|
-
const cspHashes = [
|
|
843
|
-
safari10NoModuleFix,
|
|
844
|
-
systemJSInlineCode,
|
|
845
|
-
detectModernBrowserCode,
|
|
846
|
-
dynamicFallbackInlineCode
|
|
847
|
-
].map((i) => hash("sha256", i, "base64"));
|
|
848
|
-
|
|
849
|
-
export { cspHashes, viteLegacyPlugin as default, detectPolyfills };
|