@vitejs/plugin-legacy 5.0.0-beta.0 → 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,18 +65,6 @@ 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 `true` to ignore these sources.
77
-
78
68
  ### `modernPolyfills`
79
69
 
80
70
  - **Type:** `boolean | string[]`
package/dist/index.cjs CHANGED
@@ -157,6 +157,14 @@ const _require = node_module.createRequire((typeof document === 'undefined' ? re
157
157
  function viteLegacyPlugin(options = {}) {
158
158
  let config;
159
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";
160
168
  const genLegacy = options.renderLegacyChunks !== false;
161
169
  const genModern = options.renderModernChunks !== false;
162
170
  if (!genLegacy && !genModern) {
@@ -207,13 +215,7 @@ function viteLegacyPlugin(options = {}) {
207
215
  }
208
216
  if (genLegacy) {
209
217
  overriddenBuildTarget = config2.build.target !== void 0;
210
- config2.build.target = [
211
- "es2020",
212
- "edge79",
213
- "firefox67",
214
- "chrome64",
215
- "safari12"
216
- ];
218
+ config2.build.target = modernTargetsEsbuild;
217
219
  }
218
220
  }
219
221
  return {
@@ -247,7 +249,7 @@ function viteLegacyPlugin(options = {}) {
247
249
  `[@vitejs/plugin-legacy] modern polyfills:`,
248
250
  modernPolyfills
249
251
  );
250
- await buildPolyfillChunk(
252
+ const polyfillChunk = await buildPolyfillChunk(
251
253
  config.mode,
252
254
  modernPolyfills,
253
255
  bundle,
@@ -257,6 +259,9 @@ function viteLegacyPlugin(options = {}) {
257
259
  opts,
258
260
  true
259
261
  );
262
+ if (genLegacy && polyfillChunk) {
263
+ polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code;
264
+ }
260
265
  return;
261
266
  }
262
267
  if (!genLegacy) {
@@ -343,7 +348,7 @@ function viteLegacyPlugin(options = {}) {
343
348
  }
344
349
  if (!isLegacyChunk(chunk, opts)) {
345
350
  if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
346
- await detectPolyfills(raw, { esmodules: true }, modernPolyfills);
351
+ await detectPolyfills(raw, modernTargetsBabel, modernPolyfills);
347
352
  }
348
353
  const ms = new MagicString__default(raw);
349
354
  if (genLegacy && chunk.isEntry) {
@@ -400,10 +405,7 @@ function viteLegacyPlugin(options = {}) {
400
405
  ],
401
406
  [
402
407
  (await import('@babel/preset-env')).default,
403
- createBabelPresetEnvOptions(targets, {
404
- needPolyfills,
405
- ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
406
- })
408
+ createBabelPresetEnvOptions(targets, { needPolyfills })
407
409
  ]
408
410
  ]
409
411
  });
@@ -551,12 +553,11 @@ async function detectPolyfills(code, targets, list) {
551
553
  ast: true,
552
554
  babelrc: false,
553
555
  configFile: false,
556
+ compact: false,
554
557
  presets: [
555
558
  [
556
559
  (await import('@babel/preset-env')).default,
557
- createBabelPresetEnvOptions(targets, {
558
- ignoreBrowserslistConfig: true
559
- })
560
+ createBabelPresetEnvOptions(targets, {})
560
561
  ]
561
562
  ]
562
563
  });
@@ -569,10 +570,7 @@ async function detectPolyfills(code, targets, list) {
569
570
  }
570
571
  }
571
572
  }
572
- function createBabelPresetEnvOptions(targets, {
573
- needPolyfills = true,
574
- ignoreBrowserslistConfig
575
- }) {
573
+ function createBabelPresetEnvOptions(targets, { needPolyfills = true }) {
576
574
  return {
577
575
  targets,
578
576
  bugfixes: true,
@@ -584,7 +582,7 @@ function createBabelPresetEnvOptions(targets, {
584
582
  proposals: false
585
583
  } : void 0,
586
584
  shippedProposals: true,
587
- ignoreBrowserslistConfig
585
+ ignoreBrowserslistConfig: true
588
586
  };
589
587
  }
590
588
  async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, buildOptions, format, rollupOutputOptions, excludeSystemJS) {
@@ -635,6 +633,7 @@ async function buildPolyfillChunk(mode, imports, bundle, facadeToChunkMap, build
635
633
  }
636
634
  }
637
635
  bundle[polyfillChunk.fileName] = polyfillChunk;
636
+ return polyfillChunk;
638
637
  }
639
638
  const polyfillId = "\0vite/legacy-polyfills";
640
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) {
@@ -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": "5.0.0-beta.0",
3
+ "version": "5.0.0-beta.1",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -17,7 +17,6 @@
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
  }
@@ -38,7 +37,7 @@
38
37
  "dependencies": {
39
38
  "@babel/core": "^7.22.20",
40
39
  "@babel/preset-env": "^7.22.20",
41
- "browserslist": "^4.21.10",
40
+ "browserslist": "^4.21.11",
42
41
  "core-js": "^3.32.2",
43
42
  "magic-string": "^0.30.3",
44
43
  "regenerator-runtime": "^0.14.0",
@@ -51,7 +50,7 @@
51
50
  "devDependencies": {
52
51
  "acorn": "^8.10.0",
53
52
  "picocolors": "^1.0.0",
54
- "vite": "5.0.0-beta.2"
53
+ "vite": "5.0.0-beta.3"
55
54
  },
56
55
  "scripts": {
57
56
  "dev": "unbuild --stub",