@vitejs/plugin-legacy 2.0.0-alpha.2 → 2.0.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/dist/index.cjs CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- const path = require('path');
6
- const crypto = require('crypto');
7
- const module$1 = require('module');
8
- const url = require('url');
5
+ const path = require('node:path');
6
+ const node_crypto = require('node:crypto');
7
+ const node_module = require('node:module');
8
+ const node_url = require('node:url');
9
9
  const vite = require('vite');
10
10
  const MagicString = require('magic-string');
11
11
 
@@ -21,6 +21,16 @@ async function loadBabel() {
21
21
  }
22
22
  return babel;
23
23
  }
24
+ function getBaseInHTML(urlRelativePath, baseOptions, config) {
25
+ return baseOptions.url ?? (baseOptions.relative ? path__default.posix.join(path__default.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base);
26
+ }
27
+ function getAssetsBase(urlRelativePath, config) {
28
+ return getBaseInHTML(urlRelativePath, config.experimental.buildAdvancedBaseOptions.assets, config);
29
+ }
30
+ function toAssetPathFromHtml(filename, htmlPath, config) {
31
+ const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
32
+ return getAssetsBase(relativeUrlPath, config) + filename;
33
+ }
24
34
  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
35
  const legacyPolyfillId = "vite-legacy-polyfill";
26
36
  const legacyEntryId = "vite-legacy-entry";
@@ -30,7 +40,7 @@ const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}c
30
40
  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
41
  const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
32
42
  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)));
43
+ const _require = node_module.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
44
  function viteLegacyPlugin(options = {}) {
35
45
  let config;
36
46
  const targets = options.targets || "defaults";
@@ -226,7 +236,7 @@ function viteLegacyPlugin(options = {}) {
226
236
  attrs: {
227
237
  type: "module",
228
238
  crossorigin: true,
229
- src: `${config.base}${modernPolyfillFilename}`
239
+ src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
230
240
  }
231
241
  });
232
242
  } else if (modernPolyfills.size) {
@@ -249,7 +259,7 @@ function viteLegacyPlugin(options = {}) {
249
259
  nomodule: true,
250
260
  crossorigin: true,
251
261
  id: legacyPolyfillId,
252
- src: `${config.base}${legacyPolyfillFilename}`
262
+ src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
253
263
  },
254
264
  injectTo: "body"
255
265
  });
@@ -258,14 +268,13 @@ function viteLegacyPlugin(options = {}) {
258
268
  }
259
269
  const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
260
270
  if (legacyEntryFilename) {
261
- const nonBareBase = config.base === "" ? "./" : config.base;
262
271
  tags.push({
263
272
  tag: "script",
264
273
  attrs: {
265
274
  nomodule: true,
266
275
  crossorigin: true,
267
276
  id: legacyEntryId,
268
- "data-src": nonBareBase + legacyEntryFilename
277
+ "data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
269
278
  },
270
279
  children: systemJSInlineCode,
271
280
  injectTo: "body"
@@ -376,13 +385,13 @@ async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOption
376
385
  let { minify, assetsDir } = buildOptions;
377
386
  minify = minify ? "terser" : false;
378
387
  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)))),
388
+ 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)))),
380
389
  configFile: false,
381
390
  logLevel: "error",
382
391
  plugins: [polyfillsPlugin(imports, excludeSystemJS)],
383
392
  build: {
384
393
  write: false,
385
- target: false,
394
+ target: "es5",
386
395
  minify,
387
396
  assetsDir,
388
397
  rollupOptions: {
@@ -475,10 +484,10 @@ function wrapIIFEBabelPlugin() {
475
484
  };
476
485
  }
477
486
  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")
487
+ node_crypto.createHash("sha256").update(safari10NoModuleFix).digest("base64"),
488
+ node_crypto.createHash("sha256").update(systemJSInlineCode).digest("base64"),
489
+ node_crypto.createHash("sha256").update(detectModernBrowserCode).digest("base64"),
490
+ node_crypto.createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
482
491
  ];
483
492
 
484
493
  module.exports = viteLegacyPlugin;
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
- import path from 'path';
2
- import { createHash } from 'crypto';
3
- import { createRequire } from 'module';
4
- import { fileURLToPath } from 'url';
5
- import { build } from 'vite';
1
+ import path from 'node:path';
2
+ import { createHash } from 'node:crypto';
3
+ import { createRequire } from 'node:module';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { build, normalizePath } from 'vite';
6
6
  import MagicString from 'magic-string';
7
7
 
8
8
  let babel;
@@ -12,6 +12,16 @@ async function loadBabel() {
12
12
  }
13
13
  return babel;
14
14
  }
15
+ function getBaseInHTML(urlRelativePath, baseOptions, config) {
16
+ return baseOptions.url ?? (baseOptions.relative ? path.posix.join(path.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base);
17
+ }
18
+ function getAssetsBase(urlRelativePath, config) {
19
+ return getBaseInHTML(urlRelativePath, config.experimental.buildAdvancedBaseOptions.assets, config);
20
+ }
21
+ function toAssetPathFromHtml(filename, htmlPath, config) {
22
+ const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
23
+ return getAssetsBase(relativeUrlPath, config) + filename;
24
+ }
15
25
  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()}}();`;
16
26
  const legacyPolyfillId = "vite-legacy-polyfill";
17
27
  const legacyEntryId = "vite-legacy-entry";
@@ -217,7 +227,7 @@ function viteLegacyPlugin(options = {}) {
217
227
  attrs: {
218
228
  type: "module",
219
229
  crossorigin: true,
220
- src: `${config.base}${modernPolyfillFilename}`
230
+ src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
221
231
  }
222
232
  });
223
233
  } else if (modernPolyfills.size) {
@@ -240,7 +250,7 @@ function viteLegacyPlugin(options = {}) {
240
250
  nomodule: true,
241
251
  crossorigin: true,
242
252
  id: legacyPolyfillId,
243
- src: `${config.base}${legacyPolyfillFilename}`
253
+ src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
244
254
  },
245
255
  injectTo: "body"
246
256
  });
@@ -249,14 +259,13 @@ function viteLegacyPlugin(options = {}) {
249
259
  }
250
260
  const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
251
261
  if (legacyEntryFilename) {
252
- const nonBareBase = config.base === "" ? "./" : config.base;
253
262
  tags.push({
254
263
  tag: "script",
255
264
  attrs: {
256
265
  nomodule: true,
257
266
  crossorigin: true,
258
267
  id: legacyEntryId,
259
- "data-src": nonBareBase + legacyEntryFilename
268
+ "data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
260
269
  },
261
270
  children: systemJSInlineCode,
262
271
  injectTo: "body"
@@ -373,7 +382,7 @@ async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOption
373
382
  plugins: [polyfillsPlugin(imports, excludeSystemJS)],
374
383
  build: {
375
384
  write: false,
376
- target: false,
385
+ target: "es5",
377
386
  minify,
378
387
  assetsDir,
379
388
  rollupOptions: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.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.6.0"
26
+ "node": ">=14.18.0"
27
27
  },
28
28
  "repository": {
29
29
  "type": "git",
@@ -36,7 +36,7 @@
36
36
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
37
37
  "dependencies": {
38
38
  "@babel/standalone": "^7.18.5",
39
- "core-js": "^3.22.8",
39
+ "core-js": "^3.23.1",
40
40
  "magic-string": "^0.26.2",
41
41
  "regenerator-runtime": "^0.13.9",
42
42
  "systemjs": "^6.12.1"