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