@vitejs/plugin-legacy 2.0.0-alpha.0 → 2.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/README.md +14 -4
- package/dist/index.cjs +66 -53
- package/dist/index.mjs +64 -51
- 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`
|
|
@@ -122,9 +128,9 @@ The legacy plugin offers a way to use native `import()` in the modern build whil
|
|
|
122
128
|
|
|
123
129
|
Polyfill specifier strings for `polyfills` and `modernPolyfills` can be either of the following:
|
|
124
130
|
|
|
125
|
-
- Any [`core-js` 3 sub import paths](https://unpkg.com/browse/core-js@
|
|
131
|
+
- Any [`core-js` 3 sub import paths](https://unpkg.com/browse/core-js@latest/) - e.g. `es/map` will import `core-js/es/map`
|
|
126
132
|
|
|
127
|
-
- Any [individual `core-js` 3 modules](https://unpkg.com/browse/core-js@
|
|
133
|
+
- Any [individual `core-js` 3 modules](https://unpkg.com/browse/core-js@latest/modules/) - e.g. `es.array.iterator` will import `core-js/modules/es.array.iterator.js`
|
|
128
134
|
|
|
129
135
|
**Example**
|
|
130
136
|
|
|
@@ -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,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const path = require('path');
|
|
6
|
-
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');
|
|
7
9
|
const vite = require('vite');
|
|
8
10
|
const MagicString = require('magic-string');
|
|
9
11
|
|
|
@@ -19,15 +21,26 @@ async function loadBabel() {
|
|
|
19
21
|
}
|
|
20
22
|
return babel;
|
|
21
23
|
}
|
|
24
|
+
function getBaseInHTML(urlRelativePath, baseOptions, config) {
|
|
25
|
+
return baseOptions.url ?? (baseOptions.relative ? path__default.posix.join(path__default.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base);
|
|
26
|
+
}
|
|
27
|
+
function getAssetsBase(urlRelativePath, config) {
|
|
28
|
+
return getBaseInHTML(urlRelativePath, config.experimental.buildAdvancedBaseOptions.assets, config);
|
|
29
|
+
}
|
|
30
|
+
function toAssetPathFromHtml(filename, htmlPath, config) {
|
|
31
|
+
const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
|
|
32
|
+
return getAssetsBase(relativeUrlPath, config) + filename;
|
|
33
|
+
}
|
|
22
34
|
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()}}();`;
|
|
23
35
|
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
24
36
|
const legacyEntryId = "vite-legacy-entry";
|
|
25
37
|
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const dynamicFallbackInlineCode = `!function(){if(window.${
|
|
38
|
+
const detectModernBrowserVarName = "__vite_is_modern_browser";
|
|
39
|
+
const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
|
|
40
|
+
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)}();`;
|
|
29
41
|
const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
|
|
30
42
|
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
43
|
+
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)));
|
|
31
44
|
function viteLegacyPlugin(options = {}) {
|
|
32
45
|
let config;
|
|
33
46
|
const targets = options.targets || "defaults";
|
|
@@ -39,11 +52,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
39
52
|
const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
|
|
40
53
|
const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
|
|
41
54
|
const modernPolyfills = /* @__PURE__ */ new Set();
|
|
42
|
-
const
|
|
43
|
-
"core-js/modules/es.promise",
|
|
44
|
-
"core-js/modules/es.array.iterator"
|
|
45
|
-
];
|
|
46
|
-
const legacyPolyfills = new Set(DEFAULT_LEGACY_POLYFILL);
|
|
55
|
+
const legacyPolyfills = /* @__PURE__ */ new Set();
|
|
47
56
|
if (Array.isArray(options.modernPolyfills)) {
|
|
48
57
|
options.modernPolyfills.forEach((i) => {
|
|
49
58
|
modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
@@ -87,18 +96,16 @@ function viteLegacyPlugin(options = {}) {
|
|
|
87
96
|
return;
|
|
88
97
|
}
|
|
89
98
|
isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
90
|
-
await buildPolyfillChunk(
|
|
99
|
+
await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
|
|
91
100
|
return;
|
|
92
101
|
}
|
|
93
102
|
if (!genLegacy) {
|
|
94
103
|
return;
|
|
95
104
|
}
|
|
96
105
|
if (legacyPolyfills.size || genDynamicFallback) {
|
|
97
|
-
|
|
98
|
-
await detectPolyfills(`Promise.resolve()`, targets, legacyPolyfills);
|
|
99
|
-
}
|
|
106
|
+
await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
|
|
100
107
|
isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
101
|
-
await buildPolyfillChunk(
|
|
108
|
+
await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
102
109
|
}
|
|
103
110
|
}
|
|
104
111
|
};
|
|
@@ -185,7 +192,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
185
192
|
const { code, map } = babel2.transform(raw, {
|
|
186
193
|
babelrc: false,
|
|
187
194
|
configFile: false,
|
|
188
|
-
compact:
|
|
195
|
+
compact: !!config.build.minify,
|
|
189
196
|
sourceMaps,
|
|
190
197
|
inputSourceMap: sourceMaps ? chunk.map : void 0,
|
|
191
198
|
presets: [
|
|
@@ -200,19 +207,10 @@ function viteLegacyPlugin(options = {}) {
|
|
|
200
207
|
],
|
|
201
208
|
[
|
|
202
209
|
"env",
|
|
203
|
-
{
|
|
204
|
-
|
|
205
|
-
modules: false,
|
|
206
|
-
bugfixes: true,
|
|
207
|
-
loose: false,
|
|
208
|
-
useBuiltIns: needPolyfills ? "usage" : false,
|
|
209
|
-
corejs: needPolyfills ? {
|
|
210
|
-
version: require("core-js/package.json").version,
|
|
211
|
-
proposals: false
|
|
212
|
-
} : void 0,
|
|
213
|
-
shippedProposals: true,
|
|
210
|
+
createBabelPresetEnvOptions(targets, {
|
|
211
|
+
needPolyfills,
|
|
214
212
|
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
|
|
215
|
-
}
|
|
213
|
+
})
|
|
216
214
|
]
|
|
217
215
|
]
|
|
218
216
|
});
|
|
@@ -237,7 +235,8 @@ function viteLegacyPlugin(options = {}) {
|
|
|
237
235
|
tag: "script",
|
|
238
236
|
attrs: {
|
|
239
237
|
type: "module",
|
|
240
|
-
|
|
238
|
+
crossorigin: true,
|
|
239
|
+
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
241
240
|
}
|
|
242
241
|
});
|
|
243
242
|
} else if (modernPolyfills.size) {
|
|
@@ -258,8 +257,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
258
257
|
tag: "script",
|
|
259
258
|
attrs: {
|
|
260
259
|
nomodule: true,
|
|
260
|
+
crossorigin: true,
|
|
261
261
|
id: legacyPolyfillId,
|
|
262
|
-
src:
|
|
262
|
+
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
263
263
|
},
|
|
264
264
|
injectTo: "body"
|
|
265
265
|
});
|
|
@@ -272,8 +272,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
272
272
|
tag: "script",
|
|
273
273
|
attrs: {
|
|
274
274
|
nomodule: true,
|
|
275
|
+
crossorigin: true,
|
|
275
276
|
id: legacyEntryId,
|
|
276
|
-
"data-src":
|
|
277
|
+
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
277
278
|
},
|
|
278
279
|
children: systemJSInlineCode,
|
|
279
280
|
injectTo: "body"
|
|
@@ -285,7 +286,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
285
286
|
tags.push({
|
|
286
287
|
tag: "script",
|
|
287
288
|
attrs: { type: "module" },
|
|
288
|
-
children:
|
|
289
|
+
children: detectModernBrowserCode,
|
|
289
290
|
injectTo: "head"
|
|
290
291
|
});
|
|
291
292
|
tags.push({
|
|
@@ -349,14 +350,7 @@ async function detectPolyfills(code, targets, list) {
|
|
|
349
350
|
presets: [
|
|
350
351
|
[
|
|
351
352
|
"env",
|
|
352
|
-
{
|
|
353
|
-
targets,
|
|
354
|
-
modules: false,
|
|
355
|
-
useBuiltIns: "usage",
|
|
356
|
-
corejs: { version: 3, proposals: false },
|
|
357
|
-
shippedProposals: true,
|
|
358
|
-
ignoreBrowserslistConfig: true
|
|
359
|
-
}
|
|
353
|
+
createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
|
|
360
354
|
]
|
|
361
355
|
]
|
|
362
356
|
});
|
|
@@ -369,25 +363,44 @@ async function detectPolyfills(code, targets, list) {
|
|
|
369
363
|
}
|
|
370
364
|
}
|
|
371
365
|
}
|
|
372
|
-
|
|
366
|
+
function createBabelPresetEnvOptions(targets, {
|
|
367
|
+
needPolyfills = true,
|
|
368
|
+
ignoreBrowserslistConfig
|
|
369
|
+
}) {
|
|
370
|
+
return {
|
|
371
|
+
targets,
|
|
372
|
+
bugfixes: true,
|
|
373
|
+
loose: false,
|
|
374
|
+
modules: false,
|
|
375
|
+
useBuiltIns: needPolyfills ? "usage" : false,
|
|
376
|
+
corejs: needPolyfills ? {
|
|
377
|
+
version: _require("core-js/package.json").version,
|
|
378
|
+
proposals: false
|
|
379
|
+
} : void 0,
|
|
380
|
+
shippedProposals: true,
|
|
381
|
+
ignoreBrowserslistConfig
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
|
|
373
385
|
let { minify, assetsDir } = buildOptions;
|
|
374
386
|
minify = minify ? "terser" : false;
|
|
375
387
|
const res = await vite.build({
|
|
376
|
-
root:
|
|
388
|
+
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)))),
|
|
377
389
|
configFile: false,
|
|
378
390
|
logLevel: "error",
|
|
379
|
-
plugins: [polyfillsPlugin(imports,
|
|
391
|
+
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
|
|
380
392
|
build: {
|
|
381
393
|
write: false,
|
|
382
|
-
target:
|
|
394
|
+
target: "es5",
|
|
383
395
|
minify,
|
|
384
396
|
assetsDir,
|
|
385
397
|
rollupOptions: {
|
|
386
398
|
input: {
|
|
387
|
-
|
|
399
|
+
polyfills: polyfillId
|
|
388
400
|
},
|
|
389
401
|
output: {
|
|
390
|
-
format
|
|
402
|
+
format,
|
|
403
|
+
entryFileNames: rollupOutputOptions.entryFileNames,
|
|
391
404
|
manualChunks: void 0
|
|
392
405
|
}
|
|
393
406
|
}
|
|
@@ -403,10 +416,10 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
|
|
|
403
416
|
facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
|
|
404
417
|
}
|
|
405
418
|
}
|
|
406
|
-
bundle[polyfillChunk.
|
|
419
|
+
bundle[polyfillChunk.fileName] = polyfillChunk;
|
|
407
420
|
}
|
|
408
421
|
const polyfillId = "\0vite/legacy-polyfills";
|
|
409
|
-
function polyfillsPlugin(imports,
|
|
422
|
+
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
410
423
|
return {
|
|
411
424
|
name: "vite:legacy-polyfills",
|
|
412
425
|
resolveId(id) {
|
|
@@ -416,7 +429,7 @@ function polyfillsPlugin(imports, externalSystemJS) {
|
|
|
416
429
|
},
|
|
417
430
|
load(id) {
|
|
418
431
|
if (id === polyfillId) {
|
|
419
|
-
return [...imports].map((i) => `import "${i}";`).join("") + (
|
|
432
|
+
return [...imports].map((i) => `import "${i}";`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
420
433
|
}
|
|
421
434
|
}
|
|
422
435
|
};
|
|
@@ -471,10 +484,10 @@ function wrapIIFEBabelPlugin() {
|
|
|
471
484
|
};
|
|
472
485
|
}
|
|
473
486
|
const cspHashes = [
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
487
|
+
node_crypto.createHash("sha256").update(safari10NoModuleFix).digest("base64"),
|
|
488
|
+
node_crypto.createHash("sha256").update(systemJSInlineCode).digest("base64"),
|
|
489
|
+
node_crypto.createHash("sha256").update(detectModernBrowserCode).digest("base64"),
|
|
490
|
+
node_crypto.createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
|
|
478
491
|
];
|
|
479
492
|
|
|
480
493
|
module.exports = viteLegacyPlugin;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { createHash } from 'crypto';
|
|
3
|
-
import {
|
|
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';
|
|
4
6
|
import MagicString from 'magic-string';
|
|
5
7
|
|
|
6
8
|
let babel;
|
|
@@ -10,15 +12,26 @@ async function loadBabel() {
|
|
|
10
12
|
}
|
|
11
13
|
return babel;
|
|
12
14
|
}
|
|
15
|
+
function getBaseInHTML(urlRelativePath, baseOptions, config) {
|
|
16
|
+
return baseOptions.url ?? (baseOptions.relative ? path.posix.join(path.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base);
|
|
17
|
+
}
|
|
18
|
+
function getAssetsBase(urlRelativePath, config) {
|
|
19
|
+
return getBaseInHTML(urlRelativePath, config.experimental.buildAdvancedBaseOptions.assets, config);
|
|
20
|
+
}
|
|
21
|
+
function toAssetPathFromHtml(filename, htmlPath, config) {
|
|
22
|
+
const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
|
|
23
|
+
return getAssetsBase(relativeUrlPath, config) + filename;
|
|
24
|
+
}
|
|
13
25
|
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()}}();`;
|
|
14
26
|
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
15
27
|
const legacyEntryId = "vite-legacy-entry";
|
|
16
28
|
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const dynamicFallbackInlineCode = `!function(){if(window.${
|
|
29
|
+
const detectModernBrowserVarName = "__vite_is_modern_browser";
|
|
30
|
+
const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
|
|
31
|
+
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)}();`;
|
|
20
32
|
const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
|
|
21
33
|
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
34
|
+
const _require = createRequire(import.meta.url);
|
|
22
35
|
function viteLegacyPlugin(options = {}) {
|
|
23
36
|
let config;
|
|
24
37
|
const targets = options.targets || "defaults";
|
|
@@ -30,11 +43,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
30
43
|
const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
|
|
31
44
|
const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
|
|
32
45
|
const modernPolyfills = /* @__PURE__ */ new Set();
|
|
33
|
-
const
|
|
34
|
-
"core-js/modules/es.promise",
|
|
35
|
-
"core-js/modules/es.array.iterator"
|
|
36
|
-
];
|
|
37
|
-
const legacyPolyfills = new Set(DEFAULT_LEGACY_POLYFILL);
|
|
46
|
+
const legacyPolyfills = /* @__PURE__ */ new Set();
|
|
38
47
|
if (Array.isArray(options.modernPolyfills)) {
|
|
39
48
|
options.modernPolyfills.forEach((i) => {
|
|
40
49
|
modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
@@ -78,18 +87,16 @@ function viteLegacyPlugin(options = {}) {
|
|
|
78
87
|
return;
|
|
79
88
|
}
|
|
80
89
|
isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
81
|
-
await buildPolyfillChunk(
|
|
90
|
+
await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
|
|
82
91
|
return;
|
|
83
92
|
}
|
|
84
93
|
if (!genLegacy) {
|
|
85
94
|
return;
|
|
86
95
|
}
|
|
87
96
|
if (legacyPolyfills.size || genDynamicFallback) {
|
|
88
|
-
|
|
89
|
-
await detectPolyfills(`Promise.resolve()`, targets, legacyPolyfills);
|
|
90
|
-
}
|
|
97
|
+
await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
|
|
91
98
|
isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
92
|
-
await buildPolyfillChunk(
|
|
99
|
+
await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
93
100
|
}
|
|
94
101
|
}
|
|
95
102
|
};
|
|
@@ -176,7 +183,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
176
183
|
const { code, map } = babel2.transform(raw, {
|
|
177
184
|
babelrc: false,
|
|
178
185
|
configFile: false,
|
|
179
|
-
compact:
|
|
186
|
+
compact: !!config.build.minify,
|
|
180
187
|
sourceMaps,
|
|
181
188
|
inputSourceMap: sourceMaps ? chunk.map : void 0,
|
|
182
189
|
presets: [
|
|
@@ -191,19 +198,10 @@ function viteLegacyPlugin(options = {}) {
|
|
|
191
198
|
],
|
|
192
199
|
[
|
|
193
200
|
"env",
|
|
194
|
-
{
|
|
195
|
-
|
|
196
|
-
modules: false,
|
|
197
|
-
bugfixes: true,
|
|
198
|
-
loose: false,
|
|
199
|
-
useBuiltIns: needPolyfills ? "usage" : false,
|
|
200
|
-
corejs: needPolyfills ? {
|
|
201
|
-
version: require("core-js/package.json").version,
|
|
202
|
-
proposals: false
|
|
203
|
-
} : void 0,
|
|
204
|
-
shippedProposals: true,
|
|
201
|
+
createBabelPresetEnvOptions(targets, {
|
|
202
|
+
needPolyfills,
|
|
205
203
|
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
|
|
206
|
-
}
|
|
204
|
+
})
|
|
207
205
|
]
|
|
208
206
|
]
|
|
209
207
|
});
|
|
@@ -228,7 +226,8 @@ function viteLegacyPlugin(options = {}) {
|
|
|
228
226
|
tag: "script",
|
|
229
227
|
attrs: {
|
|
230
228
|
type: "module",
|
|
231
|
-
|
|
229
|
+
crossorigin: true,
|
|
230
|
+
src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
|
|
232
231
|
}
|
|
233
232
|
});
|
|
234
233
|
} else if (modernPolyfills.size) {
|
|
@@ -249,8 +248,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
249
248
|
tag: "script",
|
|
250
249
|
attrs: {
|
|
251
250
|
nomodule: true,
|
|
251
|
+
crossorigin: true,
|
|
252
252
|
id: legacyPolyfillId,
|
|
253
|
-
src:
|
|
253
|
+
src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
|
|
254
254
|
},
|
|
255
255
|
injectTo: "body"
|
|
256
256
|
});
|
|
@@ -263,8 +263,9 @@ function viteLegacyPlugin(options = {}) {
|
|
|
263
263
|
tag: "script",
|
|
264
264
|
attrs: {
|
|
265
265
|
nomodule: true,
|
|
266
|
+
crossorigin: true,
|
|
266
267
|
id: legacyEntryId,
|
|
267
|
-
"data-src":
|
|
268
|
+
"data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
|
|
268
269
|
},
|
|
269
270
|
children: systemJSInlineCode,
|
|
270
271
|
injectTo: "body"
|
|
@@ -276,7 +277,7 @@ function viteLegacyPlugin(options = {}) {
|
|
|
276
277
|
tags.push({
|
|
277
278
|
tag: "script",
|
|
278
279
|
attrs: { type: "module" },
|
|
279
|
-
children:
|
|
280
|
+
children: detectModernBrowserCode,
|
|
280
281
|
injectTo: "head"
|
|
281
282
|
});
|
|
282
283
|
tags.push({
|
|
@@ -340,14 +341,7 @@ async function detectPolyfills(code, targets, list) {
|
|
|
340
341
|
presets: [
|
|
341
342
|
[
|
|
342
343
|
"env",
|
|
343
|
-
{
|
|
344
|
-
targets,
|
|
345
|
-
modules: false,
|
|
346
|
-
useBuiltIns: "usage",
|
|
347
|
-
corejs: { version: 3, proposals: false },
|
|
348
|
-
shippedProposals: true,
|
|
349
|
-
ignoreBrowserslistConfig: true
|
|
350
|
-
}
|
|
344
|
+
createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
|
|
351
345
|
]
|
|
352
346
|
]
|
|
353
347
|
});
|
|
@@ -360,25 +354,44 @@ async function detectPolyfills(code, targets, list) {
|
|
|
360
354
|
}
|
|
361
355
|
}
|
|
362
356
|
}
|
|
363
|
-
|
|
357
|
+
function createBabelPresetEnvOptions(targets, {
|
|
358
|
+
needPolyfills = true,
|
|
359
|
+
ignoreBrowserslistConfig
|
|
360
|
+
}) {
|
|
361
|
+
return {
|
|
362
|
+
targets,
|
|
363
|
+
bugfixes: true,
|
|
364
|
+
loose: false,
|
|
365
|
+
modules: false,
|
|
366
|
+
useBuiltIns: needPolyfills ? "usage" : false,
|
|
367
|
+
corejs: needPolyfills ? {
|
|
368
|
+
version: _require("core-js/package.json").version,
|
|
369
|
+
proposals: false
|
|
370
|
+
} : void 0,
|
|
371
|
+
shippedProposals: true,
|
|
372
|
+
ignoreBrowserslistConfig
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
|
|
364
376
|
let { minify, assetsDir } = buildOptions;
|
|
365
377
|
minify = minify ? "terser" : false;
|
|
366
378
|
const res = await build({
|
|
367
|
-
root:
|
|
379
|
+
root: path.dirname(fileURLToPath(import.meta.url)),
|
|
368
380
|
configFile: false,
|
|
369
381
|
logLevel: "error",
|
|
370
|
-
plugins: [polyfillsPlugin(imports,
|
|
382
|
+
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
|
|
371
383
|
build: {
|
|
372
384
|
write: false,
|
|
373
|
-
target:
|
|
385
|
+
target: "es5",
|
|
374
386
|
minify,
|
|
375
387
|
assetsDir,
|
|
376
388
|
rollupOptions: {
|
|
377
389
|
input: {
|
|
378
|
-
|
|
390
|
+
polyfills: polyfillId
|
|
379
391
|
},
|
|
380
392
|
output: {
|
|
381
|
-
format
|
|
393
|
+
format,
|
|
394
|
+
entryFileNames: rollupOutputOptions.entryFileNames,
|
|
382
395
|
manualChunks: void 0
|
|
383
396
|
}
|
|
384
397
|
}
|
|
@@ -394,10 +407,10 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
|
|
|
394
407
|
facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
|
|
395
408
|
}
|
|
396
409
|
}
|
|
397
|
-
bundle[polyfillChunk.
|
|
410
|
+
bundle[polyfillChunk.fileName] = polyfillChunk;
|
|
398
411
|
}
|
|
399
412
|
const polyfillId = "\0vite/legacy-polyfills";
|
|
400
|
-
function polyfillsPlugin(imports,
|
|
413
|
+
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
401
414
|
return {
|
|
402
415
|
name: "vite:legacy-polyfills",
|
|
403
416
|
resolveId(id) {
|
|
@@ -407,7 +420,7 @@ function polyfillsPlugin(imports, externalSystemJS) {
|
|
|
407
420
|
},
|
|
408
421
|
load(id) {
|
|
409
422
|
if (id === polyfillId) {
|
|
410
|
-
return [...imports].map((i) => `import "${i}";`).join("") + (
|
|
423
|
+
return [...imports].map((i) => `import "${i}";`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
411
424
|
}
|
|
412
425
|
}
|
|
413
426
|
};
|
|
@@ -464,7 +477,7 @@ function wrapIIFEBabelPlugin() {
|
|
|
464
477
|
const cspHashes = [
|
|
465
478
|
createHash("sha256").update(safari10NoModuleFix).digest("base64"),
|
|
466
479
|
createHash("sha256").update(systemJSInlineCode).digest("base64"),
|
|
467
|
-
createHash("sha256").update(
|
|
480
|
+
createHash("sha256").update(detectModernBrowserCode).digest("base64"),
|
|
468
481
|
createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
|
|
469
482
|
];
|
|
470
483
|
|
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.0",
|
|
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": "esno ../../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.5",
|
|
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.5",
|
|
50
|
+
"vite": "workspace:*"
|
|
50
51
|
}
|
|
51
52
|
}
|