@vitejs/plugin-legacy 2.0.0-alpha.0 → 2.0.0-alpha.1

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
@@ -122,9 +122,9 @@ The legacy plugin offers a way to use native `import()` in the modern build whil
122
122
 
123
123
  Polyfill specifier strings for `polyfills` and `modernPolyfills` can be either of the following:
124
124
 
125
- - Any [`core-js` 3 sub import paths](https://unpkg.com/browse/core-js@3.8.2/) - e.g. `es/map` will import `core-js/es/map`
125
+ - Any [`core-js` 3 sub import paths](https://unpkg.com/browse/core-js@latest/) - e.g. `es/map` will import `core-js/es/map`
126
126
 
127
- - Any [individual `core-js` 3 modules](https://unpkg.com/browse/core-js@3.8.2/modules/) - e.g. `es.array.iterator` will import `core-js/modules/es.array.iterator.js`
127
+ - Any [individual `core-js` 3 modules](https://unpkg.com/browse/core-js@latest/modules/) - e.g. `es.array.iterator` will import `core-js/modules/es.array.iterator.js`
128
128
 
129
129
  **Example**
130
130
 
package/dist/index.cjs CHANGED
@@ -4,6 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const path = require('path');
6
6
  const crypto = require('crypto');
7
+ const module$1 = require('module');
8
+ const url = require('url');
7
9
  const vite = require('vite');
8
10
  const MagicString = require('magic-string');
9
11
 
@@ -23,9 +25,9 @@ const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("scrip
23
25
  const legacyPolyfillId = "vite-legacy-polyfill";
24
26
  const legacyEntryId = "vite-legacy-entry";
25
27
  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)}();`;
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;`;
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)}();`;
29
31
  const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
30
32
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
31
33
  function viteLegacyPlugin(options = {}) {
@@ -102,6 +104,7 @@ function viteLegacyPlugin(options = {}) {
102
104
  }
103
105
  }
104
106
  };
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)));
105
108
  const legacyPostPlugin = {
106
109
  name: "vite:legacy-post-process",
107
110
  enforce: "post",
@@ -207,7 +210,7 @@ function viteLegacyPlugin(options = {}) {
207
210
  loose: false,
208
211
  useBuiltIns: needPolyfills ? "usage" : false,
209
212
  corejs: needPolyfills ? {
210
- version: require("core-js/package.json").version,
213
+ version: _require("core-js/package.json").version,
211
214
  proposals: false
212
215
  } : void 0,
213
216
  shippedProposals: true,
@@ -285,7 +288,7 @@ function viteLegacyPlugin(options = {}) {
285
288
  tags.push({
286
289
  tag: "script",
287
290
  attrs: { type: "module" },
288
- children: detectDynamicImportCode,
291
+ children: detectModernBrowserCode,
289
292
  injectTo: "head"
290
293
  });
291
294
  tags.push({
@@ -373,7 +376,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
373
376
  let { minify, assetsDir } = buildOptions;
374
377
  minify = minify ? "terser" : false;
375
378
  const res = await vite.build({
376
- root: __dirname,
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)))),
377
380
  configFile: false,
378
381
  logLevel: "error",
379
382
  plugins: [polyfillsPlugin(imports, externalSystemJS)],
@@ -403,7 +406,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
403
406
  facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
404
407
  }
405
408
  }
406
- bundle[polyfillChunk.name] = polyfillChunk;
409
+ bundle[polyfillChunk.fileName] = polyfillChunk;
407
410
  }
408
411
  const polyfillId = "\0vite/legacy-polyfills";
409
412
  function polyfillsPlugin(imports, externalSystemJS) {
@@ -473,7 +476,7 @@ function wrapIIFEBabelPlugin() {
473
476
  const cspHashes = [
474
477
  crypto.createHash("sha256").update(safari10NoModuleFix).digest("base64"),
475
478
  crypto.createHash("sha256").update(systemJSInlineCode).digest("base64"),
476
- crypto.createHash("sha256").update(detectDynamicImportCode).digest("base64"),
479
+ crypto.createHash("sha256").update(detectModernBrowserCode).digest("base64"),
477
480
  crypto.createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
478
481
  ];
479
482
 
package/dist/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  import path from 'path';
2
2
  import { createHash } from 'crypto';
3
+ import { createRequire } from 'module';
4
+ import { fileURLToPath } from 'url';
3
5
  import { build } from 'vite';
4
6
  import MagicString from 'magic-string';
5
7
 
@@ -14,9 +16,9 @@ const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("scrip
14
16
  const legacyPolyfillId = "vite-legacy-polyfill";
15
17
  const legacyEntryId = "vite-legacy-entry";
16
18
  const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`;
17
- const detectDynamicImportVarName = "__vite_is_dynamic_import_support";
18
- const detectDynamicImportCode = `try{import("_").catch(()=>1);}catch(e){}window.${detectDynamicImportVarName}=true;`;
19
- 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)}();`;
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;`;
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)}();`;
20
22
  const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
21
23
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
22
24
  function viteLegacyPlugin(options = {}) {
@@ -93,6 +95,7 @@ function viteLegacyPlugin(options = {}) {
93
95
  }
94
96
  }
95
97
  };
98
+ const _require = createRequire(import.meta.url);
96
99
  const legacyPostPlugin = {
97
100
  name: "vite:legacy-post-process",
98
101
  enforce: "post",
@@ -198,7 +201,7 @@ function viteLegacyPlugin(options = {}) {
198
201
  loose: false,
199
202
  useBuiltIns: needPolyfills ? "usage" : false,
200
203
  corejs: needPolyfills ? {
201
- version: require("core-js/package.json").version,
204
+ version: _require("core-js/package.json").version,
202
205
  proposals: false
203
206
  } : void 0,
204
207
  shippedProposals: true,
@@ -276,7 +279,7 @@ function viteLegacyPlugin(options = {}) {
276
279
  tags.push({
277
280
  tag: "script",
278
281
  attrs: { type: "module" },
279
- children: detectDynamicImportCode,
282
+ children: detectModernBrowserCode,
280
283
  injectTo: "head"
281
284
  });
282
285
  tags.push({
@@ -364,7 +367,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
364
367
  let { minify, assetsDir } = buildOptions;
365
368
  minify = minify ? "terser" : false;
366
369
  const res = await build({
367
- root: __dirname,
370
+ root: path.dirname(fileURLToPath(import.meta.url)),
368
371
  configFile: false,
369
372
  logLevel: "error",
370
373
  plugins: [polyfillsPlugin(imports, externalSystemJS)],
@@ -394,7 +397,7 @@ async function buildPolyfillChunk(name, imports, bundle, facadeToChunkMap, build
394
397
  facadeToChunkMap.set(chunk.facadeModuleId, polyfillChunk.fileName);
395
398
  }
396
399
  }
397
- bundle[polyfillChunk.name] = polyfillChunk;
400
+ bundle[polyfillChunk.fileName] = polyfillChunk;
398
401
  }
399
402
  const polyfillId = "\0vite/legacy-polyfills";
400
403
  function polyfillsPlugin(imports, externalSystemJS) {
@@ -464,7 +467,7 @@ function wrapIIFEBabelPlugin() {
464
467
  const cspHashes = [
465
468
  createHash("sha256").update(safari10NoModuleFix).digest("base64"),
466
469
  createHash("sha256").update(systemJSInlineCode).digest("base64"),
467
- createHash("sha256").update(detectDynamicImportCode).digest("base64"),
470
+ createHash("sha256").update(detectModernBrowserCode).digest("base64"),
468
471
  createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
469
472
  ];
470
473
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.1",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [