@vitejs/plugin-legacy 1.8.2 → 2.0.0-alpha.2
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 +11 -17
- package/dist/index.cjs +487 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.mjs +475 -0
- package/package.json +28 -10
- package/index.d.ts +0 -35
- package/index.js +0 -712
package/README.md
CHANGED
|
@@ -27,20 +27,10 @@ export default {
|
|
|
27
27
|
}
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Terser must be installed because plugin-legacy uses Terser for minification.
|
|
31
31
|
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
import legacy from '@vitejs/plugin-legacy'
|
|
35
|
-
|
|
36
|
-
export default {
|
|
37
|
-
plugins: [
|
|
38
|
-
legacy({
|
|
39
|
-
targets: ['ie >= 11'],
|
|
40
|
-
additionalLegacyPolyfills: ['regenerator-runtime/runtime']
|
|
41
|
-
})
|
|
42
|
-
]
|
|
43
|
-
}
|
|
32
|
+
```sh
|
|
33
|
+
npm add -D terser
|
|
44
34
|
```
|
|
45
35
|
|
|
46
36
|
## Options
|
|
@@ -138,9 +128,9 @@ The legacy plugin offers a way to use native `import()` in the modern build whil
|
|
|
138
128
|
|
|
139
129
|
Polyfill specifier strings for `polyfills` and `modernPolyfills` can be either of the following:
|
|
140
130
|
|
|
141
|
-
- 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`
|
|
142
132
|
|
|
143
|
-
- 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`
|
|
144
134
|
|
|
145
135
|
**Example**
|
|
146
136
|
|
|
@@ -163,8 +153,12 @@ The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https
|
|
|
163
153
|
|
|
164
154
|
- `sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=`
|
|
165
155
|
- `sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=`
|
|
166
|
-
- `sha256-
|
|
167
|
-
- `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
|
+
-->
|
|
168
162
|
|
|
169
163
|
These values (without the `sha256-` prefix) can also be retrieved via
|
|
170
164
|
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const crypto = require('crypto');
|
|
7
|
+
const module$1 = require('module');
|
|
8
|
+
const url = require('url');
|
|
9
|
+
const vite = require('vite');
|
|
10
|
+
const MagicString = require('magic-string');
|
|
11
|
+
|
|
12
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
13
|
+
|
|
14
|
+
const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
15
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
16
|
+
|
|
17
|
+
let babel;
|
|
18
|
+
async function loadBabel() {
|
|
19
|
+
if (!babel) {
|
|
20
|
+
babel = await import('@babel/standalone');
|
|
21
|
+
}
|
|
22
|
+
return babel;
|
|
23
|
+
}
|
|
24
|
+
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
|
+
const legacyPolyfillId = "vite-legacy-polyfill";
|
|
26
|
+
const legacyEntryId = "vite-legacy-entry";
|
|
27
|
+
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
|
|
28
|
+
const detectModernBrowserVarName = "__vite_is_modern_browser";
|
|
29
|
+
const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
|
|
30
|
+
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
|
+
const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
|
|
32
|
+
const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
|
|
33
|
+
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)));
|
|
34
|
+
function viteLegacyPlugin(options = {}) {
|
|
35
|
+
let config;
|
|
36
|
+
const targets = options.targets || "defaults";
|
|
37
|
+
const genLegacy = options.renderLegacyChunks !== false;
|
|
38
|
+
const genDynamicFallback = genLegacy;
|
|
39
|
+
const debugFlags = (process.env.DEBUG || "").split(",");
|
|
40
|
+
const isDebug = debugFlags.includes("vite:*") || debugFlags.includes("vite:legacy");
|
|
41
|
+
const facadeToLegacyChunkMap = /* @__PURE__ */ new Map();
|
|
42
|
+
const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
|
|
43
|
+
const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
|
|
44
|
+
const modernPolyfills = /* @__PURE__ */ new Set();
|
|
45
|
+
const legacyPolyfills = /* @__PURE__ */ new Set();
|
|
46
|
+
if (Array.isArray(options.modernPolyfills)) {
|
|
47
|
+
options.modernPolyfills.forEach((i) => {
|
|
48
|
+
modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (Array.isArray(options.polyfills)) {
|
|
52
|
+
options.polyfills.forEach((i) => {
|
|
53
|
+
if (i.startsWith(`regenerator`)) {
|
|
54
|
+
legacyPolyfills.add(`regenerator-runtime/runtime.js`);
|
|
55
|
+
} else {
|
|
56
|
+
legacyPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(options.additionalLegacyPolyfills)) {
|
|
61
|
+
options.additionalLegacyPolyfills.forEach((i) => {
|
|
62
|
+
legacyPolyfills.add(i);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const legacyConfigPlugin = {
|
|
66
|
+
name: "vite:legacy-config",
|
|
67
|
+
apply: "build",
|
|
68
|
+
config(config2) {
|
|
69
|
+
if (!config2.build) {
|
|
70
|
+
config2.build = {};
|
|
71
|
+
}
|
|
72
|
+
if (!config2.build.cssTarget) {
|
|
73
|
+
config2.build.cssTarget = "chrome61";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const legacyGenerateBundlePlugin = {
|
|
78
|
+
name: "vite:legacy-generate-polyfill-chunk",
|
|
79
|
+
apply: "build",
|
|
80
|
+
async generateBundle(opts, bundle) {
|
|
81
|
+
if (config.build.ssr) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (!isLegacyBundle(bundle, opts)) {
|
|
85
|
+
if (!modernPolyfills.size) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
|
|
89
|
+
await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!genLegacy) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (legacyPolyfills.size || genDynamicFallback) {
|
|
96
|
+
await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
|
|
97
|
+
isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
|
|
98
|
+
await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const legacyPostPlugin = {
|
|
103
|
+
name: "vite:legacy-post-process",
|
|
104
|
+
enforce: "post",
|
|
105
|
+
apply: "build",
|
|
106
|
+
configResolved(_config) {
|
|
107
|
+
if (_config.build.lib) {
|
|
108
|
+
throw new Error("@vitejs/plugin-legacy does not support library mode.");
|
|
109
|
+
}
|
|
110
|
+
config = _config;
|
|
111
|
+
if (!genLegacy || config.build.ssr) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy.[hash].js") => {
|
|
115
|
+
if (!fileNames) {
|
|
116
|
+
return path__default.posix.join(config.build.assetsDir, defaultFileName);
|
|
117
|
+
}
|
|
118
|
+
return (chunkInfo) => {
|
|
119
|
+
let fileName = typeof fileNames === "function" ? fileNames(chunkInfo) : fileNames;
|
|
120
|
+
if (fileName.includes("[name]")) {
|
|
121
|
+
fileName = fileName.replace("[name]", "[name]-legacy");
|
|
122
|
+
} else {
|
|
123
|
+
fileName = fileName.replace(/(.+)\.(.+)/, "$1-legacy.$2");
|
|
124
|
+
}
|
|
125
|
+
return fileName;
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
const createLegacyOutput = (options2 = {}) => {
|
|
129
|
+
return {
|
|
130
|
+
...options2,
|
|
131
|
+
format: "system",
|
|
132
|
+
entryFileNames: getLegacyOutputFileName(options2.entryFileNames),
|
|
133
|
+
chunkFileNames: getLegacyOutputFileName(options2.chunkFileNames)
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
const { rollupOptions } = config.build;
|
|
137
|
+
const { output } = rollupOptions;
|
|
138
|
+
if (Array.isArray(output)) {
|
|
139
|
+
rollupOptions.output = [...output.map(createLegacyOutput), ...output];
|
|
140
|
+
} else {
|
|
141
|
+
rollupOptions.output = [createLegacyOutput(output), output || {}];
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
async renderChunk(raw, chunk, opts) {
|
|
145
|
+
if (config.build.ssr) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
if (!isLegacyChunk(chunk, opts)) {
|
|
149
|
+
if (options.modernPolyfills && !Array.isArray(options.modernPolyfills)) {
|
|
150
|
+
await detectPolyfills(raw, { esmodules: true }, modernPolyfills);
|
|
151
|
+
}
|
|
152
|
+
const ms = new MagicString__default(raw);
|
|
153
|
+
if (genDynamicFallback && chunk.isEntry) {
|
|
154
|
+
ms.prepend(forceDynamicImportUsage);
|
|
155
|
+
}
|
|
156
|
+
if (raw.includes(legacyEnvVarMarker)) {
|
|
157
|
+
const re = new RegExp(legacyEnvVarMarker, "g");
|
|
158
|
+
let match;
|
|
159
|
+
while (match = re.exec(raw)) {
|
|
160
|
+
ms.overwrite(match.index, match.index + legacyEnvVarMarker.length, `false`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (config.build.sourcemap) {
|
|
164
|
+
return {
|
|
165
|
+
code: ms.toString(),
|
|
166
|
+
map: ms.generateMap({ hires: true })
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
code: ms.toString()
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
if (!genLegacy) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
opts.__vite_skip_esbuild__ = true;
|
|
177
|
+
opts.__vite_force_terser__ = true;
|
|
178
|
+
opts.__vite_skip_asset_emit__ = true;
|
|
179
|
+
const needPolyfills = options.polyfills !== false && !Array.isArray(options.polyfills);
|
|
180
|
+
const sourceMaps = !!config.build.sourcemap;
|
|
181
|
+
const babel2 = await loadBabel();
|
|
182
|
+
const { code, map } = babel2.transform(raw, {
|
|
183
|
+
babelrc: false,
|
|
184
|
+
configFile: false,
|
|
185
|
+
compact: !!config.build.minify,
|
|
186
|
+
sourceMaps,
|
|
187
|
+
inputSourceMap: sourceMaps ? chunk.map : void 0,
|
|
188
|
+
presets: [
|
|
189
|
+
[
|
|
190
|
+
() => ({
|
|
191
|
+
plugins: [
|
|
192
|
+
recordAndRemovePolyfillBabelPlugin(legacyPolyfills),
|
|
193
|
+
replaceLegacyEnvBabelPlugin(),
|
|
194
|
+
wrapIIFEBabelPlugin()
|
|
195
|
+
]
|
|
196
|
+
})
|
|
197
|
+
],
|
|
198
|
+
[
|
|
199
|
+
"env",
|
|
200
|
+
createBabelPresetEnvOptions(targets, {
|
|
201
|
+
needPolyfills,
|
|
202
|
+
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
|
|
203
|
+
})
|
|
204
|
+
]
|
|
205
|
+
]
|
|
206
|
+
});
|
|
207
|
+
if (code)
|
|
208
|
+
return { code, map };
|
|
209
|
+
return null;
|
|
210
|
+
},
|
|
211
|
+
transformIndexHtml(html, { chunk }) {
|
|
212
|
+
if (config.build.ssr)
|
|
213
|
+
return;
|
|
214
|
+
if (!chunk)
|
|
215
|
+
return;
|
|
216
|
+
if (chunk.fileName.includes("-legacy")) {
|
|
217
|
+
facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const tags = [];
|
|
221
|
+
const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
|
|
222
|
+
const modernPolyfillFilename = facadeToModernPolyfillMap.get(chunk.facadeModuleId);
|
|
223
|
+
if (modernPolyfillFilename) {
|
|
224
|
+
tags.push({
|
|
225
|
+
tag: "script",
|
|
226
|
+
attrs: {
|
|
227
|
+
type: "module",
|
|
228
|
+
crossorigin: true,
|
|
229
|
+
src: `${config.base}${modernPolyfillFilename}`
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
} else if (modernPolyfills.size) {
|
|
233
|
+
throw new Error(`No corresponding modern polyfill chunk found for ${htmlFilename}`);
|
|
234
|
+
}
|
|
235
|
+
if (!genLegacy) {
|
|
236
|
+
return { html, tags };
|
|
237
|
+
}
|
|
238
|
+
tags.push({
|
|
239
|
+
tag: "script",
|
|
240
|
+
attrs: { nomodule: true },
|
|
241
|
+
children: safari10NoModuleFix,
|
|
242
|
+
injectTo: "body"
|
|
243
|
+
});
|
|
244
|
+
const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(chunk.facadeModuleId);
|
|
245
|
+
if (legacyPolyfillFilename) {
|
|
246
|
+
tags.push({
|
|
247
|
+
tag: "script",
|
|
248
|
+
attrs: {
|
|
249
|
+
nomodule: true,
|
|
250
|
+
crossorigin: true,
|
|
251
|
+
id: legacyPolyfillId,
|
|
252
|
+
src: `${config.base}${legacyPolyfillFilename}`
|
|
253
|
+
},
|
|
254
|
+
injectTo: "body"
|
|
255
|
+
});
|
|
256
|
+
} else if (legacyPolyfills.size) {
|
|
257
|
+
throw new Error(`No corresponding legacy polyfill chunk found for ${htmlFilename}`);
|
|
258
|
+
}
|
|
259
|
+
const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
|
|
260
|
+
if (legacyEntryFilename) {
|
|
261
|
+
const nonBareBase = config.base === "" ? "./" : config.base;
|
|
262
|
+
tags.push({
|
|
263
|
+
tag: "script",
|
|
264
|
+
attrs: {
|
|
265
|
+
nomodule: true,
|
|
266
|
+
crossorigin: true,
|
|
267
|
+
id: legacyEntryId,
|
|
268
|
+
"data-src": nonBareBase + legacyEntryFilename
|
|
269
|
+
},
|
|
270
|
+
children: systemJSInlineCode,
|
|
271
|
+
injectTo: "body"
|
|
272
|
+
});
|
|
273
|
+
} else {
|
|
274
|
+
throw new Error(`No corresponding legacy entry chunk found for ${htmlFilename}`);
|
|
275
|
+
}
|
|
276
|
+
if (genDynamicFallback && legacyPolyfillFilename && legacyEntryFilename) {
|
|
277
|
+
tags.push({
|
|
278
|
+
tag: "script",
|
|
279
|
+
attrs: { type: "module" },
|
|
280
|
+
children: detectModernBrowserCode,
|
|
281
|
+
injectTo: "head"
|
|
282
|
+
});
|
|
283
|
+
tags.push({
|
|
284
|
+
tag: "script",
|
|
285
|
+
attrs: { type: "module" },
|
|
286
|
+
children: dynamicFallbackInlineCode,
|
|
287
|
+
injectTo: "head"
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
html,
|
|
292
|
+
tags
|
|
293
|
+
};
|
|
294
|
+
},
|
|
295
|
+
generateBundle(opts, bundle) {
|
|
296
|
+
if (config.build.ssr) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (isLegacyBundle(bundle, opts)) {
|
|
300
|
+
for (const name in bundle) {
|
|
301
|
+
if (bundle[name].type === "asset") {
|
|
302
|
+
delete bundle[name];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
let envInjectionFailed = false;
|
|
309
|
+
const legacyEnvPlugin = {
|
|
310
|
+
name: "vite:legacy-env",
|
|
311
|
+
config(config2, env) {
|
|
312
|
+
if (env) {
|
|
313
|
+
return {
|
|
314
|
+
define: {
|
|
315
|
+
"import.meta.env.LEGACY": env.command === "serve" || config2.build?.ssr ? false : legacyEnvVarMarker
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
} else {
|
|
319
|
+
envInjectionFailed = true;
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
configResolved(config2) {
|
|
323
|
+
if (envInjectionFailed) {
|
|
324
|
+
config2.logger.warn(`[@vitejs/plugin-legacy] import.meta.env.LEGACY was not injected due to incompatible vite version (requires vite@^2.0.0-beta.69).`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
return [
|
|
329
|
+
legacyConfigPlugin,
|
|
330
|
+
legacyGenerateBundlePlugin,
|
|
331
|
+
legacyPostPlugin,
|
|
332
|
+
legacyEnvPlugin
|
|
333
|
+
];
|
|
334
|
+
}
|
|
335
|
+
async function detectPolyfills(code, targets, list) {
|
|
336
|
+
const babel2 = await loadBabel();
|
|
337
|
+
const { ast } = babel2.transform(code, {
|
|
338
|
+
ast: true,
|
|
339
|
+
babelrc: false,
|
|
340
|
+
configFile: false,
|
|
341
|
+
presets: [
|
|
342
|
+
[
|
|
343
|
+
"env",
|
|
344
|
+
createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
|
|
345
|
+
]
|
|
346
|
+
]
|
|
347
|
+
});
|
|
348
|
+
for (const node of ast.program.body) {
|
|
349
|
+
if (node.type === "ImportDeclaration") {
|
|
350
|
+
const source = node.source.value;
|
|
351
|
+
if (source.startsWith("core-js/") || source.startsWith("regenerator-runtime/")) {
|
|
352
|
+
list.add(source);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
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) {
|
|
376
|
+
let { minify, assetsDir } = buildOptions;
|
|
377
|
+
minify = minify ? "terser" : false;
|
|
378
|
+
const res = await vite.build({
|
|
379
|
+
root: path__default.dirname(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
|
+
configFile: false,
|
|
381
|
+
logLevel: "error",
|
|
382
|
+
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
|
|
383
|
+
build: {
|
|
384
|
+
write: false,
|
|
385
|
+
target: false,
|
|
386
|
+
minify,
|
|
387
|
+
assetsDir,
|
|
388
|
+
rollupOptions: {
|
|
389
|
+
input: {
|
|
390
|
+
polyfills: polyfillId
|
|
391
|
+
},
|
|
392
|
+
output: {
|
|
393
|
+
format,
|
|
394
|
+
entryFileNames: rollupOutputOptions.entryFileNames,
|
|
395
|
+
manualChunks: void 0
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
const _polyfillChunk = Array.isArray(res) ? res[0] : res;
|
|
401
|
+
if (!("output" in _polyfillChunk))
|
|
402
|
+
return;
|
|
403
|
+
const polyfillChunk = _polyfillChunk.output[0];
|
|
404
|
+
for (const key in bundle) {
|
|
405
|
+
const chunk = bundle[key];
|
|
406
|
+
if (chunk.type === "chunk" && chunk.facadeModuleId) {
|
|
407
|
+
facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
bundle[polyfillChunk.fileName] = polyfillChunk;
|
|
411
|
+
}
|
|
412
|
+
const polyfillId = "\0vite/legacy-polyfills";
|
|
413
|
+
function polyfillsPlugin(imports, excludeSystemJS) {
|
|
414
|
+
return {
|
|
415
|
+
name: "vite:legacy-polyfills",
|
|
416
|
+
resolveId(id) {
|
|
417
|
+
if (id === polyfillId) {
|
|
418
|
+
return id;
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
load(id) {
|
|
422
|
+
if (id === polyfillId) {
|
|
423
|
+
return [...imports].map((i) => `import "${i}";`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
function isLegacyChunk(chunk, options) {
|
|
429
|
+
return options.format === "system" && chunk.fileName.includes("-legacy");
|
|
430
|
+
}
|
|
431
|
+
function isLegacyBundle(bundle, options) {
|
|
432
|
+
if (options.format === "system") {
|
|
433
|
+
const entryChunk = Object.values(bundle).find((output) => output.type === "chunk" && output.isEntry);
|
|
434
|
+
return !!entryChunk && entryChunk.fileName.includes("-legacy");
|
|
435
|
+
}
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
function recordAndRemovePolyfillBabelPlugin(polyfills) {
|
|
439
|
+
return ({ types: t }) => ({
|
|
440
|
+
name: "vite-remove-polyfill-import",
|
|
441
|
+
post({ path: path2 }) {
|
|
442
|
+
path2.get("body").forEach((p) => {
|
|
443
|
+
if (t.isImportDeclaration(p)) {
|
|
444
|
+
polyfills.add(p.node.source.value);
|
|
445
|
+
p.remove();
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
function replaceLegacyEnvBabelPlugin() {
|
|
452
|
+
return ({ types: t }) => ({
|
|
453
|
+
name: "vite-replace-env-legacy",
|
|
454
|
+
visitor: {
|
|
455
|
+
Identifier(path2) {
|
|
456
|
+
if (path2.node.name === legacyEnvVarMarker) {
|
|
457
|
+
path2.replaceWith(t.booleanLiteral(true));
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
function wrapIIFEBabelPlugin() {
|
|
464
|
+
return ({ types: t, template }) => {
|
|
465
|
+
const buildIIFE = template(";(function(){%%body%%})();");
|
|
466
|
+
return {
|
|
467
|
+
name: "vite-wrap-iife",
|
|
468
|
+
post({ path: path2 }) {
|
|
469
|
+
if (!this.isWrapped) {
|
|
470
|
+
this.isWrapped = true;
|
|
471
|
+
path2.replaceWith(t.program(buildIIFE({ body: path2.node.body })));
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
const cspHashes = [
|
|
478
|
+
crypto.createHash("sha256").update(safari10NoModuleFix).digest("base64"),
|
|
479
|
+
crypto.createHash("sha256").update(systemJSInlineCode).digest("base64"),
|
|
480
|
+
crypto.createHash("sha256").update(detectModernBrowserCode).digest("base64"),
|
|
481
|
+
crypto.createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
|
|
482
|
+
];
|
|
483
|
+
|
|
484
|
+
module.exports = viteLegacyPlugin;
|
|
485
|
+
module.exports.cspHashes = cspHashes;
|
|
486
|
+
module.exports["default"] = viteLegacyPlugin;
|
|
487
|
+
module.exports.detectPolyfills = detectPolyfills;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
interface Options {
|
|
4
|
+
/**
|
|
5
|
+
* default: 'defaults'
|
|
6
|
+
*/
|
|
7
|
+
targets?: string | string[] | {
|
|
8
|
+
[key: string]: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* default: false
|
|
12
|
+
*/
|
|
13
|
+
ignoreBrowserslistConfig?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* default: true
|
|
16
|
+
*/
|
|
17
|
+
polyfills?: boolean | string[];
|
|
18
|
+
additionalLegacyPolyfills?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* default: false
|
|
21
|
+
*/
|
|
22
|
+
modernPolyfills?: boolean | string[];
|
|
23
|
+
/**
|
|
24
|
+
* default: true
|
|
25
|
+
*/
|
|
26
|
+
renderLegacyChunks?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* default: false
|
|
29
|
+
*/
|
|
30
|
+
externalSystemJS?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare function viteLegacyPlugin(options?: Options): Plugin[];
|
|
34
|
+
declare function detectPolyfills(code: string, targets: any, list: Set<string>): Promise<void>;
|
|
35
|
+
declare const cspHashes: string[];
|
|
36
|
+
|
|
37
|
+
export { cspHashes, viteLegacyPlugin as default, detectPolyfills };
|