@vitejs/plugin-legacy 4.1.1 → 5.0.0-beta.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
@@ -44,6 +44,8 @@ npm add -D terser
44
44
 
45
45
  The query is also [Browserslist compatible](https://github.com/browserslist/browserslist). See [Browserslist Best Practices](https://github.com/browserslist/browserslist#best-practices) for more details.
46
46
 
47
+ If it's not set, plugin-legacy will load [the browserslist config sources](https://github.com/browserslist/browserslist#queries) and then fallback to the default value.
48
+
47
49
  ### `polyfills`
48
50
 
49
51
  - **Type:** `boolean | string[]`
@@ -63,24 +65,12 @@ npm add -D terser
63
65
 
64
66
  Note: if additional polyfills are needed for both the modern and legacy chunks, they can simply be imported in the application source code.
65
67
 
66
- ### `ignoreBrowserslistConfig`
67
-
68
- - **Type:** `boolean`
69
- - **Default:** `false`
70
-
71
- `@babel/preset-env` automatically detects [`browserslist` config sources](https://github.com/browserslist/browserslist#browserslist-):
72
-
73
- - `browserslist` field in `package.json`
74
- - `.browserslistrc` file in cwd.
75
-
76
- Set to `false` to ignore these sources.
77
-
78
68
  ### `modernPolyfills`
79
69
 
80
70
  - **Type:** `boolean | string[]`
81
71
  - **Default:** `false`
82
72
 
83
- Defaults to `false`. Enabling this option will generate a separate polyfills chunk for the modern build (targeting browsers with [native ESM support](https://caniuse.com/es6-module)).
73
+ Defaults to `false`. Enabling this option will generate a separate polyfills chunk for the modern build (targeting [browsers that support widely-available features](#browsers-that-supports-esm-but-does-not-support-widely-available-features)).
84
74
 
85
75
  Set to a list of strings to explicitly control which polyfills to include. See [Polyfill Specifiers](#polyfill-specifiers) for details.
86
76
 
package/dist/index.cjs CHANGED
@@ -11,6 +11,7 @@ const MagicString = require('magic-string');
11
11
  const require$$0 = require('tty');
12
12
  const browserslist = require('browserslist');
13
13
 
14
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
14
15
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
15
16
 
16
17
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
@@ -152,10 +153,18 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
152
153
  );
153
154
  }
154
155
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
155
- const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
156
+ const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
156
157
  function viteLegacyPlugin(options = {}) {
157
158
  let config;
158
159
  let targets;
160
+ const modernTargetsEsbuild = [
161
+ "es2020",
162
+ "edge79",
163
+ "firefox67",
164
+ "chrome64",
165
+ "safari12"
166
+ ];
167
+ const modernTargetsBabel = "edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1";
159
168
  const genLegacy = options.renderLegacyChunks !== false;
160
169
  const genModern = options.renderModernChunks !== false;
161
170
  if (!genLegacy && !genModern) {
@@ -206,13 +215,7 @@ function viteLegacyPlugin(options = {}) {
206
215
  }
207
216
  if (genLegacy) {
208
217
  overriddenBuildTarget = config2.build.target !== void 0;
209
- config2.build.target = [
210
- "es2020",
211
- "edge79",
212
- "firefox67",
213
- "chrome64",
214
- "safari12"
215
- ];
218
+ config2.build.target = modernTargetsEsbuild;
216
219
  }
217
220
  }
218
221
  return {
@@ -246,7 +249,7 @@ function viteLegacyPlugin(options = {}) {
246
249
  `[@vitejs/plugin-legacy] modern polyfills:`,
247
250
  modernPolyfills
248
251
  );
249
- await buildPolyfillChunk(
252
+ const polyfillChunk = await buildPolyfillChunk(
250
253
  config.mode,
251
254
  modernPolyfills,
252
255
  bundle,
@@ -256,6 +259,9 @@ function viteLegacyPlugin(options = {}) {
256
259
  opts,
257
260
  true
258
261
  );
262
+ if (genLegacy && polyfillChunk) {
263
+ polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code;
264
+ }
259
265
  return;
260
266
  }
261
267
  if (!genLegacy) {
@@ -342,7 +348,7 @@ function viteLegacyPlugin(options = {}) {
342
348
  }
343
349
  if (!isLegacyChunk(chunk, opts)) {
344
350
  if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
345
- await detectPolyfills(raw, { esmodules: true }, modernPolyfills);
351
+ await detectPolyfills(raw, modernTargetsBabel, modernPolyfills);
346
352
  }
347
353
  const ms = new MagicString__default(raw);
348
354
  if (genLegacy && chunk.isEntry) {
@@ -362,7 +368,7 @@ function viteLegacyPlugin(options = {}) {
362
368
  if (config.build.sourcemap) {
363
369
  return {
364
370
  code: ms.toString(),
365
- map: ms.generateMap({ hires: true })
371
+ map: ms.generateMap({ hires: "boundary" })
366
372
  };
367
373
  }
368
374
  return {
@@ -399,10 +405,7 @@ function viteLegacyPlugin(options = {}) {
399
405
  ],
400
406
  [
401
407
  (await import('@babel/preset-env')).default,
402
- createBabelPresetEnvOptions(targets, {
403
- needPolyfills,
404
- ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
405
- })
408
+ createBabelPresetEnvOptions(targets, { needPolyfills })
406
409
  ]
407
410
  ]
408
411
  });
@@ -550,12 +553,11 @@ async function detectPolyfills(code, targets, list) {
550
553
  ast: true,
551
554
  babelrc: false,
552
555
  configFile: false,
556
+ compact: false,
553
557
  presets: [
554
558
  [
555
559
  (await import('@babel/preset-env')).default,
556
- createBabelPresetEnvOptions(targets, {
557
- ignoreBrowserslistConfig: true
558
- })
560
+ createBabelPresetEnvOptions(targets, {})
559
561
  ]
560
562
  ]
561
563
  });
@@ -568,10 +570,7 @@ async function detectPolyfills(code, targets, list) {
568
570
  }
569
571
  }
570
572
  }
571
- function createBabelPresetEnvOptions(targets, {
572
- needPolyfills = true,
573
- ignoreBrowserslistConfig
574
- }) {
573
+ function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
575
574
  return {
576
575
  targets,
577
576
  bugfixes: true,
@@ -583,7 +582,7 @@ function createBabelPresetEnvOptions(targets, {
583
582
  proposals: false
584
583
  } : void 0,
585
584
  shippedProposals: true,
586
- ignoreBrowserslistConfig
585
+ ignoreBrowserslistConfig: true
587
586
  };
588
587
  }
589
588
  async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
@@ -592,7 +591,7 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
592
591
  const res = await vite.build({
593
592
  mode,
594
593
  // so that everything is resolved from here
595
- root: path__default.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)))),
594
+ root: path__default.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)))),
596
595
  configFile: false,
597
596
  logLevel: "error",
598
597
  plugins: [polyfillsPlugin(imports, excludeSystemJS)],
@@ -634,6 +633,7 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
634
633
  }
635
634
  }
636
635
  bundle[polyfillChunk.fileName] = polyfillChunk;
636
+ return polyfillChunk;
637
637
  }
638
638
  const polyfillId = "\0vite/legacy-polyfills";
639
639
  function polyfillsPlugin(imports, excludeSystemJS) {
@@ -0,0 +1,37 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface Options {
4
+ /**
5
+ * default: 'defaults'
6
+ */
7
+ targets?: string | string[] | {
8
+ [key: string]: string;
9
+ };
10
+ /**
11
+ * default: true
12
+ */
13
+ polyfills?: boolean | string[];
14
+ additionalLegacyPolyfills?: string[];
15
+ /**
16
+ * default: false
17
+ */
18
+ modernPolyfills?: boolean | string[];
19
+ /**
20
+ * default: true
21
+ */
22
+ renderLegacyChunks?: boolean;
23
+ /**
24
+ * default: false
25
+ */
26
+ externalSystemJS?: boolean;
27
+ /**
28
+ * default: true
29
+ */
30
+ renderModernChunks?: boolean;
31
+ }
32
+
33
+ declare function viteLegacyPlugin(options?: Options): Plugin[];
34
+ declare function detectPolyfills(code: string, targets: any, list: Set<string>): Promise<void>;
35
+ declare const cspHashes: string[];
36
+
37
+ export { cspHashes, viteLegacyPlugin as default, detectPolyfills };
@@ -0,0 +1,37 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface Options {
4
+ /**
5
+ * default: 'defaults'
6
+ */
7
+ targets?: string | string[] | {
8
+ [key: string]: string;
9
+ };
10
+ /**
11
+ * default: true
12
+ */
13
+ polyfills?: boolean | string[];
14
+ additionalLegacyPolyfills?: string[];
15
+ /**
16
+ * default: false
17
+ */
18
+ modernPolyfills?: boolean | string[];
19
+ /**
20
+ * default: true
21
+ */
22
+ renderLegacyChunks?: boolean;
23
+ /**
24
+ * default: false
25
+ */
26
+ externalSystemJS?: boolean;
27
+ /**
28
+ * default: true
29
+ */
30
+ renderModernChunks?: boolean;
31
+ }
32
+
33
+ declare function viteLegacyPlugin(options?: Options): Plugin[];
34
+ declare function detectPolyfills(code: string, targets: any, list: Set<string>): Promise<void>;
35
+ declare const cspHashes: string[];
36
+
37
+ export { cspHashes, viteLegacyPlugin as default, detectPolyfills };
package/dist/index.d.ts CHANGED
@@ -7,10 +7,6 @@ interface Options {
7
7
  targets?: string | string[] | {
8
8
  [key: string]: string;
9
9
  };
10
- /**
11
- * default: false
12
- */
13
- ignoreBrowserslistConfig?: boolean;
14
10
  /**
15
11
  * default: true
16
12
  */
package/dist/index.mjs CHANGED
@@ -145,6 +145,14 @@ const _require = createRequire(import.meta.url);
145
145
  function viteLegacyPlugin(options = {}) {
146
146
  let config;
147
147
  let targets;
148
+ const modernTargetsEsbuild = [
149
+ "es2020",
150
+ "edge79",
151
+ "firefox67",
152
+ "chrome64",
153
+ "safari12"
154
+ ];
155
+ const modernTargetsBabel = "edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1";
148
156
  const genLegacy = options.renderLegacyChunks !== false;
149
157
  const genModern = options.renderModernChunks !== false;
150
158
  if (!genLegacy && !genModern) {
@@ -195,13 +203,7 @@ function viteLegacyPlugin(options = {}) {
195
203
  }
196
204
  if (genLegacy) {
197
205
  overriddenBuildTarget = config2.build.target !== void 0;
198
- config2.build.target = [
199
- "es2020",
200
- "edge79",
201
- "firefox67",
202
- "chrome64",
203
- "safari12"
204
- ];
206
+ config2.build.target = modernTargetsEsbuild;
205
207
  }
206
208
  }
207
209
  return {
@@ -235,7 +237,7 @@ function viteLegacyPlugin(options = {}) {
235
237
  `[@vitejs/plugin-legacy] modern polyfills:`,
236
238
  modernPolyfills
237
239
  );
238
- await buildPolyfillChunk(
240
+ const polyfillChunk = await buildPolyfillChunk(
239
241
  config.mode,
240
242
  modernPolyfills,
241
243
  bundle,
@@ -245,6 +247,9 @@ function viteLegacyPlugin(options = {}) {
245
247
  opts,
246
248
  true
247
249
  );
250
+ if (genLegacy && polyfillChunk) {
251
+ polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code;
252
+ }
248
253
  return;
249
254
  }
250
255
  if (!genLegacy) {
@@ -331,7 +336,7 @@ function viteLegacyPlugin(options = {}) {
331
336
  }
332
337
  if (!isLegacyChunk(chunk, opts)) {
333
338
  if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
334
- await detectPolyfills(raw, { esmodules: true }, modernPolyfills);
339
+ await detectPolyfills(raw, modernTargetsBabel, modernPolyfills);
335
340
  }
336
341
  const ms = new MagicString(raw);
337
342
  if (genLegacy && chunk.isEntry) {
@@ -351,7 +356,7 @@ function viteLegacyPlugin(options = {}) {
351
356
  if (config.build.sourcemap) {
352
357
  return {
353
358
  code: ms.toString(),
354
- map: ms.generateMap({ hires: true })
359
+ map: ms.generateMap({ hires: "boundary" })
355
360
  };
356
361
  }
357
362
  return {
@@ -388,10 +393,7 @@ function viteLegacyPlugin(options = {}) {
388
393
  ],
389
394
  [
390
395
  (await import('@babel/preset-env')).default,
391
- createBabelPresetEnvOptions(targets, {
392
- needPolyfills,
393
- ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
394
- })
396
+ createBabelPresetEnvOptions(targets, { needPolyfills })
395
397
  ]
396
398
  ]
397
399
  });
@@ -539,12 +541,11 @@ async function detectPolyfills(code, targets, list) {
539
541
  ast: true,
540
542
  babelrc: false,
541
543
  configFile: false,
544
+ compact: false,
542
545
  presets: [
543
546
  [
544
547
  (await import('@babel/preset-env')).default,
545
- createBabelPresetEnvOptions(targets, {
546
- ignoreBrowserslistConfig: true
547
- })
548
+ createBabelPresetEnvOptions(targets, {})
548
549
  ]
549
550
  ]
550
551
  });
@@ -557,10 +558,7 @@ async function detectPolyfills(code, targets, list) {
557
558
  }
558
559
  }
559
560
  }
560
- function createBabelPresetEnvOptions(targets, {
561
- needPolyfills = true,
562
- ignoreBrowserslistConfig
563
- }) {
561
+ function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
564
562
  return {
565
563
  targets,
566
564
  bugfixes: true,
@@ -572,7 +570,7 @@ function createBabelPresetEnvOptions(targets, {
572
570
  proposals: false
573
571
  } : void 0,
574
572
  shippedProposals: true,
575
- ignoreBrowserslistConfig
573
+ ignoreBrowserslistConfig: true
576
574
  };
577
575
  }
578
576
  async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
@@ -623,6 +621,7 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
623
621
  }
624
622
  }
625
623
  bundle[polyfillChunk.fileName] = polyfillChunk;
624
+ return polyfillChunk;
626
625
  }
627
626
  const polyfillId = "\0vite/legacy-polyfills";
628
627
  function polyfillsPlugin(imports, excludeSystemJS) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "4.1.1",
3
+ "version": "5.0.0-beta.1",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -17,19 +17,12 @@
17
17
  "types": "./dist/index.d.ts",
18
18
  "exports": {
19
19
  ".": {
20
- "types": "./dist/index.d.ts",
21
20
  "import": "./dist/index.mjs",
22
21
  "require": "./dist/index.cjs"
23
22
  }
24
23
  },
25
- "scripts": {
26
- "dev": "unbuild --stub",
27
- "build": "unbuild && pnpm run patch-cjs",
28
- "patch-cjs": "tsx ../../scripts/patchCJS.ts",
29
- "prepublishOnly": "npm run build"
30
- },
31
24
  "engines": {
32
- "node": "^14.18.0 || >=16.0.0"
25
+ "node": "^18.0.0 || >=20.0.0"
33
26
  },
34
27
  "repository": {
35
28
  "type": "git",
@@ -42,13 +35,13 @@
42
35
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
43
36
  "funding": "https://github.com/vitejs/vite?sponsor=1",
44
37
  "dependencies": {
45
- "@babel/core": "^7.22.9",
46
- "@babel/preset-env": "^7.22.9",
47
- "browserslist": "^4.21.9",
48
- "core-js": "^3.31.1",
49
- "magic-string": "^0.30.1",
50
- "regenerator-runtime": "^0.13.11",
51
- "systemjs": "^6.14.1"
38
+ "@babel/core": "^7.22.20",
39
+ "@babel/preset-env": "^7.22.20",
40
+ "browserslist": "^4.21.11",
41
+ "core-js": "^3.32.2",
42
+ "magic-string": "^0.30.3",
43
+ "regenerator-runtime": "^0.14.0",
44
+ "systemjs": "^6.14.2"
52
45
  },
53
46
  "peerDependencies": {
54
47
  "terser": "^5.4.0",
@@ -57,6 +50,11 @@
57
50
  "devDependencies": {
58
51
  "acorn": "^8.10.0",
59
52
  "picocolors": "^1.0.0",
60
- "vite": "workspace:*"
53
+ "vite": "5.0.0-beta.3"
54
+ },
55
+ "scripts": {
56
+ "dev": "unbuild --stub",
57
+ "build": "unbuild && pnpm run patch-cjs",
58
+ "patch-cjs": "tsx ../../scripts/patchCJS.ts"
61
59
  }
62
- }
60
+ }