@vitejs/plugin-legacy 2.0.0-beta.1 → 2.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -163,7 +163,7 @@ Run `node --input-type=module -e "import {cspHashes} from '@vitejs/plugin-legacy
163
163
  These values (without the `sha256-` prefix) can also be retrieved via
164
164
 
165
165
  ```js
166
- const { cspHashes } = require('@vitejs/plugin-legacy')
166
+ import { cspHashes } from '@vitejs/plugin-legacy'
167
167
  ```
168
168
 
169
169
  When using the `regenerator-runtime` polyfill, it will attempt to use the `globalThis` object to register itself. If `globalThis` is not available (it is [fairly new](https://caniuse.com/?search=globalThis) and not widely supported, including IE 11), it attempts to perform dynamic `Function(...)` call which violates the CSP. To avoid dynamic `eval` in the absence of `globalThis` consider adding `core-js/proposals/global-this` to `additionalLegacyPolyfills` to define it.
package/dist/index.cjs CHANGED
@@ -33,7 +33,9 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
33
33
  });
34
34
  if (typeof result === "object") {
35
35
  if (result.runtime) {
36
- throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
36
+ throw new Error(
37
+ `{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`
38
+ );
37
39
  }
38
40
  if (typeof result.relative === "boolean") {
39
41
  relative = result.relative;
@@ -49,12 +51,22 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
49
51
  }
50
52
  }
51
53
  function getBaseInHTML(urlRelativePath, config) {
52
- return config.base === "./" || config.base === "" ? path__default.posix.join(path__default.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
54
+ return config.base === "./" || config.base === "" ? path__default.posix.join(
55
+ path__default.posix.relative(urlRelativePath, "").slice(0, -2),
56
+ "./"
57
+ ) : config.base;
53
58
  }
54
59
  function toAssetPathFromHtml(filename, htmlPath, config) {
55
60
  const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
56
61
  const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
57
- return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
62
+ return toOutputFilePathInHtml(
63
+ filename,
64
+ "asset",
65
+ htmlPath,
66
+ "html",
67
+ config,
68
+ toRelative
69
+ );
58
70
  }
59
71
  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()}}();`;
60
72
  const legacyPolyfillId = "vite-legacy-polyfill";
@@ -80,7 +92,9 @@ function viteLegacyPlugin(options = {}) {
80
92
  const legacyPolyfills = /* @__PURE__ */ new Set();
81
93
  if (Array.isArray(options.modernPolyfills)) {
82
94
  options.modernPolyfills.forEach((i) => {
83
- modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
95
+ modernPolyfills.add(
96
+ i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
97
+ );
84
98
  });
85
99
  }
86
100
  if (Array.isArray(options.polyfills)) {
@@ -88,7 +102,9 @@ function viteLegacyPlugin(options = {}) {
88
102
  if (i.startsWith(`regenerator`)) {
89
103
  legacyPolyfills.add(`regenerator-runtime/runtime.js`);
90
104
  } else {
91
- legacyPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
105
+ legacyPolyfills.add(
106
+ i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
107
+ );
92
108
  }
93
109
  });
94
110
  }
@@ -99,14 +115,20 @@ function viteLegacyPlugin(options = {}) {
99
115
  }
100
116
  const legacyConfigPlugin = {
101
117
  name: "vite:legacy-config",
102
- apply: "build",
103
- config(config2) {
104
- if (!config2.build) {
105
- config2.build = {};
106
- }
107
- if (!config2.build.cssTarget) {
108
- config2.build.cssTarget = "chrome61";
118
+ config(config2, env) {
119
+ if (env.command === "build") {
120
+ if (!config2.build) {
121
+ config2.build = {};
122
+ }
123
+ if (!config2.build.cssTarget) {
124
+ config2.build.cssTarget = "chrome61";
125
+ }
109
126
  }
127
+ return {
128
+ define: {
129
+ "import.meta.env.LEGACY": env.command === "serve" || config2.build?.ssr ? false : legacyEnvVarMarker
130
+ }
131
+ };
110
132
  }
111
133
  };
112
134
  const legacyGenerateBundlePlugin = {
@@ -120,17 +142,45 @@ function viteLegacyPlugin(options = {}) {
120
142
  if (!modernPolyfills.size) {
121
143
  return;
122
144
  }
123
- isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
124
- await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
145
+ isDebug && console.log(
146
+ `[@vitejs/plugin-legacy] modern polyfills:`,
147
+ modernPolyfills
148
+ );
149
+ await buildPolyfillChunk(
150
+ config.mode,
151
+ modernPolyfills,
152
+ bundle,
153
+ facadeToModernPolyfillMap,
154
+ config.build,
155
+ "es",
156
+ opts,
157
+ true
158
+ );
125
159
  return;
126
160
  }
127
161
  if (!genLegacy) {
128
162
  return;
129
163
  }
130
164
  if (legacyPolyfills.size || genDynamicFallback) {
131
- await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
132
- isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
133
- await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
165
+ await detectPolyfills(
166
+ `Promise.resolve(); Promise.all();`,
167
+ targets,
168
+ legacyPolyfills
169
+ );
170
+ isDebug && console.log(
171
+ `[@vitejs/plugin-legacy] legacy polyfills:`,
172
+ legacyPolyfills
173
+ );
174
+ await buildPolyfillChunk(
175
+ config.mode,
176
+ legacyPolyfills,
177
+ bundle,
178
+ facadeToLegacyPolyfillMap,
179
+ config.build,
180
+ "iife",
181
+ opts,
182
+ options.externalSystemJS
183
+ );
134
184
  }
135
185
  }
136
186
  };
@@ -192,7 +242,11 @@ function viteLegacyPlugin(options = {}) {
192
242
  const re = new RegExp(legacyEnvVarMarker, "g");
193
243
  let match;
194
244
  while (match = re.exec(raw)) {
195
- ms.overwrite(match.index, match.index + legacyEnvVarMarker.length, `false`);
245
+ ms.overwrite(
246
+ match.index,
247
+ match.index + legacyEnvVarMarker.length,
248
+ `false`
249
+ );
196
250
  }
197
251
  }
198
252
  if (config.build.sourcemap) {
@@ -254,18 +308,26 @@ function viteLegacyPlugin(options = {}) {
254
308
  }
255
309
  const tags = [];
256
310
  const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
257
- const modernPolyfillFilename = facadeToModernPolyfillMap.get(chunk.facadeModuleId);
311
+ const modernPolyfillFilename = facadeToModernPolyfillMap.get(
312
+ chunk.facadeModuleId
313
+ );
258
314
  if (modernPolyfillFilename) {
259
315
  tags.push({
260
316
  tag: "script",
261
317
  attrs: {
262
318
  type: "module",
263
319
  crossorigin: true,
264
- src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
320
+ src: toAssetPathFromHtml(
321
+ modernPolyfillFilename,
322
+ chunk.facadeModuleId,
323
+ config
324
+ )
265
325
  }
266
326
  });
267
327
  } else if (modernPolyfills.size) {
268
- throw new Error(`No corresponding modern polyfill chunk found for ${htmlFilename}`);
328
+ throw new Error(
329
+ `No corresponding modern polyfill chunk found for ${htmlFilename}`
330
+ );
269
331
  }
270
332
  if (!genLegacy) {
271
333
  return { html, tags };
@@ -276,7 +338,9 @@ function viteLegacyPlugin(options = {}) {
276
338
  children: safari10NoModuleFix,
277
339
  injectTo: "body"
278
340
  });
279
- const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(chunk.facadeModuleId);
341
+ const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(
342
+ chunk.facadeModuleId
343
+ );
280
344
  if (legacyPolyfillFilename) {
281
345
  tags.push({
282
346
  tag: "script",
@@ -284,14 +348,22 @@ function viteLegacyPlugin(options = {}) {
284
348
  nomodule: true,
285
349
  crossorigin: true,
286
350
  id: legacyPolyfillId,
287
- src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
351
+ src: toAssetPathFromHtml(
352
+ legacyPolyfillFilename,
353
+ chunk.facadeModuleId,
354
+ config
355
+ )
288
356
  },
289
357
  injectTo: "body"
290
358
  });
291
359
  } else if (legacyPolyfills.size) {
292
- throw new Error(`No corresponding legacy polyfill chunk found for ${htmlFilename}`);
360
+ throw new Error(
361
+ `No corresponding legacy polyfill chunk found for ${htmlFilename}`
362
+ );
293
363
  }
294
- const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
364
+ const legacyEntryFilename = facadeToLegacyChunkMap.get(
365
+ chunk.facadeModuleId
366
+ );
295
367
  if (legacyEntryFilename) {
296
368
  tags.push({
297
369
  tag: "script",
@@ -299,13 +371,19 @@ function viteLegacyPlugin(options = {}) {
299
371
  nomodule: true,
300
372
  crossorigin: true,
301
373
  id: legacyEntryId,
302
- "data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
374
+ "data-src": toAssetPathFromHtml(
375
+ legacyEntryFilename,
376
+ chunk.facadeModuleId,
377
+ config
378
+ )
303
379
  },
304
380
  children: systemJSInlineCode,
305
381
  injectTo: "body"
306
382
  });
307
383
  } else {
308
- throw new Error(`No corresponding legacy entry chunk found for ${htmlFilename}`);
384
+ throw new Error(
385
+ `No corresponding legacy entry chunk found for ${htmlFilename}`
386
+ );
309
387
  }
310
388
  if (genDynamicFallback && legacyPolyfillFilename && legacyEntryFilename) {
311
389
  tags.push({
@@ -339,32 +417,7 @@ function viteLegacyPlugin(options = {}) {
339
417
  }
340
418
  }
341
419
  };
342
- let envInjectionFailed = false;
343
- const legacyEnvPlugin = {
344
- name: "vite:legacy-env",
345
- config(config2, env) {
346
- if (env) {
347
- return {
348
- define: {
349
- "import.meta.env.LEGACY": env.command === "serve" || config2.build?.ssr ? false : legacyEnvVarMarker
350
- }
351
- };
352
- } else {
353
- envInjectionFailed = true;
354
- }
355
- },
356
- configResolved(config2) {
357
- if (envInjectionFailed) {
358
- 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).`);
359
- }
360
- }
361
- };
362
- return [
363
- legacyConfigPlugin,
364
- legacyGenerateBundlePlugin,
365
- legacyPostPlugin,
366
- legacyEnvPlugin
367
- ];
420
+ return [legacyConfigPlugin, legacyGenerateBundlePlugin, legacyPostPlugin];
368
421
  }
369
422
  async function detectPolyfills(code, targets, list) {
370
423
  const babel2 = await loadBabel();
@@ -406,17 +459,17 @@ function createBabelPresetEnvOptions(targets, {
406
459
  ignoreBrowserslistConfig
407
460
  };
408
461
  }
409
- async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
462
+ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
410
463
  let { minify, assetsDir } = buildOptions;
411
464
  minify = minify ? "terser" : false;
412
465
  const res = await vite.build({
466
+ mode,
413
467
  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)))),
414
468
  configFile: false,
415
469
  logLevel: "error",
416
470
  plugins: [polyfillsPlugin(imports, excludeSystemJS)],
417
471
  build: {
418
472
  write: false,
419
- target: "es5",
420
473
  minify,
421
474
  assetsDir,
422
475
  rollupOptions: {
@@ -425,10 +478,15 @@ async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOption
425
478
  },
426
479
  output: {
427
480
  format,
428
- entryFileNames: rollupOutputOptions.entryFileNames,
429
- manualChunks: void 0
481
+ entryFileNames: rollupOutputOptions.entryFileNames
430
482
  }
431
483
  }
484
+ },
485
+ esbuild: false,
486
+ optimizeDeps: {
487
+ esbuildOptions: {
488
+ target: "es5"
489
+ }
432
490
  }
433
491
  });
434
492
  const _polyfillChunk = Array.isArray(res) ? res[0] : res;
@@ -464,7 +522,9 @@ function isLegacyChunk(chunk, options) {
464
522
  }
465
523
  function isLegacyBundle(bundle, options) {
466
524
  if (options.format === "system") {
467
- const entryChunk = Object.values(bundle).find((output) => output.type === "chunk" && output.isEntry);
525
+ const entryChunk = Object.values(bundle).find(
526
+ (output) => output.type === "chunk" && output.isEntry
527
+ );
468
528
  return !!entryChunk && entryChunk.fileName.includes("-legacy");
469
529
  }
470
530
  return false;
package/dist/index.mjs CHANGED
@@ -24,7 +24,9 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
24
24
  });
25
25
  if (typeof result === "object") {
26
26
  if (result.runtime) {
27
- throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
27
+ throw new Error(
28
+ `{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`
29
+ );
28
30
  }
29
31
  if (typeof result.relative === "boolean") {
30
32
  relative = result.relative;
@@ -40,12 +42,22 @@ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRela
40
42
  }
41
43
  }
42
44
  function getBaseInHTML(urlRelativePath, config) {
43
- return config.base === "./" || config.base === "" ? path.posix.join(path.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
45
+ return config.base === "./" || config.base === "" ? path.posix.join(
46
+ path.posix.relative(urlRelativePath, "").slice(0, -2),
47
+ "./"
48
+ ) : config.base;
44
49
  }
45
50
  function toAssetPathFromHtml(filename, htmlPath, config) {
46
51
  const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
47
52
  const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
48
- return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
53
+ return toOutputFilePathInHtml(
54
+ filename,
55
+ "asset",
56
+ htmlPath,
57
+ "html",
58
+ config,
59
+ toRelative
60
+ );
49
61
  }
50
62
  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()}}();`;
51
63
  const legacyPolyfillId = "vite-legacy-polyfill";
@@ -71,7 +83,9 @@ function viteLegacyPlugin(options = {}) {
71
83
  const legacyPolyfills = /* @__PURE__ */ new Set();
72
84
  if (Array.isArray(options.modernPolyfills)) {
73
85
  options.modernPolyfills.forEach((i) => {
74
- modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
86
+ modernPolyfills.add(
87
+ i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
88
+ );
75
89
  });
76
90
  }
77
91
  if (Array.isArray(options.polyfills)) {
@@ -79,7 +93,9 @@ function viteLegacyPlugin(options = {}) {
79
93
  if (i.startsWith(`regenerator`)) {
80
94
  legacyPolyfills.add(`regenerator-runtime/runtime.js`);
81
95
  } else {
82
- legacyPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
96
+ legacyPolyfills.add(
97
+ i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`
98
+ );
83
99
  }
84
100
  });
85
101
  }
@@ -90,14 +106,20 @@ function viteLegacyPlugin(options = {}) {
90
106
  }
91
107
  const legacyConfigPlugin = {
92
108
  name: "vite:legacy-config",
93
- apply: "build",
94
- config(config2) {
95
- if (!config2.build) {
96
- config2.build = {};
97
- }
98
- if (!config2.build.cssTarget) {
99
- config2.build.cssTarget = "chrome61";
109
+ config(config2, env) {
110
+ if (env.command === "build") {
111
+ if (!config2.build) {
112
+ config2.build = {};
113
+ }
114
+ if (!config2.build.cssTarget) {
115
+ config2.build.cssTarget = "chrome61";
116
+ }
100
117
  }
118
+ return {
119
+ define: {
120
+ "import.meta.env.LEGACY": env.command === "serve" || config2.build?.ssr ? false : legacyEnvVarMarker
121
+ }
122
+ };
101
123
  }
102
124
  };
103
125
  const legacyGenerateBundlePlugin = {
@@ -111,17 +133,45 @@ function viteLegacyPlugin(options = {}) {
111
133
  if (!modernPolyfills.size) {
112
134
  return;
113
135
  }
114
- isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
115
- await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
136
+ isDebug && console.log(
137
+ `[@vitejs/plugin-legacy] modern polyfills:`,
138
+ modernPolyfills
139
+ );
140
+ await buildPolyfillChunk(
141
+ config.mode,
142
+ modernPolyfills,
143
+ bundle,
144
+ facadeToModernPolyfillMap,
145
+ config.build,
146
+ "es",
147
+ opts,
148
+ true
149
+ );
116
150
  return;
117
151
  }
118
152
  if (!genLegacy) {
119
153
  return;
120
154
  }
121
155
  if (legacyPolyfills.size || genDynamicFallback) {
122
- await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
123
- isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
124
- await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
156
+ await detectPolyfills(
157
+ `Promise.resolve(); Promise.all();`,
158
+ targets,
159
+ legacyPolyfills
160
+ );
161
+ isDebug && console.log(
162
+ `[@vitejs/plugin-legacy] legacy polyfills:`,
163
+ legacyPolyfills
164
+ );
165
+ await buildPolyfillChunk(
166
+ config.mode,
167
+ legacyPolyfills,
168
+ bundle,
169
+ facadeToLegacyPolyfillMap,
170
+ config.build,
171
+ "iife",
172
+ opts,
173
+ options.externalSystemJS
174
+ );
125
175
  }
126
176
  }
127
177
  };
@@ -183,7 +233,11 @@ function viteLegacyPlugin(options = {}) {
183
233
  const re = new RegExp(legacyEnvVarMarker, "g");
184
234
  let match;
185
235
  while (match = re.exec(raw)) {
186
- ms.overwrite(match.index, match.index + legacyEnvVarMarker.length, `false`);
236
+ ms.overwrite(
237
+ match.index,
238
+ match.index + legacyEnvVarMarker.length,
239
+ `false`
240
+ );
187
241
  }
188
242
  }
189
243
  if (config.build.sourcemap) {
@@ -245,18 +299,26 @@ function viteLegacyPlugin(options = {}) {
245
299
  }
246
300
  const tags = [];
247
301
  const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, "");
248
- const modernPolyfillFilename = facadeToModernPolyfillMap.get(chunk.facadeModuleId);
302
+ const modernPolyfillFilename = facadeToModernPolyfillMap.get(
303
+ chunk.facadeModuleId
304
+ );
249
305
  if (modernPolyfillFilename) {
250
306
  tags.push({
251
307
  tag: "script",
252
308
  attrs: {
253
309
  type: "module",
254
310
  crossorigin: true,
255
- src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
311
+ src: toAssetPathFromHtml(
312
+ modernPolyfillFilename,
313
+ chunk.facadeModuleId,
314
+ config
315
+ )
256
316
  }
257
317
  });
258
318
  } else if (modernPolyfills.size) {
259
- throw new Error(`No corresponding modern polyfill chunk found for ${htmlFilename}`);
319
+ throw new Error(
320
+ `No corresponding modern polyfill chunk found for ${htmlFilename}`
321
+ );
260
322
  }
261
323
  if (!genLegacy) {
262
324
  return { html, tags };
@@ -267,7 +329,9 @@ function viteLegacyPlugin(options = {}) {
267
329
  children: safari10NoModuleFix,
268
330
  injectTo: "body"
269
331
  });
270
- const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(chunk.facadeModuleId);
332
+ const legacyPolyfillFilename = facadeToLegacyPolyfillMap.get(
333
+ chunk.facadeModuleId
334
+ );
271
335
  if (legacyPolyfillFilename) {
272
336
  tags.push({
273
337
  tag: "script",
@@ -275,14 +339,22 @@ function viteLegacyPlugin(options = {}) {
275
339
  nomodule: true,
276
340
  crossorigin: true,
277
341
  id: legacyPolyfillId,
278
- src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
342
+ src: toAssetPathFromHtml(
343
+ legacyPolyfillFilename,
344
+ chunk.facadeModuleId,
345
+ config
346
+ )
279
347
  },
280
348
  injectTo: "body"
281
349
  });
282
350
  } else if (legacyPolyfills.size) {
283
- throw new Error(`No corresponding legacy polyfill chunk found for ${htmlFilename}`);
351
+ throw new Error(
352
+ `No corresponding legacy polyfill chunk found for ${htmlFilename}`
353
+ );
284
354
  }
285
- const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
355
+ const legacyEntryFilename = facadeToLegacyChunkMap.get(
356
+ chunk.facadeModuleId
357
+ );
286
358
  if (legacyEntryFilename) {
287
359
  tags.push({
288
360
  tag: "script",
@@ -290,13 +362,19 @@ function viteLegacyPlugin(options = {}) {
290
362
  nomodule: true,
291
363
  crossorigin: true,
292
364
  id: legacyEntryId,
293
- "data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
365
+ "data-src": toAssetPathFromHtml(
366
+ legacyEntryFilename,
367
+ chunk.facadeModuleId,
368
+ config
369
+ )
294
370
  },
295
371
  children: systemJSInlineCode,
296
372
  injectTo: "body"
297
373
  });
298
374
  } else {
299
- throw new Error(`No corresponding legacy entry chunk found for ${htmlFilename}`);
375
+ throw new Error(
376
+ `No corresponding legacy entry chunk found for ${htmlFilename}`
377
+ );
300
378
  }
301
379
  if (genDynamicFallback && legacyPolyfillFilename && legacyEntryFilename) {
302
380
  tags.push({
@@ -330,32 +408,7 @@ function viteLegacyPlugin(options = {}) {
330
408
  }
331
409
  }
332
410
  };
333
- let envInjectionFailed = false;
334
- const legacyEnvPlugin = {
335
- name: "vite:legacy-env",
336
- config(config2, env) {
337
- if (env) {
338
- return {
339
- define: {
340
- "import.meta.env.LEGACY": env.command === "serve" || config2.build?.ssr ? false : legacyEnvVarMarker
341
- }
342
- };
343
- } else {
344
- envInjectionFailed = true;
345
- }
346
- },
347
- configResolved(config2) {
348
- if (envInjectionFailed) {
349
- 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).`);
350
- }
351
- }
352
- };
353
- return [
354
- legacyConfigPlugin,
355
- legacyGenerateBundlePlugin,
356
- legacyPostPlugin,
357
- legacyEnvPlugin
358
- ];
411
+ return [legacyConfigPlugin, legacyGenerateBundlePlugin, legacyPostPlugin];
359
412
  }
360
413
  async function detectPolyfills(code, targets, list) {
361
414
  const babel2 = await loadBabel();
@@ -397,17 +450,17 @@ function createBabelPresetEnvOptions(targets, {
397
450
  ignoreBrowserslistConfig
398
451
  };
399
452
  }
400
- async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
453
+ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
401
454
  let { minify, assetsDir } = buildOptions;
402
455
  minify = minify ? "terser" : false;
403
456
  const res = await build({
457
+ mode,
404
458
  root: path.dirname(fileURLToPath(import.meta.url)),
405
459
  configFile: false,
406
460
  logLevel: "error",
407
461
  plugins: [polyfillsPlugin(imports, excludeSystemJS)],
408
462
  build: {
409
463
  write: false,
410
- target: "es5",
411
464
  minify,
412
465
  assetsDir,
413
466
  rollupOptions: {
@@ -416,10 +469,15 @@ async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOption
416
469
  },
417
470
  output: {
418
471
  format,
419
- entryFileNames: rollupOutputOptions.entryFileNames,
420
- manualChunks: void 0
472
+ entryFileNames: rollupOutputOptions.entryFileNames
421
473
  }
422
474
  }
475
+ },
476
+ esbuild: false,
477
+ optimizeDeps: {
478
+ esbuildOptions: {
479
+ target: "es5"
480
+ }
423
481
  }
424
482
  });
425
483
  const _polyfillChunk = Array.isArray(res) ? res[0] : res;
@@ -455,7 +513,9 @@ function isLegacyChunk(chunk, options) {
455
513
  }
456
514
  function isLegacyBundle(bundle, options) {
457
515
  if (options.format === "system") {
458
- const entryChunk = Object.values(bundle).find((output) => output.type === "chunk" && output.isEntry);
516
+ const entryChunk = Object.values(bundle).find(
517
+ (output) => output.type === "chunk" && output.isEntry
518
+ );
459
519
  return !!entryChunk && entryChunk.fileName.includes("-legacy");
460
520
  }
461
521
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.1.0-beta.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -23,7 +23,7 @@
23
23
  "prepublishOnly": "npm run build"
24
24
  },
25
25
  "engines": {
26
- "node": ">=14.18.0"
26
+ "node": "^14.18.0 || >=16.0.0"
27
27
  },
28
28
  "repository": {
29
29
  "type": "git",
@@ -35,18 +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.18.7",
39
- "core-js": "^3.23.1",
38
+ "@babel/standalone": "^7.18.13",
39
+ "core-js": "^3.25.0",
40
40
  "magic-string": "^0.26.2",
41
41
  "regenerator-runtime": "^0.13.9",
42
- "systemjs": "^6.12.1"
42
+ "systemjs": "^6.12.4"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "terser": "^5.4.0",
46
- "vite": "^3.0.0-alpha"
46
+ "vite": "^3.0.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@babel/core": "^7.18.6",
49
+ "@babel/core": "^7.18.13",
50
50
  "vite": "workspace:*"
51
51
  }
52
52
  }