@vitejs/plugin-legacy 7.2.1 → 8.0.0-beta.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/dist/index.js +274 -263
- package/package.json +20 -17
- package/dist/vendor/rolldown-vite/index.js +0 -608
- package/dist/vendor/rolldown-vite/vendor/rolldown-vite/index.js +0 -607
package/dist/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
import path from "node:path";
|
|
2
|
+
import path$1 from "node:path";
|
|
3
3
|
import crypto from "node:crypto";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import * as vite from "vite";
|
|
6
5
|
import { build, normalizePath } from "vite";
|
|
7
6
|
import MagicString from "magic-string";
|
|
8
7
|
import browserslist from "browserslist";
|
|
@@ -14,16 +13,18 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
14
13
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
15
14
|
var __getProtoOf = Object.getPrototypeOf;
|
|
16
15
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
-
var
|
|
18
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
19
|
-
};
|
|
16
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
20
17
|
var __copyProps = (to, from, except, desc) => {
|
|
21
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
20
|
+
key = keys[i];
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
22
|
+
__defProp(to, key, {
|
|
23
|
+
get: ((k) => from[k]).bind(null, key),
|
|
24
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
27
28
|
}
|
|
28
29
|
return to;
|
|
29
30
|
};
|
|
@@ -34,7 +35,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
34
35
|
|
|
35
36
|
//#endregion
|
|
36
37
|
//#region ../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js
|
|
37
|
-
var require_picocolors = /* @__PURE__ */
|
|
38
|
+
var require_picocolors = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
38
39
|
let p = process || {}, argv = p.argv || [], env = p.env || {};
|
|
39
40
|
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
41
|
let formatter = (open, close, replace = open) => (input) => {
|
|
@@ -99,10 +100,11 @@ var require_picocolors = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/
|
|
|
99
100
|
};
|
|
100
101
|
module.exports = createColors();
|
|
101
102
|
module.exports.createColors = createColors;
|
|
102
|
-
})
|
|
103
|
+
}));
|
|
103
104
|
|
|
104
105
|
//#endregion
|
|
105
106
|
//#region src/snippets.ts
|
|
107
|
+
var import_picocolors = /* @__PURE__ */ __toESM(require_picocolors(), 1);
|
|
106
108
|
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()}}();`;
|
|
107
109
|
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
108
110
|
const legacyEntryId = "vite-legacy-entry";
|
|
@@ -115,7 +117,6 @@ const modernChunkLegacyGuard = `export function __vite_legacy_guard(){${detectMo
|
|
|
115
117
|
|
|
116
118
|
//#endregion
|
|
117
119
|
//#region src/index.ts
|
|
118
|
-
var import_picocolors = /* @__PURE__ */ __toESM(require_picocolors(), 1);
|
|
119
120
|
let babel;
|
|
120
121
|
async function loadBabel() {
|
|
121
122
|
return babel ??= import("@babel/core");
|
|
@@ -140,7 +141,7 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
|
|
|
140
141
|
else return joinUrlSegments(config.decodedBase, filename);
|
|
141
142
|
}
|
|
142
143
|
function getBaseInHTML(urlRelativePath, config) {
|
|
143
|
-
return config.base === "./" || config.base === "" ? path.posix.join(path.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
|
|
144
|
+
return config.base === "./" || config.base === "" ? path$1.posix.join(path$1.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
|
|
144
145
|
}
|
|
145
146
|
function joinUrlSegments(a, b) {
|
|
146
147
|
if (!a || !b) return a || b || "";
|
|
@@ -149,8 +150,8 @@ function joinUrlSegments(a, b) {
|
|
|
149
150
|
return a + b;
|
|
150
151
|
}
|
|
151
152
|
function toAssetPathFromHtml(filename, htmlPath, config) {
|
|
152
|
-
const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
|
|
153
|
-
const toRelative = (filename
|
|
153
|
+
const relativeUrlPath = normalizePath(path$1.relative(config.root, htmlPath));
|
|
154
|
+
const toRelative = (filename, _hostId) => getBaseInHTML(relativeUrlPath, config) + filename;
|
|
154
155
|
return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
|
|
155
156
|
}
|
|
156
157
|
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
@@ -165,11 +166,8 @@ const modernTargetsEsbuild = [
|
|
|
165
166
|
"safari12"
|
|
166
167
|
];
|
|
167
168
|
const modernTargetsBabel = "edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12";
|
|
169
|
+
const outputOptionsForLegacyChunks = /* @__PURE__ */ new WeakSet();
|
|
168
170
|
function viteLegacyPlugin(options = {}) {
|
|
169
|
-
if ("rolldownVersion" in vite) {
|
|
170
|
-
const { default: viteLegacyPluginForRolldownVite } = _require("#legacy-for-rolldown-vite");
|
|
171
|
-
return viteLegacyPluginForRolldownVite(options);
|
|
172
|
-
}
|
|
173
171
|
let config;
|
|
174
172
|
let targets;
|
|
175
173
|
const modernTargets = options.modernTargets || modernTargetsBabel;
|
|
@@ -201,247 +199,255 @@ function viteLegacyPlugin(options = {}) {
|
|
|
201
199
|
let overriddenBuildTarget = false;
|
|
202
200
|
let overriddenBuildTargetOnlyModern = false;
|
|
203
201
|
let overriddenDefaultModernTargets = false;
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
202
|
+
return [
|
|
203
|
+
{
|
|
204
|
+
name: "vite:legacy-config",
|
|
205
|
+
async config(config, env) {
|
|
206
|
+
if (env.command === "build" && !config.build?.ssr) {
|
|
207
|
+
if (!config.build) config.build = {};
|
|
208
|
+
if (genLegacy && !config.build.cssTarget) config.build.cssTarget = "chrome61";
|
|
209
|
+
if (genLegacy) {
|
|
210
|
+
overriddenBuildTarget = config.build.target !== void 0;
|
|
211
|
+
overriddenDefaultModernTargets = options.modernTargets !== void 0;
|
|
212
|
+
} else overriddenBuildTargetOnlyModern = config.build.target !== void 0;
|
|
213
|
+
if (options.modernTargets) {
|
|
214
|
+
const { default: browserslistToEsbuild } = await import("browserslist-to-esbuild");
|
|
215
|
+
config.build.target = browserslistToEsbuild(options.modernTargets);
|
|
216
|
+
} else config.build.target = modernTargetsEsbuild;
|
|
217
|
+
}
|
|
218
|
+
return { define: { "import.meta.env.LEGACY": env.command === "serve" || config.build?.ssr ? false : legacyEnvVarMarker } };
|
|
219
|
+
},
|
|
220
|
+
configResolved(config) {
|
|
221
|
+
if (overriddenBuildTarget) config.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.`));
|
|
222
|
+
if (overriddenBuildTargetOnlyModern) config.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.`));
|
|
223
|
+
if (overriddenDefaultModernTargets) config.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.`));
|
|
224
|
+
if (config.isWorker) config.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.`));
|
|
218
225
|
}
|
|
219
|
-
return { define: { "import.meta.env.LEGACY": env$1.command === "serve" || config$1.build?.ssr ? false : legacyEnvVarMarker } };
|
|
220
226
|
},
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
for (const {
|
|
237
|
-
if (
|
|
238
|
-
if (
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (!genLegacy) return;
|
|
243
|
-
for (const { legacy } of chunkFileNameToPolyfills.values()) legacy.forEach((p$1) => legacyPolyfills.add(p$1));
|
|
244
|
-
if (options.polyfills !== false) await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, assumptions, legacyPolyfills);
|
|
245
|
-
if (legacyPolyfills.size || !options.externalSystemJS) {
|
|
246
|
-
if (isDebug) console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
247
|
-
await buildPolyfillChunk(config.mode, legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
227
|
+
{
|
|
228
|
+
name: "vite:legacy-generate-polyfill-chunk",
|
|
229
|
+
apply: "build",
|
|
230
|
+
async generateBundle(opts, bundle) {
|
|
231
|
+
if (config.build.ssr) return;
|
|
232
|
+
const chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
233
|
+
if (chunkFileNameToPolyfills == null) throw new Error("Internal @vitejs/plugin-legacy error: discovered polyfills should exist");
|
|
234
|
+
if (!isLegacyBundle(bundle)) {
|
|
235
|
+
for (const { modern } of chunkFileNameToPolyfills.values()) modern.forEach((p) => modernPolyfills.add(p));
|
|
236
|
+
if (!modernPolyfills.size) return;
|
|
237
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
238
|
+
await buildPolyfillChunk(this, config.mode, modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true, genLegacy);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (!genLegacy) return;
|
|
242
|
+
for (const { legacy } of chunkFileNameToPolyfills.values()) legacy.forEach((p) => legacyPolyfills.add(p));
|
|
243
|
+
if (options.polyfills !== false) await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, assumptions, legacyPolyfills);
|
|
244
|
+
if (legacyPolyfills.size || !options.externalSystemJS) {
|
|
245
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
246
|
+
await buildPolyfillChunk(this, config.mode, legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
247
|
+
}
|
|
248
248
|
}
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
const legacyPostPlugin = {
|
|
252
|
-
name: "vite:legacy-post-process",
|
|
253
|
-
enforce: "post",
|
|
254
|
-
apply: "build",
|
|
255
|
-
renderStart(opts) {
|
|
256
|
-
outputToChunkFileNameToPolyfills.set(opts, null);
|
|
257
249
|
},
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
250
|
+
{
|
|
251
|
+
name: "vite:legacy-post-process",
|
|
252
|
+
enforce: "post",
|
|
253
|
+
apply: "build",
|
|
254
|
+
renderStart(opts) {
|
|
255
|
+
outputToChunkFileNameToPolyfills.set(opts, null);
|
|
256
|
+
},
|
|
257
|
+
configResolved(_config) {
|
|
258
|
+
if (_config.build.lib) throw new Error("@vitejs/plugin-legacy does not support library mode.");
|
|
259
|
+
config = _config;
|
|
260
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
|
|
261
|
+
if (!genLegacy || config.build.ssr) return;
|
|
262
|
+
targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
|
|
263
|
+
if (isDebug) console.log(`[@vitejs/plugin-legacy] targets:`, targets);
|
|
264
|
+
const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
|
|
265
|
+
if (!fileNames) return path$1.posix.join(config.build.assetsDir, defaultFileName);
|
|
266
|
+
return (chunkInfo) => {
|
|
267
|
+
let fileName = typeof fileNames === "function" ? fileNames(chunkInfo) : fileNames;
|
|
268
|
+
if (fileName.includes("[name]")) fileName = fileName.replace("[name]", "[name]-legacy");
|
|
269
|
+
else if (nonLeadingHashInFileNameRE.test(fileName)) fileName = fileName.replace(prefixedHashInFileNameRE, "-legacy$&");
|
|
270
|
+
else fileName = fileName.replace(/(.+?)\.(.+)/, "$1-legacy.$2");
|
|
271
|
+
return fileName;
|
|
272
|
+
};
|
|
273
273
|
};
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
274
|
+
const createLegacyOutput = (options = {}) => {
|
|
275
|
+
return {
|
|
276
|
+
...options,
|
|
277
|
+
format: "esm",
|
|
278
|
+
entryFileNames: getLegacyOutputFileName(options.entryFileNames),
|
|
279
|
+
chunkFileNames: getLegacyOutputFileName(options.chunkFileNames),
|
|
280
|
+
minify: false
|
|
281
|
+
};
|
|
281
282
|
};
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
283
|
+
const { rollupOptions } = config.build;
|
|
284
|
+
const { output } = rollupOptions;
|
|
285
|
+
if (Array.isArray(output)) rollupOptions.output = [...output.map(createLegacyOutput), ...genModern ? output : []];
|
|
286
|
+
else rollupOptions.output = [createLegacyOutput(output), ...genModern ? [output || {}] : []];
|
|
287
|
+
_config.isOutputOptionsForLegacyChunks = (opts) => outputOptionsForLegacyChunks.has(opts);
|
|
288
|
+
},
|
|
289
|
+
async renderChunk(raw, chunk, opts, { chunks }) {
|
|
290
|
+
if (config.build.ssr) return null;
|
|
291
|
+
let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts);
|
|
292
|
+
if (chunkFileNameToPolyfills == null) {
|
|
293
|
+
chunkFileNameToPolyfills = /* @__PURE__ */ new Map();
|
|
294
|
+
for (const fileName in chunks) chunkFileNameToPolyfills.set(fileName, {
|
|
295
|
+
modern: /* @__PURE__ */ new Set(),
|
|
296
|
+
legacy: /* @__PURE__ */ new Set()
|
|
297
|
+
});
|
|
298
|
+
outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills);
|
|
299
|
+
}
|
|
300
|
+
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName);
|
|
301
|
+
if (polyfillsDiscovered == null) throw new Error(`Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`);
|
|
302
|
+
if (!isLegacyChunk(chunk)) {
|
|
303
|
+
if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) await detectPolyfills(raw, modernTargets, assumptions, polyfillsDiscovered.modern);
|
|
304
|
+
const ms = new MagicString(raw);
|
|
305
|
+
if (genLegacy && chunk.isEntry) ms.prepend(modernChunkLegacyGuard);
|
|
306
|
+
if (raw.includes(legacyEnvVarMarker)) {
|
|
307
|
+
const re = new RegExp(legacyEnvVarMarker, "g");
|
|
308
|
+
let match;
|
|
309
|
+
while (match = re.exec(raw)) ms.overwrite(match.index, match.index + legacyEnvVarMarker.length, `false`);
|
|
310
|
+
}
|
|
311
|
+
if (config.build.sourcemap) return {
|
|
312
|
+
code: ms.toString(),
|
|
313
|
+
map: ms.generateMap({ hires: "boundary" })
|
|
314
|
+
};
|
|
315
|
+
return { code: ms.toString() };
|
|
316
|
+
}
|
|
317
|
+
if (!genLegacy) return null;
|
|
318
|
+
outputOptionsForLegacyChunks.add(opts);
|
|
319
|
+
const needPolyfills = options.polyfills !== false && !Array.isArray(options.polyfills);
|
|
320
|
+
const sourceMaps = !!config.build.sourcemap;
|
|
321
|
+
const babel = await loadBabel();
|
|
322
|
+
const resultSystem = babel.transform(raw, {
|
|
323
|
+
babelrc: false,
|
|
324
|
+
configFile: false,
|
|
325
|
+
ast: true,
|
|
326
|
+
code: false,
|
|
327
|
+
sourceMaps,
|
|
328
|
+
plugins: [(await import("@babel/plugin-transform-dynamic-import")).default, (await import("@babel/plugin-transform-modules-systemjs")).default]
|
|
296
329
|
});
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
330
|
+
const babelTransformOptions = {
|
|
331
|
+
babelrc: false,
|
|
332
|
+
configFile: false,
|
|
333
|
+
cloneInputAst: false,
|
|
334
|
+
compact: !!config.build.minify,
|
|
335
|
+
sourceMaps,
|
|
336
|
+
inputSourceMap: void 0,
|
|
337
|
+
targets,
|
|
338
|
+
assumptions,
|
|
339
|
+
browserslistConfigFile: false,
|
|
340
|
+
presets: [[() => ({ plugins: [
|
|
341
|
+
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
|
|
342
|
+
replaceLegacyEnvBabelPlugin(),
|
|
343
|
+
wrapIIFEBabelPlugin()
|
|
344
|
+
] })], [(await import("@babel/preset-env")).default, {
|
|
345
|
+
bugfixes: true,
|
|
346
|
+
modules: false,
|
|
347
|
+
useBuiltIns: needPolyfills ? "usage" : false,
|
|
348
|
+
corejs: needPolyfills ? {
|
|
349
|
+
version: _require("core-js/package.json").version,
|
|
350
|
+
proposals: false
|
|
351
|
+
} : void 0,
|
|
352
|
+
shippedProposals: true
|
|
353
|
+
}]]
|
|
354
|
+
};
|
|
355
|
+
let result;
|
|
356
|
+
if (resultSystem) result = babel.transformFromAstSync(resultSystem.ast, void 0, babelTransformOptions);
|
|
357
|
+
else result = babel.transform(raw, babelTransformOptions);
|
|
358
|
+
if (result) return {
|
|
359
|
+
code: result.code,
|
|
360
|
+
map: result.map
|
|
361
|
+
};
|
|
362
|
+
return null;
|
|
363
|
+
},
|
|
364
|
+
transformIndexHtml(html, { chunk }) {
|
|
365
|
+
if (config.build.ssr) return;
|
|
366
|
+
if (!chunk) return;
|
|
367
|
+
if (chunk.fileName.includes("-legacy")) {
|
|
368
|
+
facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
|
|
369
|
+
if (genModern) return;
|
|
370
|
+
}
|
|
371
|
+
if (!genModern) html = html.replace(/<script type="module".*?<\/script>/g, "");
|
|
372
|
+
const tags = [];
|
|
373
|
+
const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
|
|
374
|
+
if (genModern) {
|
|
375
|
+
const modernPolyfillFilename = facadeToModernPolyfillMap.get(chunk.facadeModuleId);
|
|
376
|
+
if (modernPolyfillFilename) tags.push({
|
|
377
|
+
tag: "script",
|
|
378
|
+
attrs: {
|
|
379
|
+
type: "module",
|
|
380
|
+
crossorigin: true,
|
|
381
|
+
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
else if (modernPolyfills.size) throw new Error(`No corresponding modern polyfill chunk found for ${htmlFilename}`);
|
|
309
385
|
}
|
|
310
|
-
if (
|
|
311
|
-
|
|
312
|
-
|
|
386
|
+
if (!genLegacy) return {
|
|
387
|
+
html,
|
|
388
|
+
tags
|
|
313
389
|
};
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
if (!genLegacy) return null;
|
|
317
|
-
opts.__vite_skip_esbuild__ = true;
|
|
318
|
-
opts.__vite_force_terser__ = true;
|
|
319
|
-
opts.__vite_skip_asset_emit__ = true;
|
|
320
|
-
const needPolyfills = options.polyfills !== false && !Array.isArray(options.polyfills);
|
|
321
|
-
const sourceMaps = !!config.build.sourcemap;
|
|
322
|
-
const babel$1 = await loadBabel();
|
|
323
|
-
const result = babel$1.transform(raw, {
|
|
324
|
-
babelrc: false,
|
|
325
|
-
configFile: false,
|
|
326
|
-
compact: !!config.build.minify,
|
|
327
|
-
sourceMaps,
|
|
328
|
-
inputSourceMap: void 0,
|
|
329
|
-
targets,
|
|
330
|
-
assumptions,
|
|
331
|
-
browserslistConfigFile: false,
|
|
332
|
-
presets: [[() => ({ plugins: [
|
|
333
|
-
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
|
|
334
|
-
replaceLegacyEnvBabelPlugin(),
|
|
335
|
-
wrapIIFEBabelPlugin()
|
|
336
|
-
] })], [(await import("@babel/preset-env")).default, {
|
|
337
|
-
bugfixes: true,
|
|
338
|
-
modules: false,
|
|
339
|
-
useBuiltIns: needPolyfills ? "usage" : false,
|
|
340
|
-
corejs: needPolyfills ? {
|
|
341
|
-
version: _require("core-js/package.json").version,
|
|
342
|
-
proposals: false
|
|
343
|
-
} : void 0,
|
|
344
|
-
shippedProposals: true
|
|
345
|
-
}]]
|
|
346
|
-
});
|
|
347
|
-
if (result) return {
|
|
348
|
-
code: result.code,
|
|
349
|
-
map: result.map
|
|
350
|
-
};
|
|
351
|
-
return null;
|
|
352
|
-
},
|
|
353
|
-
transformIndexHtml(html, { chunk }) {
|
|
354
|
-
if (config.build.ssr) return;
|
|
355
|
-
if (!chunk) return;
|
|
356
|
-
if (chunk.fileName.includes("-legacy")) {
|
|
357
|
-
facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
|
|
358
|
-
if (genModern) return;
|
|
359
|
-
}
|
|
360
|
-
if (!genModern) html = html.replace(/<script type="module".*?<\/script>/g, "");
|
|
361
|
-
const tags = [];
|
|
362
|
-
const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
|
|
363
|
-
if (genModern) {
|
|
364
|
-
const modernPolyfillFilename = facadeToModernPolyfillMap.get(chunk.facadeModuleId);
|
|
365
|
-
if (modernPolyfillFilename) tags.push({
|
|
390
|
+
if (genModern) tags.push({
|
|
366
391
|
tag: "script",
|
|
367
|
-
attrs: {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
371
|
-
}
|
|
392
|
+
attrs: { nomodule: genModern },
|
|
393
|
+
children: safari10NoModuleFix,
|
|
394
|
+
injectTo: "body"
|
|
372
395
|
});
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
if (!genLegacy) return {
|
|
376
|
-
html,
|
|
377
|
-
tags
|
|
378
|
-
};
|
|
379
|
-
if (genModern) tags.push({
|
|
380
|
-
tag: "script",
|
|
381
|
-
attrs: { nomodule: genModern },
|
|
382
|
-
children: safari10NoModuleFix,
|
|
383
|
-
injectTo: "body"
|
|
384
|
-
});
|
|
385
|
-
const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(chunk.facadeModuleId);
|
|
386
|
-
if (legacyPolyfillFilename) tags.push({
|
|
387
|
-
tag: "script",
|
|
388
|
-
attrs: {
|
|
389
|
-
nomodule: genModern,
|
|
390
|
-
crossorigin: true,
|
|
391
|
-
id: legacyPolyfillId,
|
|
392
|
-
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
393
|
-
},
|
|
394
|
-
injectTo: "body"
|
|
395
|
-
});
|
|
396
|
-
else if (legacyPolyfills.size) throw new Error(`No corresponding legacy polyfill chunk found for ${htmlFilename}`);
|
|
397
|
-
const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
|
|
398
|
-
if (legacyEntryFilename) tags.push({
|
|
399
|
-
tag: "script",
|
|
400
|
-
attrs: {
|
|
401
|
-
nomodule: genModern,
|
|
402
|
-
crossorigin: true,
|
|
403
|
-
id: legacyEntryId,
|
|
404
|
-
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
405
|
-
},
|
|
406
|
-
children: systemJSInlineCode,
|
|
407
|
-
injectTo: "body"
|
|
408
|
-
});
|
|
409
|
-
else throw new Error(`No corresponding legacy entry chunk found for ${htmlFilename}`);
|
|
410
|
-
if (legacyPolyfillFilename && legacyEntryFilename && genModern) {
|
|
411
|
-
tags.push({
|
|
396
|
+
const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(chunk.facadeModuleId);
|
|
397
|
+
if (legacyPolyfillFilename) tags.push({
|
|
412
398
|
tag: "script",
|
|
413
|
-
attrs: {
|
|
414
|
-
|
|
415
|
-
|
|
399
|
+
attrs: {
|
|
400
|
+
nomodule: genModern,
|
|
401
|
+
crossorigin: true,
|
|
402
|
+
id: legacyPolyfillId,
|
|
403
|
+
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
404
|
+
},
|
|
405
|
+
injectTo: "body"
|
|
416
406
|
});
|
|
417
|
-
|
|
407
|
+
else if (legacyPolyfills.size) throw new Error(`No corresponding legacy polyfill chunk found for ${htmlFilename}`);
|
|
408
|
+
const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
|
|
409
|
+
if (legacyEntryFilename) tags.push({
|
|
418
410
|
tag: "script",
|
|
419
|
-
attrs: {
|
|
420
|
-
|
|
421
|
-
|
|
411
|
+
attrs: {
|
|
412
|
+
nomodule: genModern,
|
|
413
|
+
crossorigin: true,
|
|
414
|
+
id: legacyEntryId,
|
|
415
|
+
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
416
|
+
},
|
|
417
|
+
children: systemJSInlineCode,
|
|
418
|
+
injectTo: "body"
|
|
422
419
|
});
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
420
|
+
else throw new Error(`No corresponding legacy entry chunk found for ${htmlFilename}`);
|
|
421
|
+
if (legacyPolyfillFilename && legacyEntryFilename && genModern) {
|
|
422
|
+
tags.push({
|
|
423
|
+
tag: "script",
|
|
424
|
+
attrs: { type: "module" },
|
|
425
|
+
children: detectModernBrowserCode,
|
|
426
|
+
injectTo: "head"
|
|
427
|
+
});
|
|
428
|
+
tags.push({
|
|
429
|
+
tag: "script",
|
|
430
|
+
attrs: { type: "module" },
|
|
431
|
+
children: dynamicFallbackInlineCode,
|
|
432
|
+
injectTo: "head"
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
html,
|
|
437
|
+
tags
|
|
438
|
+
};
|
|
439
|
+
},
|
|
440
|
+
generateBundle(_opts, bundle) {
|
|
441
|
+
if (config.build.ssr) return;
|
|
442
|
+
if (isLegacyBundle(bundle) && genModern) {
|
|
443
|
+
for (const name in bundle) if (bundle[name].type === "asset" && !name.endsWith(".map") && !name.includes("-legacy")) delete bundle[name];
|
|
444
|
+
}
|
|
433
445
|
}
|
|
434
446
|
}
|
|
435
|
-
};
|
|
436
|
-
return [
|
|
437
|
-
legacyConfigPlugin,
|
|
438
|
-
legacyGenerateBundlePlugin,
|
|
439
|
-
legacyPostPlugin
|
|
440
447
|
];
|
|
441
448
|
}
|
|
442
449
|
async function detectPolyfills(code, targets, assumptions, list) {
|
|
443
|
-
const
|
|
444
|
-
const result = babel$1.transform(code, {
|
|
450
|
+
const result = (await loadBabel()).transform(code, {
|
|
445
451
|
ast: true,
|
|
446
452
|
code: false,
|
|
447
453
|
babelrc: false,
|
|
@@ -461,12 +467,12 @@ async function detectPolyfills(code, targets, assumptions, list) {
|
|
|
461
467
|
if (source.startsWith("core-js/") || source.startsWith("regenerator-runtime/")) list.add(source);
|
|
462
468
|
}
|
|
463
469
|
}
|
|
464
|
-
async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
|
|
470
|
+
async function buildPolyfillChunk(ctx, mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS, prependModenChunkLegacyGuard) {
|
|
465
471
|
let { minify, assetsDir, sourcemap } = buildOptions;
|
|
466
472
|
minify = minify ? "terser" : false;
|
|
467
473
|
const res = await build({
|
|
468
474
|
mode,
|
|
469
|
-
root: path.dirname(fileURLToPath(import.meta.url)),
|
|
475
|
+
root: path$1.dirname(fileURLToPath(import.meta.url)),
|
|
470
476
|
configFile: false,
|
|
471
477
|
logLevel: "error",
|
|
472
478
|
plugins: [polyfillsPlugin(imports, excludeSystemJS), prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin()],
|
|
@@ -495,10 +501,18 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
|
|
|
495
501
|
const chunk = bundle[key];
|
|
496
502
|
if (chunk.type === "chunk" && chunk.facadeModuleId) facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
|
|
497
503
|
}
|
|
498
|
-
|
|
504
|
+
ctx.emitFile({
|
|
505
|
+
type: "asset",
|
|
506
|
+
fileName: polyfillChunk.fileName,
|
|
507
|
+
source: polyfillChunk.code
|
|
508
|
+
});
|
|
499
509
|
if (polyfillChunk.sourcemapFileName) {
|
|
500
510
|
const polyfillChunkMapAsset = _polyfillChunk.output.find((chunk) => chunk.type === "asset" && chunk.fileName === polyfillChunk.sourcemapFileName);
|
|
501
|
-
if (polyfillChunkMapAsset)
|
|
511
|
+
if (polyfillChunkMapAsset) ctx.emitFile({
|
|
512
|
+
type: "asset",
|
|
513
|
+
fileName: polyfillChunkMapAsset.fileName,
|
|
514
|
+
source: polyfillChunkMapAsset.source
|
|
515
|
+
});
|
|
502
516
|
}
|
|
503
517
|
}
|
|
504
518
|
const polyfillId = "\0vite/legacy-polyfills";
|
|
@@ -531,24 +545,21 @@ function prependModenChunkLegacyGuardPlugin() {
|
|
|
531
545
|
}
|
|
532
546
|
};
|
|
533
547
|
}
|
|
534
|
-
function isLegacyChunk(chunk
|
|
535
|
-
return
|
|
548
|
+
function isLegacyChunk(chunk) {
|
|
549
|
+
return chunk.fileName.includes("-legacy");
|
|
536
550
|
}
|
|
537
|
-
function isLegacyBundle(bundle
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
return !!entryChunk && entryChunk.fileName.includes("-legacy");
|
|
541
|
-
}
|
|
542
|
-
return false;
|
|
551
|
+
function isLegacyBundle(bundle) {
|
|
552
|
+
const entryChunk = Object.values(bundle).find((output) => output.type === "chunk" && output.isEntry);
|
|
553
|
+
return !!entryChunk && entryChunk.fileName.includes("-legacy");
|
|
543
554
|
}
|
|
544
555
|
function recordAndRemovePolyfillBabelPlugin(polyfills) {
|
|
545
556
|
return ({ types: t }) => ({
|
|
546
557
|
name: "vite-remove-polyfill-import",
|
|
547
|
-
post({ path
|
|
548
|
-
path
|
|
549
|
-
if (t.isImportDeclaration(p
|
|
550
|
-
polyfills.add(p
|
|
551
|
-
p
|
|
558
|
+
post({ path }) {
|
|
559
|
+
path.get("body").forEach((p) => {
|
|
560
|
+
if (t.isImportDeclaration(p.node)) {
|
|
561
|
+
polyfills.add(p.node.source.value);
|
|
562
|
+
p.remove();
|
|
552
563
|
}
|
|
553
564
|
});
|
|
554
565
|
}
|
|
@@ -557,8 +568,8 @@ function recordAndRemovePolyfillBabelPlugin(polyfills) {
|
|
|
557
568
|
function replaceLegacyEnvBabelPlugin() {
|
|
558
569
|
return ({ types: t }) => ({
|
|
559
570
|
name: "vite-replace-env-legacy",
|
|
560
|
-
visitor: { Identifier(path
|
|
561
|
-
if (path
|
|
571
|
+
visitor: { Identifier(path) {
|
|
572
|
+
if (path.node.name === legacyEnvVarMarker) path.replaceWith(t.booleanLiteral(true));
|
|
562
573
|
} }
|
|
563
574
|
});
|
|
564
575
|
}
|
|
@@ -567,10 +578,10 @@ function wrapIIFEBabelPlugin() {
|
|
|
567
578
|
const buildIIFE = template(";(function(){%%body%%})();");
|
|
568
579
|
return {
|
|
569
580
|
name: "vite-wrap-iife",
|
|
570
|
-
post({ path
|
|
581
|
+
post({ path }) {
|
|
571
582
|
if (!this.isWrapped) {
|
|
572
583
|
this.isWrapped = true;
|
|
573
|
-
path
|
|
584
|
+
path.replaceWith(t.program(buildIIFE({ body: path.node.body })));
|
|
574
585
|
}
|
|
575
586
|
}
|
|
576
587
|
};
|