@vitejs/plugin-legacy 2.0.0-alpha.1 → 2.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/README.md +12 -2
- package/dist/index.cjs +87 -52
- package/dist/index.mjs +83 -48
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -27,6 +27,12 @@ export default {
|
|
|
27
27
|
}
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
+
Terser must be installed because plugin-legacy uses Terser for minification.
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
npm add -D terser
|
|
34
|
+
```
|
|
35
|
+
|
|
30
36
|
## Options
|
|
31
37
|
|
|
32
38
|
### `targets`
|
|
@@ -147,8 +153,12 @@ The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https
|
|
|
147
153
|
|
|
148
154
|
- `sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=`
|
|
149
155
|
- `sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=`
|
|
150
|
-
- `sha256-
|
|
151
|
-
- `sha256-
|
|
156
|
+
- `sha256-BoFUHKsYhJ9tbsHugtNQCmnkBbZ11pcW6kZguu+T+EU=`
|
|
157
|
+
- `sha256-A18HC3jLpyEc9B8oyxq/NBFCyFBJFSsRLt0gmT9kft8=`
|
|
158
|
+
|
|
159
|
+
<!--
|
|
160
|
+
Run `node --input-type=module -e "import {cspHashes} from '@vitejs/plugin-legacy'; console.log(cspHashes.map(h => 'sha256-'+h))"` to retrieve the value.
|
|
161
|
+
-->
|
|
152
162
|
|
|
153
163
|
These values (without the `sha256-` prefix) can also be retrieved via
|
|
154
164
|
|
package/dist/index.cjs
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const node_crypto = require('node:crypto');
|
|
7
|
+
const node_module = require('node:module');
|
|
8
|
+
const node_url = require('node:url');
|
|
9
9
|
const vite = require('vite');
|
|
10
10
|
const MagicString = require('magic-string');
|
|
11
11
|
|
|
@@ -21,15 +21,51 @@ async function loadBabel() {
|
|
|
21
21
|
}
|
|
22
22
|
return babel;
|
|
23
23
|
}
|
|
24
|
+
function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRelative) {
|
|
25
|
+
const { renderBuiltUrl } = config.experimental;
|
|
26
|
+
let relative = config.base === "" || config.base === "./";
|
|
27
|
+
if (renderBuiltUrl) {
|
|
28
|
+
const result = renderBuiltUrl(filename, {
|
|
29
|
+
hostId,
|
|
30
|
+
hostType,
|
|
31
|
+
type,
|
|
32
|
+
ssr: !!config.build.ssr
|
|
33
|
+
});
|
|
34
|
+
if (typeof result === "object") {
|
|
35
|
+
if (result.runtime) {
|
|
36
|
+
throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
|
|
37
|
+
}
|
|
38
|
+
if (typeof result.relative === "boolean") {
|
|
39
|
+
relative = result.relative;
|
|
40
|
+
}
|
|
41
|
+
} else if (result) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (relative && !config.build.ssr) {
|
|
46
|
+
return toRelative(filename, hostId);
|
|
47
|
+
} else {
|
|
48
|
+
return config.base + filename;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function getBaseInHTML(urlRelativePath, config) {
|
|
52
|
+
return config.base === "./" || config.base === "" ? path__default.posix.join(path__default.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
|
|
53
|
+
}
|
|
54
|
+
function toAssetPathFromHtml(filename, htmlPath, config) {
|
|
55
|
+
const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
|
|
56
|
+
const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
|
|
57
|
+
return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
|
|
58
|
+
}
|
|
24
59
|
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()}}();`;
|
|
25
60
|
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
26
61
|
const legacyEntryId = "vite-legacy-entry";
|
|
27
62
|
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
|
|
28
63
|
const detectModernBrowserVarName = "__vite_is_modern_browser";
|
|
29
|
-
const detectModernBrowserCode = `try{import
|
|
64
|
+
const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
|
|
30
65
|
const dynamicFallbackInlineCode = `!function(){if(window.${detectModernBrowserVarName})return;console.warn("vite: loading legacy build because dynamic import or import.meta.url is unsupported, syntax error above should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}();`;
|
|
31
66
|
const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
|
|
32
67
|
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
68
|
+
const _require = node_module.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
33
69
|
function viteLegacyPlugin(options = {}) {
|
|
34
70
|
let config;
|
|
35
71
|
const targets = options.targets || "defaults";
|
|
@@ -41,11 +77,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
41
77
|
const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
|
|
42
78
|
const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
|
|
43
79
|
const modernPolyfills = /* @__PURE__ */ new Set();
|
|
44
|
-
const
|
|
45
|
-
"core-js/modules/es.promise",
|
|
46
|
-
"core-js/modules/es.array.iterator"
|
|
47
|
-
];
|
|
48
|
-
const legacyPolyfills = new Set(DEFAULT_LEGACY_POLYFILL);
|
|
80
|
+
const legacyPolyfills = /* @__PURE__ */ new Set();
|
|
49
81
|
if (Array.isArray(options.modernPolyfills)) {
|
|
50
82
|
options.modernPolyfills.forEach((i) => {
|
|
51
83
|
modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
@@ -89,22 +121,19 @@ function viteLegacyPlugin(options = {}) {
|
|
|
89
121
|
return;
|
|
90
122
|
}
|
|
91
123
|
isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
92
|
-
await buildPolyfillChunk(
|
|
124
|
+
await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
|
|
93
125
|
return;
|
|
94
126
|
}
|
|
95
127
|
if (!genLegacy) {
|
|
96
128
|
return;
|
|
97
129
|
}
|
|
98
130
|
if (legacyPolyfills.size || genDynamicFallback) {
|
|
99
|
-
|
|
100
|
-
await detectPolyfills(`Promise.resolve()`, targets, legacyPolyfills);
|
|
101
|
-
}
|
|
131
|
+
await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
|
|
102
132
|
isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
103
|
-
await buildPolyfillChunk(
|
|
133
|
+
await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
104
134
|
}
|
|
105
135
|
}
|
|
106
136
|
};
|
|
107
|
-
const _require = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
108
137
|
const legacyPostPlugin = {
|
|
109
138
|
name: "vite:legacy-post-process",
|
|
110
139
|
enforce: "post",
|
|
@@ -188,7 +217,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
188
217
|
const { code, map } = babel2.transform(raw, {
|
|
189
218
|
babelrc: false,
|
|
190
219
|
configFile: false,
|
|
191
|
-
compact:
|
|
220
|
+
compact: !!config.build.minify,
|
|
192
221
|
sourceMaps,
|
|
193
222
|
inputSourceMap: sourceMaps ? chunk.map : void 0,
|
|
194
223
|
presets: [
|
|
@@ -203,19 +232,10 @@ function viteLegacyPlugin(options = {}) {
|
|
|
203
232
|
],
|
|
204
233
|
[
|
|
205
234
|
"env",
|
|
206
|
-
{
|
|
207
|
-
|
|
208
|
-
modules: false,
|
|
209
|
-
bugfixes: true,
|
|
210
|
-
loose: false,
|
|
211
|
-
useBuiltIns: needPolyfills ? "usage" : false,
|
|
212
|
-
corejs: needPolyfills ? {
|
|
213
|
-
version: _require("core-js/package.json").version,
|
|
214
|
-
proposals: false
|
|
215
|
-
} : void 0,
|
|
216
|
-
shippedProposals: true,
|
|
235
|
+
createBabelPresetEnvOptions(targets, {
|
|
236
|
+
needPolyfills,
|
|
217
237
|
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
|
|
218
|
-
}
|
|
238
|
+
})
|
|
219
239
|
]
|
|
220
240
|
]
|
|
221
241
|
});
|
|
@@ -240,7 +260,8 @@ function viteLegacyPlugin(options = {}) {
|
|
|
240
260
|
tag: "script",
|
|
241
261
|
attrs: {
|
|
242
262
|
type: "module",
|
|
243
|
-
|
|
263
|
+
crossorigin: true,
|
|
264
|
+
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
244
265
|
}
|
|
245
266
|
});
|
|
246
267
|
} else if (modernPolyfills.size) {
|
|
@@ -261,8 +282,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
261
282
|
tag: "script",
|
|
262
283
|
attrs: {
|
|
263
284
|
nomodule: true,
|
|
285
|
+
crossorigin: true,
|
|
264
286
|
id: legacyPolyfillId,
|
|
265
|
-
src:
|
|
287
|
+
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
266
288
|
},
|
|
267
289
|
injectTo: "body"
|
|
268
290
|
});
|
|
@@ -275,8 +297,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
275
297
|
tag: "script",
|
|
276
298
|
attrs: {
|
|
277
299
|
nomodule: true,
|
|
300
|
+
crossorigin: true,
|
|
278
301
|
id: legacyEntryId,
|
|
279
|
-
"data-src":
|
|
302
|
+
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
280
303
|
},
|
|
281
304
|
children: systemJSInlineCode,
|
|
282
305
|
injectTo: "body"
|
|
@@ -352,14 +375,7 @@ async function detectPolyfills(code, targets, list) {
|
|
|
352
375
|
presets: [
|
|
353
376
|
[
|
|
354
377
|
"env",
|
|
355
|
-
{
|
|
356
|
-
targets,
|
|
357
|
-
modules: false,
|
|
358
|
-
useBuiltIns: "usage",
|
|
359
|
-
corejs: { version: 3, proposals: false },
|
|
360
|
-
shippedProposals: true,
|
|
361
|
-
ignoreBrowserslistConfig: true
|
|
362
|
-
}
|
|
378
|
+
createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
|
|
363
379
|
]
|
|
364
380
|
]
|
|
365
381
|
});
|
|
@@ -372,25 +388,44 @@ async function detectPolyfills(code, targets, list) {
|
|
|
372
388
|
}
|
|
373
389
|
}
|
|
374
390
|
}
|
|
375
|
-
|
|
391
|
+
function createBabelPresetEnvOptions(targets, {
|
|
392
|
+
needPolyfills = true,
|
|
393
|
+
ignoreBrowserslistConfig
|
|
394
|
+
}) {
|
|
395
|
+
return {
|
|
396
|
+
targets,
|
|
397
|
+
bugfixes: true,
|
|
398
|
+
loose: false,
|
|
399
|
+
modules: false,
|
|
400
|
+
useBuiltIns: needPolyfills ? "usage" : false,
|
|
401
|
+
corejs: needPolyfills ? {
|
|
402
|
+
version: _require("core-js/package.json").version,
|
|
403
|
+
proposals: false
|
|
404
|
+
} : void 0,
|
|
405
|
+
shippedProposals: true,
|
|
406
|
+
ignoreBrowserslistConfig
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
|
|
376
410
|
let { minify, assetsDir } = buildOptions;
|
|
377
411
|
minify = minify ? "terser" : false;
|
|
378
412
|
const res = await vite.build({
|
|
379
|
-
root: path__default.dirname(
|
|
413
|
+
root: path__default.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)))),
|
|
380
414
|
configFile: false,
|
|
381
415
|
logLevel: "error",
|
|
382
|
-
plugins: [polyfillsPlugin(imports,
|
|
416
|
+
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
|
|
383
417
|
build: {
|
|
384
418
|
write: false,
|
|
385
|
-
target:
|
|
419
|
+
target: "es5",
|
|
386
420
|
minify,
|
|
387
421
|
assetsDir,
|
|
388
422
|
rollupOptions: {
|
|
389
423
|
input: {
|
|
390
|
-
|
|
424
|
+
polyfills: polyfillId
|
|
391
425
|
},
|
|
392
426
|
output: {
|
|
393
|
-
format
|
|
427
|
+
format,
|
|
428
|
+
entryFileNames: rollupOutputOptions.entryFileNames,
|
|
394
429
|
manualChunks: void 0
|
|
395
430
|
}
|
|
396
431
|
}
|
|
@@ -409,7 +444,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
|
|
|
409
444
|
bundle[polyfillChunk.fileName] = polyfillChunk;
|
|
410
445
|
}
|
|
411
446
|
const polyfillId = "\0vite/legacy-polyfills";
|
|
412
|
-
function polyfillsPlugin(imports,
|
|
447
|
+
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
413
448
|
return {
|
|
414
449
|
name: "vite:legacy-polyfills",
|
|
415
450
|
resolveId(id) {
|
|
@@ -419,7 +454,7 @@ function polyfillsPlugin(imports, externalSystemJS) {
|
|
|
419
454
|
},
|
|
420
455
|
load(id) {
|
|
421
456
|
if (id === polyfillId) {
|
|
422
|
-
return [...imports].map((i) => `import "${i}";`).join("") + (
|
|
457
|
+
return [...imports].map((i) => `import "${i}";`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
423
458
|
}
|
|
424
459
|
}
|
|
425
460
|
};
|
|
@@ -474,10 +509,10 @@ function wrapIIFEBabelPlugin() {
|
|
|
474
509
|
};
|
|
475
510
|
}
|
|
476
511
|
const cspHashes = [
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
512
|
+
node_crypto.createHash("sha256").update(safari10NoModuleFix).digest("base64"),
|
|
513
|
+
node_crypto.createHash("sha256").update(systemJSInlineCode).digest("base64"),
|
|
514
|
+
node_crypto.createHash("sha256").update(detectModernBrowserCode).digest("base64"),
|
|
515
|
+
node_crypto.createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
|
|
481
516
|
];
|
|
482
517
|
|
|
483
518
|
module.exports = viteLegacyPlugin;
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { createHash } from 'crypto';
|
|
3
|
-
import { createRequire } from 'module';
|
|
4
|
-
import { fileURLToPath } from 'url';
|
|
5
|
-
import { build } from 'vite';
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { build, normalizePath } from 'vite';
|
|
6
6
|
import MagicString from 'magic-string';
|
|
7
7
|
|
|
8
8
|
let babel;
|
|
@@ -12,15 +12,51 @@ async function loadBabel() {
|
|
|
12
12
|
}
|
|
13
13
|
return babel;
|
|
14
14
|
}
|
|
15
|
+
function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRelative) {
|
|
16
|
+
const { renderBuiltUrl } = config.experimental;
|
|
17
|
+
let relative = config.base === "" || config.base === "./";
|
|
18
|
+
if (renderBuiltUrl) {
|
|
19
|
+
const result = renderBuiltUrl(filename, {
|
|
20
|
+
hostId,
|
|
21
|
+
hostType,
|
|
22
|
+
type,
|
|
23
|
+
ssr: !!config.build.ssr
|
|
24
|
+
});
|
|
25
|
+
if (typeof result === "object") {
|
|
26
|
+
if (result.runtime) {
|
|
27
|
+
throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
|
|
28
|
+
}
|
|
29
|
+
if (typeof result.relative === "boolean") {
|
|
30
|
+
relative = result.relative;
|
|
31
|
+
}
|
|
32
|
+
} else if (result) {
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (relative && !config.build.ssr) {
|
|
37
|
+
return toRelative(filename, hostId);
|
|
38
|
+
} else {
|
|
39
|
+
return config.base + filename;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function getBaseInHTML(urlRelativePath, config) {
|
|
43
|
+
return config.base === "./" || config.base === "" ? path.posix.join(path.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
|
|
44
|
+
}
|
|
45
|
+
function toAssetPathFromHtml(filename, htmlPath, config) {
|
|
46
|
+
const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
|
|
47
|
+
const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
|
|
48
|
+
return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
|
|
49
|
+
}
|
|
15
50
|
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()}}();`;
|
|
16
51
|
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
17
52
|
const legacyEntryId = "vite-legacy-entry";
|
|
18
53
|
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
|
|
19
54
|
const detectModernBrowserVarName = "__vite_is_modern_browser";
|
|
20
|
-
const detectModernBrowserCode = `try{import
|
|
55
|
+
const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
|
|
21
56
|
const dynamicFallbackInlineCode = `!function(){if(window.${detectModernBrowserVarName})return;console.warn("vite: loading legacy build because dynamic import or import.meta.url is unsupported, syntax error above should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}();`;
|
|
22
57
|
const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
|
|
23
58
|
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
59
|
+
const _require = createRequire(import.meta.url);
|
|
24
60
|
function viteLegacyPlugin(options = {}) {
|
|
25
61
|
let config;
|
|
26
62
|
const targets = options.targets || "defaults";
|
|
@@ -32,11 +68,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
32
68
|
const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
|
|
33
69
|
const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
|
|
34
70
|
const modernPolyfills = /* @__PURE__ */ new Set();
|
|
35
|
-
const
|
|
36
|
-
"core-js/modules/es.promise",
|
|
37
|
-
"core-js/modules/es.array.iterator"
|
|
38
|
-
];
|
|
39
|
-
const legacyPolyfills = new Set(DEFAULT_LEGACY_POLYFILL);
|
|
71
|
+
const legacyPolyfills = /* @__PURE__ */ new Set();
|
|
40
72
|
if (Array.isArray(options.modernPolyfills)) {
|
|
41
73
|
options.modernPolyfills.forEach((i) => {
|
|
42
74
|
modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
@@ -80,22 +112,19 @@ function viteLegacyPlugin(options = {}) {
|
|
|
80
112
|
return;
|
|
81
113
|
}
|
|
82
114
|
isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
83
|
-
await buildPolyfillChunk(
|
|
115
|
+
await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
|
|
84
116
|
return;
|
|
85
117
|
}
|
|
86
118
|
if (!genLegacy) {
|
|
87
119
|
return;
|
|
88
120
|
}
|
|
89
121
|
if (legacyPolyfills.size || genDynamicFallback) {
|
|
90
|
-
|
|
91
|
-
await detectPolyfills(`Promise.resolve()`, targets, legacyPolyfills);
|
|
92
|
-
}
|
|
122
|
+
await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
|
|
93
123
|
isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
94
|
-
await buildPolyfillChunk(
|
|
124
|
+
await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
95
125
|
}
|
|
96
126
|
}
|
|
97
127
|
};
|
|
98
|
-
const _require = createRequire(import.meta.url);
|
|
99
128
|
const legacyPostPlugin = {
|
|
100
129
|
name: "vite:legacy-post-process",
|
|
101
130
|
enforce: "post",
|
|
@@ -179,7 +208,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
179
208
|
const { code, map } = babel2.transform(raw, {
|
|
180
209
|
babelrc: false,
|
|
181
210
|
configFile: false,
|
|
182
|
-
compact:
|
|
211
|
+
compact: !!config.build.minify,
|
|
183
212
|
sourceMaps,
|
|
184
213
|
inputSourceMap: sourceMaps ? chunk.map : void 0,
|
|
185
214
|
presets: [
|
|
@@ -194,19 +223,10 @@ function viteLegacyPlugin(options = {}) {
|
|
|
194
223
|
],
|
|
195
224
|
[
|
|
196
225
|
"env",
|
|
197
|
-
{
|
|
198
|
-
|
|
199
|
-
modules: false,
|
|
200
|
-
bugfixes: true,
|
|
201
|
-
loose: false,
|
|
202
|
-
useBuiltIns: needPolyfills ? "usage" : false,
|
|
203
|
-
corejs: needPolyfills ? {
|
|
204
|
-
version: _require("core-js/package.json").version,
|
|
205
|
-
proposals: false
|
|
206
|
-
} : void 0,
|
|
207
|
-
shippedProposals: true,
|
|
226
|
+
createBabelPresetEnvOptions(targets, {
|
|
227
|
+
needPolyfills,
|
|
208
228
|
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
|
|
209
|
-
}
|
|
229
|
+
})
|
|
210
230
|
]
|
|
211
231
|
]
|
|
212
232
|
});
|
|
@@ -231,7 +251,8 @@ function viteLegacyPlugin(options = {}) {
|
|
|
231
251
|
tag: "script",
|
|
232
252
|
attrs: {
|
|
233
253
|
type: "module",
|
|
234
|
-
|
|
254
|
+
crossorigin: true,
|
|
255
|
+
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
235
256
|
}
|
|
236
257
|
});
|
|
237
258
|
} else if (modernPolyfills.size) {
|
|
@@ -252,8 +273,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
252
273
|
tag: "script",
|
|
253
274
|
attrs: {
|
|
254
275
|
nomodule: true,
|
|
276
|
+
crossorigin: true,
|
|
255
277
|
id: legacyPolyfillId,
|
|
256
|
-
src:
|
|
278
|
+
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
257
279
|
},
|
|
258
280
|
injectTo: "body"
|
|
259
281
|
});
|
|
@@ -266,8 +288,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
266
288
|
tag: "script",
|
|
267
289
|
attrs: {
|
|
268
290
|
nomodule: true,
|
|
291
|
+
crossorigin: true,
|
|
269
292
|
id: legacyEntryId,
|
|
270
|
-
"data-src":
|
|
293
|
+
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
271
294
|
},
|
|
272
295
|
children: systemJSInlineCode,
|
|
273
296
|
injectTo: "body"
|
|
@@ -343,14 +366,7 @@ async function detectPolyfills(code, targets, list) {
|
|
|
343
366
|
presets: [
|
|
344
367
|
[
|
|
345
368
|
"env",
|
|
346
|
-
{
|
|
347
|
-
targets,
|
|
348
|
-
modules: false,
|
|
349
|
-
useBuiltIns: "usage",
|
|
350
|
-
corejs: { version: 3, proposals: false },
|
|
351
|
-
shippedProposals: true,
|
|
352
|
-
ignoreBrowserslistConfig: true
|
|
353
|
-
}
|
|
369
|
+
createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
|
|
354
370
|
]
|
|
355
371
|
]
|
|
356
372
|
});
|
|
@@ -363,25 +379,44 @@ async function detectPolyfills(code, targets, list) {
|
|
|
363
379
|
}
|
|
364
380
|
}
|
|
365
381
|
}
|
|
366
|
-
|
|
382
|
+
function createBabelPresetEnvOptions(targets, {
|
|
383
|
+
needPolyfills = true,
|
|
384
|
+
ignoreBrowserslistConfig
|
|
385
|
+
}) {
|
|
386
|
+
return {
|
|
387
|
+
targets,
|
|
388
|
+
bugfixes: true,
|
|
389
|
+
loose: false,
|
|
390
|
+
modules: false,
|
|
391
|
+
useBuiltIns: needPolyfills ? "usage" : false,
|
|
392
|
+
corejs: needPolyfills ? {
|
|
393
|
+
version: _require("core-js/package.json").version,
|
|
394
|
+
proposals: false
|
|
395
|
+
} : void 0,
|
|
396
|
+
shippedProposals: true,
|
|
397
|
+
ignoreBrowserslistConfig
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
|
|
367
401
|
let { minify, assetsDir } = buildOptions;
|
|
368
402
|
minify = minify ? "terser" : false;
|
|
369
403
|
const res = await build({
|
|
370
404
|
root: path.dirname(fileURLToPath(import.meta.url)),
|
|
371
405
|
configFile: false,
|
|
372
406
|
logLevel: "error",
|
|
373
|
-
plugins: [polyfillsPlugin(imports,
|
|
407
|
+
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
|
|
374
408
|
build: {
|
|
375
409
|
write: false,
|
|
376
|
-
target:
|
|
410
|
+
target: "es5",
|
|
377
411
|
minify,
|
|
378
412
|
assetsDir,
|
|
379
413
|
rollupOptions: {
|
|
380
414
|
input: {
|
|
381
|
-
|
|
415
|
+
polyfills: polyfillId
|
|
382
416
|
},
|
|
383
417
|
output: {
|
|
384
|
-
format
|
|
418
|
+
format,
|
|
419
|
+
entryFileNames: rollupOutputOptions.entryFileNames,
|
|
385
420
|
manualChunks: void 0
|
|
386
421
|
}
|
|
387
422
|
}
|
|
@@ -400,7 +435,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
|
|
|
400
435
|
bundle[polyfillChunk.fileName] = polyfillChunk;
|
|
401
436
|
}
|
|
402
437
|
const polyfillId = "\0vite/legacy-polyfills";
|
|
403
|
-
function polyfillsPlugin(imports,
|
|
438
|
+
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
404
439
|
return {
|
|
405
440
|
name: "vite:legacy-polyfills",
|
|
406
441
|
resolveId(id) {
|
|
@@ -410,7 +445,7 @@ function polyfillsPlugin(imports, externalSystemJS) {
|
|
|
410
445
|
},
|
|
411
446
|
load(id) {
|
|
412
447
|
if (id === polyfillId) {
|
|
413
|
-
return [...imports].map((i) => `import "${i}";`).join("") + (
|
|
448
|
+
return [...imports].map((i) => `import "${i}";`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
414
449
|
}
|
|
415
450
|
}
|
|
416
451
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitejs/plugin-legacy",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Evan You",
|
|
6
6
|
"files": [
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
"scripts": {
|
|
20
20
|
"dev": "unbuild --stub",
|
|
21
21
|
"build": "unbuild && pnpm run patch-cjs",
|
|
22
|
-
"patch-cjs": "
|
|
22
|
+
"patch-cjs": "tsx ../../scripts/patchCJS.ts",
|
|
23
23
|
"prepublishOnly": "npm run build"
|
|
24
24
|
},
|
|
25
25
|
"engines": {
|
|
26
|
-
"node": ">=14.
|
|
26
|
+
"node": ">=14.18.0"
|
|
27
27
|
},
|
|
28
28
|
"repository": {
|
|
29
29
|
"type": "git",
|
|
@@ -35,17 +35,18 @@
|
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@babel/standalone": "^7.
|
|
39
|
-
"core-js": "^3.
|
|
40
|
-
"magic-string": "^0.26.
|
|
38
|
+
"@babel/standalone": "^7.18.7",
|
|
39
|
+
"core-js": "^3.23.1",
|
|
40
|
+
"magic-string": "^0.26.2",
|
|
41
41
|
"regenerator-runtime": "^0.13.9",
|
|
42
42
|
"systemjs": "^6.12.1"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
+
"terser": "^5.4.0",
|
|
45
46
|
"vite": "^3.0.0-alpha"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
|
-
"
|
|
49
|
-
"
|
|
49
|
+
"@babel/core": "^7.18.6",
|
|
50
|
+
"vite": "workspace:*"
|
|
50
51
|
}
|
|
51
52
|
}
|