@vitejs/plugin-legacy 2.0.0-alpha.1 → 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 CHANGED
@@ -27,6 +27,12 @@ export default {
27
27
  }
28
28
  ```
29
29
 
30
+ Terser must be installed because plugin-legacy uses Terser for minification.
31
+
32
+ ```sh
33
+ npm add -D terser
34
+ ```
35
+
30
36
  ## Options
31
37
 
32
38
  ### `targets`
@@ -147,8 +153,12 @@ The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https
147
153
 
148
154
  - `sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=`
149
155
  - `sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=`
150
- - `sha256-4m6wOIrq/wFDmi9Xh3mFM2mwI4ik9n3TMgHk6xDtLxk=`
151
- - `sha256-uS7/g9fhQwNZS1f/MqYqqKv8y9hCu36IfX9XZB5L7YY=`
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
@@ -26,10 +26,11 @@ const legacyPolyfillId = "vite-legacy-polyfill";
26
26
  const legacyEntryId = "vite-legacy-entry";
27
27
  const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
28
28
  const detectModernBrowserVarName = "__vite_is_modern_browser";
29
- const detectModernBrowserCode = `try{import(new URL(import.meta.url).href).catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
29
+ const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
30
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
31
  const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
32
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)));
33
34
  function viteLegacyPlugin(options = {}) {
34
35
  let config;
35
36
  const targets = options.targets || "defaults";
@@ -41,11 +42,7 @@ function viteLegacyPlugin(options = {}) {
41
42
  const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
42
43
  const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
43
44
  const modernPolyfills = /* @__PURE__ */ new Set();
44
- const DEFAULT_LEGACY_POLYFILL = [
45
- "core-js/modules/es.promise",
46
- "core-js/modules/es.array.iterator"
47
- ];
48
- const legacyPolyfills = new Set(DEFAULT_LEGACY_POLYFILL);
45
+ const legacyPolyfills = /* @__PURE__ */ new Set();
49
46
  if (Array.isArray(options.modernPolyfills)) {
50
47
  options.modernPolyfills.forEach((i) => {
51
48
  modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
@@ -89,22 +86,19 @@ function viteLegacyPlugin(options = {}) {
89
86
  return;
90
87
  }
91
88
  isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
92
- await buildPolyfillChunk("polyfills-modern", modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, options.externalSystemJS);
89
+ await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
93
90
  return;
94
91
  }
95
92
  if (!genLegacy) {
96
93
  return;
97
94
  }
98
95
  if (legacyPolyfills.size || genDynamicFallback) {
99
- if (!legacyPolyfills.has("es.promise")) {
100
- await detectPolyfills(`Promise.resolve()`, targets, legacyPolyfills);
101
- }
96
+ await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
102
97
  isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
103
- await buildPolyfillChunk("polyfills-legacy", legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, options.externalSystemJS);
98
+ await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
104
99
  }
105
100
  }
106
101
  };
107
- const _require = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
108
102
  const legacyPostPlugin = {
109
103
  name: "vite:legacy-post-process",
110
104
  enforce: "post",
@@ -188,7 +182,7 @@ function viteLegacyPlugin(options = {}) {
188
182
  const { code, map } = babel2.transform(raw, {
189
183
  babelrc: false,
190
184
  configFile: false,
191
- compact: true,
185
+ compact: !!config.build.minify,
192
186
  sourceMaps,
193
187
  inputSourceMap: sourceMaps ? chunk.map : void 0,
194
188
  presets: [
@@ -203,19 +197,10 @@ function viteLegacyPlugin(options = {}) {
203
197
  ],
204
198
  [
205
199
  "env",
206
- {
207
- targets,
208
- modules: false,
209
- bugfixes: true,
210
- loose: false,
211
- useBuiltIns: needPolyfills ? "usage" : false,
212
- corejs: needPolyfills ? {
213
- version: _require("core-js/package.json").version,
214
- proposals: false
215
- } : void 0,
216
- shippedProposals: true,
200
+ createBabelPresetEnvOptions(targets, {
201
+ needPolyfills,
217
202
  ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
218
- }
203
+ })
219
204
  ]
220
205
  ]
221
206
  });
@@ -240,6 +225,7 @@ function viteLegacyPlugin(options = {}) {
240
225
  tag: "script",
241
226
  attrs: {
242
227
  type: "module",
228
+ crossorigin: true,
243
229
  src: `${config.base}${modernPolyfillFilename}`
244
230
  }
245
231
  });
@@ -261,6 +247,7 @@ function viteLegacyPlugin(options = {}) {
261
247
  tag: "script",
262
248
  attrs: {
263
249
  nomodule: true,
250
+ crossorigin: true,
264
251
  id: legacyPolyfillId,
265
252
  src: `${config.base}${legacyPolyfillFilename}`
266
253
  },
@@ -271,12 +258,14 @@ function viteLegacyPlugin(options = {}) {
271
258
  }
272
259
  const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
273
260
  if (legacyEntryFilename) {
261
+ const nonBareBase = config.base === "" ? "./" : config.base;
274
262
  tags.push({
275
263
  tag: "script",
276
264
  attrs: {
277
265
  nomodule: true,
266
+ crossorigin: true,
278
267
  id: legacyEntryId,
279
- "data-src": config.base + legacyEntryFilename
268
+ "data-src": nonBareBase + legacyEntryFilename
280
269
  },
281
270
  children: systemJSInlineCode,
282
271
  injectTo: "body"
@@ -352,14 +341,7 @@ async function detectPolyfills(code, targets, list) {
352
341
  presets: [
353
342
  [
354
343
  "env",
355
- {
356
- targets,
357
- modules: false,
358
- useBuiltIns: "usage",
359
- corejs: { version: 3, proposals: false },
360
- shippedProposals: true,
361
- ignoreBrowserslistConfig: true
362
- }
344
+ createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
363
345
  ]
364
346
  ]
365
347
  });
@@ -372,14 +354,32 @@ async function detectPolyfills(code, targets, list) {
372
354
  }
373
355
  }
374
356
  }
375
- async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, buildOptions, externalSystemJS) {
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
376
  let { minify, assetsDir } = buildOptions;
377
377
  minify = minify ? "terser" : false;
378
378
  const res = await vite.build({
379
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
380
  configFile: false,
381
381
  logLevel: "error",
382
- plugins: [polyfillsPlugin(imports, externalSystemJS)],
382
+ plugins: [polyfillsPlugin(imports, excludeSystemJS)],
383
383
  build: {
384
384
  write: false,
385
385
  target: false,
@@ -387,10 +387,11 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
387
387
  assetsDir,
388
388
  rollupOptions: {
389
389
  input: {
390
- [name]: polyfillId
390
+ polyfills: polyfillId
391
391
  },
392
392
  output: {
393
- format: name.includes("legacy") ? "iife" : "es",
393
+ format,
394
+ entryFileNames: rollupOutputOptions.entryFileNames,
394
395
  manualChunks: void 0
395
396
  }
396
397
  }
@@ -409,7 +410,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
409
410
  bundle[polyfillChunk.fileName] = polyfillChunk;
410
411
  }
411
412
  const polyfillId = "\0vite/legacy-polyfills";
412
- function polyfillsPlugin(imports, externalSystemJS) {
413
+ function polyfillsPlugin(imports, excludeSystemJS) {
413
414
  return {
414
415
  name: "vite:legacy-polyfills",
415
416
  resolveId(id) {
@@ -419,7 +420,7 @@ function polyfillsPlugin(imports, externalSystemJS) {
419
420
  },
420
421
  load(id) {
421
422
  if (id === polyfillId) {
422
- return [...imports].map((i) => `import "${i}";`).join("") + (externalSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
423
+ return [...imports].map((i) => `import "${i}";`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
423
424
  }
424
425
  }
425
426
  };
package/dist/index.mjs CHANGED
@@ -17,10 +17,11 @@ const legacyPolyfillId = "vite-legacy-polyfill";
17
17
  const legacyEntryId = "vite-legacy-entry";
18
18
  const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
19
19
  const detectModernBrowserVarName = "__vite_is_modern_browser";
20
- const detectModernBrowserCode = `try{import(new URL(import.meta.url).href).catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
20
+ const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}catch(e){}window.${detectModernBrowserVarName}=true;`;
21
21
  const dynamicFallbackInlineCode = `!function(){if(window.${detectModernBrowserVarName})return;console.warn("vite: loading legacy build because dynamic import or import.meta.url is unsupported, syntax error above should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}();`;
22
22
  const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
23
23
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
24
+ const _require = createRequire(import.meta.url);
24
25
  function viteLegacyPlugin(options = {}) {
25
26
  let config;
26
27
  const targets = options.targets || "defaults";
@@ -32,11 +33,7 @@ function viteLegacyPlugin(options = {}) {
32
33
  const facadeToLegacyPolyfillMap = /* @__PURE__ */ new Map();
33
34
  const facadeToModernPolyfillMap = /* @__PURE__ */ new Map();
34
35
  const modernPolyfills = /* @__PURE__ */ new Set();
35
- const DEFAULT_LEGACY_POLYFILL = [
36
- "core-js/modules/es.promise",
37
- "core-js/modules/es.array.iterator"
38
- ];
39
- const legacyPolyfills = new Set(DEFAULT_LEGACY_POLYFILL);
36
+ const legacyPolyfills = /* @__PURE__ */ new Set();
40
37
  if (Array.isArray(options.modernPolyfills)) {
41
38
  options.modernPolyfills.forEach((i) => {
42
39
  modernPolyfills.add(i.includes("/") ? `core-js/${i}` : `core-js/modules/${i}.js`);
@@ -80,22 +77,19 @@ function viteLegacyPlugin(options = {}) {
80
77
  return;
81
78
  }
82
79
  isDebug && console.log(`[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills);
83
- await buildPolyfillChunk("polyfills-modern", modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, options.externalSystemJS);
80
+ await buildPolyfillChunk(modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, "es", opts, true);
84
81
  return;
85
82
  }
86
83
  if (!genLegacy) {
87
84
  return;
88
85
  }
89
86
  if (legacyPolyfills.size || genDynamicFallback) {
90
- if (!legacyPolyfills.has("es.promise")) {
91
- await detectPolyfills(`Promise.resolve()`, targets, legacyPolyfills);
92
- }
87
+ await detectPolyfills(`Promise.resolve(); Promise.all();`, targets, legacyPolyfills);
93
88
  isDebug && console.log(`[@vitejs/plugin-legacy] legacy polyfills:`, legacyPolyfills);
94
- await buildPolyfillChunk("polyfills-legacy", legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, options.externalSystemJS);
89
+ await buildPolyfillChunk(legacyPolyfills, bundle, facadeToLegacyPolyfillMap, config.build, "iife", opts, options.externalSystemJS);
95
90
  }
96
91
  }
97
92
  };
98
- const _require = createRequire(import.meta.url);
99
93
  const legacyPostPlugin = {
100
94
  name: "vite:legacy-post-process",
101
95
  enforce: "post",
@@ -179,7 +173,7 @@ function viteLegacyPlugin(options = {}) {
179
173
  const { code, map } = babel2.transform(raw, {
180
174
  babelrc: false,
181
175
  configFile: false,
182
- compact: true,
176
+ compact: !!config.build.minify,
183
177
  sourceMaps,
184
178
  inputSourceMap: sourceMaps ? chunk.map : void 0,
185
179
  presets: [
@@ -194,19 +188,10 @@ function viteLegacyPlugin(options = {}) {
194
188
  ],
195
189
  [
196
190
  "env",
197
- {
198
- targets,
199
- modules: false,
200
- bugfixes: true,
201
- loose: false,
202
- useBuiltIns: needPolyfills ? "usage" : false,
203
- corejs: needPolyfills ? {
204
- version: _require("core-js/package.json").version,
205
- proposals: false
206
- } : void 0,
207
- shippedProposals: true,
191
+ createBabelPresetEnvOptions(targets, {
192
+ needPolyfills,
208
193
  ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
209
- }
194
+ })
210
195
  ]
211
196
  ]
212
197
  });
@@ -231,6 +216,7 @@ function viteLegacyPlugin(options = {}) {
231
216
  tag: "script",
232
217
  attrs: {
233
218
  type: "module",
219
+ crossorigin: true,
234
220
  src: `${config.base}${modernPolyfillFilename}`
235
221
  }
236
222
  });
@@ -252,6 +238,7 @@ function viteLegacyPlugin(options = {}) {
252
238
  tag: "script",
253
239
  attrs: {
254
240
  nomodule: true,
241
+ crossorigin: true,
255
242
  id: legacyPolyfillId,
256
243
  src: `${config.base}${legacyPolyfillFilename}`
257
244
  },
@@ -262,12 +249,14 @@ function viteLegacyPlugin(options = {}) {
262
249
  }
263
250
  const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
264
251
  if (legacyEntryFilename) {
252
+ const nonBareBase = config.base === "" ? "./" : config.base;
265
253
  tags.push({
266
254
  tag: "script",
267
255
  attrs: {
268
256
  nomodule: true,
257
+ crossorigin: true,
269
258
  id: legacyEntryId,
270
- "data-src": config.base + legacyEntryFilename
259
+ "data-src": nonBareBase + legacyEntryFilename
271
260
  },
272
261
  children: systemJSInlineCode,
273
262
  injectTo: "body"
@@ -343,14 +332,7 @@ async function detectPolyfills(code, targets, list) {
343
332
  presets: [
344
333
  [
345
334
  "env",
346
- {
347
- targets,
348
- modules: false,
349
- useBuiltIns: "usage",
350
- corejs: { version: 3, proposals: false },
351
- shippedProposals: true,
352
- ignoreBrowserslistConfig: true
353
- }
335
+ createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
354
336
  ]
355
337
  ]
356
338
  });
@@ -363,14 +345,32 @@ async function detectPolyfills(code, targets, list) {
363
345
  }
364
346
  }
365
347
  }
366
- async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, buildOptions, externalSystemJS) {
348
+ function createBabelPresetEnvOptions(targets, {
349
+ needPolyfills = true,
350
+ ignoreBrowserslistConfig
351
+ }) {
352
+ return {
353
+ targets,
354
+ bugfixes: true,
355
+ loose: false,
356
+ modules: false,
357
+ useBuiltIns: needPolyfills ? "usage" : false,
358
+ corejs: needPolyfills ? {
359
+ version: _require("core-js/package.json").version,
360
+ proposals: false
361
+ } : void 0,
362
+ shippedProposals: true,
363
+ ignoreBrowserslistConfig
364
+ };
365
+ }
366
+ async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
367
367
  let { minify, assetsDir } = buildOptions;
368
368
  minify = minify ? "terser" : false;
369
369
  const res = await build({
370
370
  root: path.dirname(fileURLToPath(import.meta.url)),
371
371
  configFile: false,
372
372
  logLevel: "error",
373
- plugins: [polyfillsPlugin(imports, externalSystemJS)],
373
+ plugins: [polyfillsPlugin(imports, excludeSystemJS)],
374
374
  build: {
375
375
  write: false,
376
376
  target: false,
@@ -378,10 +378,11 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
378
378
  assetsDir,
379
379
  rollupOptions: {
380
380
  input: {
381
- [name]: polyfillId
381
+ polyfills: polyfillId
382
382
  },
383
383
  output: {
384
- format: name.includes("legacy") ? "iife" : "es",
384
+ format,
385
+ entryFileNames: rollupOutputOptions.entryFileNames,
385
386
  manualChunks: void 0
386
387
  }
387
388
  }
@@ -400,7 +401,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
400
401
  bundle[polyfillChunk.fileName] = polyfillChunk;
401
402
  }
402
403
  const polyfillId = "\0vite/legacy-polyfills";
403
- function polyfillsPlugin(imports, externalSystemJS) {
404
+ function polyfillsPlugin(imports, excludeSystemJS) {
404
405
  return {
405
406
  name: "vite:legacy-polyfills",
406
407
  resolveId(id) {
@@ -410,7 +411,7 @@ function polyfillsPlugin(imports, externalSystemJS) {
410
411
  },
411
412
  load(id) {
412
413
  if (id === polyfillId) {
413
- return [...imports].map((i) => `import "${i}";`).join("") + (externalSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
414
+ return [...imports].map((i) => `import "${i}";`).join("") + (excludeSystemJS ? "" : `import "systemjs/dist/s.min.js";`);
414
415
  }
415
416
  }
416
417
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.2",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -19,7 +19,7 @@
19
19
  "scripts": {
20
20
  "dev": "unbuild --stub",
21
21
  "build": "unbuild && pnpm run patch-cjs",
22
- "patch-cjs": "ts-node ../../scripts/patchCJS.ts",
22
+ "patch-cjs": "esno ../../scripts/patchCJS.ts",
23
23
  "prepublishOnly": "npm run build"
24
24
  },
25
25
  "engines": {
@@ -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.17.11",
39
- "core-js": "^3.22.4",
40
- "magic-string": "^0.26.1",
38
+ "@babel/standalone": "^7.18.5",
39
+ "core-js": "^3.22.8",
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
- "vite": "workspace:*",
49
- "@babel/core": "^7.17.10"
49
+ "@babel/core": "^7.18.5",
50
+ "vite": "workspace:*"
50
51
  }
51
52
  }