@vitejs/plugin-legacy 2.0.0-alpha.2 → 2.0.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 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
@@ -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,41 @@ async function loadBabel() {
21
21
  }
22
22
  return babel;
23
23
  }
24
+ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRelative) {
25
+ const { renderBuiltUrl } = config.experimental;
26
+ let relative = config.base === "" || config.base === "./";
27
+ if (renderBuiltUrl) {
28
+ const result = renderBuiltUrl(filename, {
29
+ hostId,
30
+ hostType,
31
+ type,
32
+ ssr: !!config.build.ssr
33
+ });
34
+ if (typeof result === "object") {
35
+ if (result.runtime) {
36
+ throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
37
+ }
38
+ if (typeof result.relative === "boolean") {
39
+ relative = result.relative;
40
+ }
41
+ } else if (result) {
42
+ return result;
43
+ }
44
+ }
45
+ if (relative && !config.build.ssr) {
46
+ return toRelative(filename, hostId);
47
+ } else {
48
+ return config.base + filename;
49
+ }
50
+ }
51
+ function getBaseInHTML(urlRelativePath, config) {
52
+ return config.base === "./" || config.base === "" ? path__default.posix.join(path__default.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
53
+ }
54
+ function toAssetPathFromHtml(filename, htmlPath, config) {
55
+ const relativeUrlPath = vite.normalizePath(path__default.relative(config.root, htmlPath));
56
+ const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
57
+ return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
58
+ }
24
59
  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
60
  const legacyPolyfillId = "vite-legacy-polyfill";
26
61
  const legacyEntryId = "vite-legacy-entry";
@@ -30,7 +65,7 @@ const detectModernBrowserCode = `try{import.meta.url;import("_").catch(()=>1);}c
30
65
  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
66
  const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('data:text/javascript,')};`;
32
67
  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)));
68
+ 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
69
  function viteLegacyPlugin(options = {}) {
35
70
  let config;
36
71
  const targets = options.targets || "defaults";
@@ -226,7 +261,7 @@ function viteLegacyPlugin(options = {}) {
226
261
  attrs: {
227
262
  type: "module",
228
263
  crossorigin: true,
229
- src: `${config.base}${modernPolyfillFilename}`
264
+ src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
230
265
  }
231
266
  });
232
267
  } else if (modernPolyfills.size) {
@@ -249,7 +284,7 @@ function viteLegacyPlugin(options = {}) {
249
284
  nomodule: true,
250
285
  crossorigin: true,
251
286
  id: legacyPolyfillId,
252
- src: `${config.base}${legacyPolyfillFilename}`
287
+ src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
253
288
  },
254
289
  injectTo: "body"
255
290
  });
@@ -258,14 +293,13 @@ function viteLegacyPlugin(options = {}) {
258
293
  }
259
294
  const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
260
295
  if (legacyEntryFilename) {
261
- const nonBareBase = config.base === "" ? "./" : config.base;
262
296
  tags.push({
263
297
  tag: "script",
264
298
  attrs: {
265
299
  nomodule: true,
266
300
  crossorigin: true,
267
301
  id: legacyEntryId,
268
- "data-src": nonBareBase + legacyEntryFilename
302
+ "data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
269
303
  },
270
304
  children: systemJSInlineCode,
271
305
  injectTo: "body"
@@ -376,13 +410,13 @@ async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOption
376
410
  let { minify, assetsDir } = buildOptions;
377
411
  minify = minify ? "terser" : false;
378
412
  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)))),
413
+ 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
414
  configFile: false,
381
415
  logLevel: "error",
382
416
  plugins: [polyfillsPlugin(imports, excludeSystemJS)],
383
417
  build: {
384
418
  write: false,
385
- target: false,
419
+ target: "es5",
386
420
  minify,
387
421
  assetsDir,
388
422
  rollupOptions: {
@@ -475,10 +509,10 @@ function wrapIIFEBabelPlugin() {
475
509
  };
476
510
  }
477
511
  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")
512
+ node_crypto.createHash("sha256").update(safari10NoModuleFix).digest("base64"),
513
+ node_crypto.createHash("sha256").update(systemJSInlineCode).digest("base64"),
514
+ node_crypto.createHash("sha256").update(detectModernBrowserCode).digest("base64"),
515
+ node_crypto.createHash("sha256").update(dynamicFallbackInlineCode).digest("base64")
482
516
  ];
483
517
 
484
518
  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,41 @@ async function loadBabel() {
12
12
  }
13
13
  return babel;
14
14
  }
15
+ function toOutputFilePathInHtml(filename, type, hostId, hostType, config, toRelative) {
16
+ const { renderBuiltUrl } = config.experimental;
17
+ let relative = config.base === "" || config.base === "./";
18
+ if (renderBuiltUrl) {
19
+ const result = renderBuiltUrl(filename, {
20
+ hostId,
21
+ hostType,
22
+ type,
23
+ ssr: !!config.build.ssr
24
+ });
25
+ if (typeof result === "object") {
26
+ if (result.runtime) {
27
+ throw new Error(`{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`);
28
+ }
29
+ if (typeof result.relative === "boolean") {
30
+ relative = result.relative;
31
+ }
32
+ } else if (result) {
33
+ return result;
34
+ }
35
+ }
36
+ if (relative && !config.build.ssr) {
37
+ return toRelative(filename, hostId);
38
+ } else {
39
+ return config.base + filename;
40
+ }
41
+ }
42
+ function getBaseInHTML(urlRelativePath, config) {
43
+ return config.base === "./" || config.base === "" ? path.posix.join(path.posix.relative(urlRelativePath, "").slice(0, -2), "./") : config.base;
44
+ }
45
+ function toAssetPathFromHtml(filename, htmlPath, config) {
46
+ const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath));
47
+ const toRelative = (filename2, hostId) => getBaseInHTML(relativeUrlPath, config) + filename2;
48
+ return toOutputFilePathInHtml(filename, "asset", htmlPath, "html", config, toRelative);
49
+ }
15
50
  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
51
  const legacyPolyfillId = "vite-legacy-polyfill";
17
52
  const legacyEntryId = "vite-legacy-entry";
@@ -217,7 +252,7 @@ function viteLegacyPlugin(options = {}) {
217
252
  attrs: {
218
253
  type: "module",
219
254
  crossorigin: true,
220
- src: `${config.base}${modernPolyfillFilename}`
255
+ src: toAssetPathFromHtml(modernPolyfillFilename, chunk.facadeModuleId, config)
221
256
  }
222
257
  });
223
258
  } else if (modernPolyfills.size) {
@@ -240,7 +275,7 @@ function viteLegacyPlugin(options = {}) {
240
275
  nomodule: true,
241
276
  crossorigin: true,
242
277
  id: legacyPolyfillId,
243
- src: `${config.base}${legacyPolyfillFilename}`
278
+ src: toAssetPathFromHtml(legacyPolyfillFilename, chunk.facadeModuleId, config)
244
279
  },
245
280
  injectTo: "body"
246
281
  });
@@ -249,14 +284,13 @@ function viteLegacyPlugin(options = {}) {
249
284
  }
250
285
  const legacyEntryFilename = facadeToLegacyChunkMap.get(chunk.facadeModuleId);
251
286
  if (legacyEntryFilename) {
252
- const nonBareBase = config.base === "" ? "./" : config.base;
253
287
  tags.push({
254
288
  tag: "script",
255
289
  attrs: {
256
290
  nomodule: true,
257
291
  crossorigin: true,
258
292
  id: legacyEntryId,
259
- "data-src": nonBareBase + legacyEntryFilename
293
+ "data-src": toAssetPathFromHtml(legacyEntryFilename, chunk.facadeModuleId, config)
260
294
  },
261
295
  children: systemJSInlineCode,
262
296
  injectTo: "body"
@@ -373,7 +407,7 @@ async function buildPolyfillChunk(imports, bundle, facadeToChunkMap, buildOption
373
407
  plugins: [polyfillsPlugin(imports, excludeSystemJS)],
374
408
  build: {
375
409
  write: false,
376
- target: false,
410
+ target: "es5",
377
411
  minify,
378
412
  assetsDir,
379
413
  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",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -19,11 +19,11 @@
19
19
  "scripts": {
20
20
  "dev": "unbuild --stub",
21
21
  "build": "unbuild && pnpm run patch-cjs",
22
- "patch-cjs": "esno ../../scripts/patchCJS.ts",
22
+ "patch-cjs": "tsx ../../scripts/patchCJS.ts",
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",
@@ -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.5",
39
- "core-js": "^3.22.8",
38
+ "@babel/standalone": "^7.18.8",
39
+ "core-js": "^3.23.4",
40
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
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.5",
49
+ "@babel/core": "^7.18.6",
50
50
  "vite": "workspace:*"
51
51
  }
52
52
  }