@vitejs/plugin-legacy 5.2.0 → 5.3.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
@@ -40,12 +40,23 @@ npm add -D terser
40
40
  - **Type:** `string | string[] | { [key: string]: string }`
41
41
  - **Default:** [`'last 2 versions and not dead, > 0.3%, Firefox ESR'`](https://browsersl.ist/#q=last+2+versions+and+not+dead%2C+%3E+0.3%25%2C+Firefox+ESR)
42
42
 
43
- If explicitly set, it's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets).
43
+ If explicitly set, it's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets) when rendering **legacy chunks**.
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
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
48
 
49
+ ### `modernTargets`
50
+
51
+ - **Type:** `string | string[]`
52
+ - **Default:** [`'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'`](https://browsersl.ist/#q=edge%3E%3D80%2C+firefox%3E%3D72%2C+chrome%3E%3D80%2C+safari%3E%3D13.1%2C+chromeAndroid%3E%3D80%2C+iOS%3E%3D13.1)
53
+
54
+ If explicitly set, it's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets) when rendering **modern chunks**.
55
+
56
+ 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.
57
+
58
+ If it's not set, plugin-legacy will fallback to the default value.
59
+
49
60
  ### `polyfills`
50
61
 
51
62
  - **Type:** `boolean | string[]`
@@ -76,7 +87,7 @@ npm add -D terser
76
87
 
77
88
  Note it is **not recommended** to use the `true` value (which uses auto-detection) because `core-js@3` is very aggressive in polyfill inclusions due to all the bleeding edge features it supports. Even when targeting native ESM support, it injects 15kb of polyfills!
78
89
 
79
- If you don't have hard reliance on bleeding edge runtime features, it is not that hard to avoid having to use polyfills in the modern build altogether. Alternatively, consider using an on-demand service like [Polyfill.io](https://polyfill.io/v3/) to only inject necessary polyfills based on actual browser user-agents (most modern browsers will need nothing!).
90
+ If you don't have hard reliance on bleeding edge runtime features, it is not that hard to avoid having to use polyfills in the modern build altogether. Alternatively, consider using an on-demand service like [Polyfill.io](https://polyfill.io) to only inject necessary polyfills based on actual browser user-agents (most modern browsers will need nothing!).
80
91
 
81
92
  ### `renderLegacyChunks`
82
93
 
package/dist/index.cjs CHANGED
@@ -10,6 +10,7 @@ const vite = require('vite');
10
10
  const MagicString = require('magic-string');
11
11
  const require$$0 = require('tty');
12
12
  const browserslist = require('browserslist');
13
+ const esbuildPluginBrowserslist = require('esbuild-plugin-browserslist');
13
14
 
14
15
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
15
16
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
@@ -154,9 +155,12 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
154
155
  }
155
156
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
156
157
  const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
158
+ const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
159
+ const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/;
157
160
  function viteLegacyPlugin(options = {}) {
158
161
  let config;
159
162
  let targets;
163
+ let modernTargets;
160
164
  const modernTargetsEsbuild = [
161
165
  "es2020",
162
166
  "edge79",
@@ -203,6 +207,7 @@ function viteLegacyPlugin(options = {}) {
203
207
  });
204
208
  }
205
209
  let overriddenBuildTarget = false;
210
+ let overriddenDefaultModernTargets = false;
206
211
  const legacyConfigPlugin = {
207
212
  name: "vite:legacy-config",
208
213
  config(config2, env) {
@@ -215,7 +220,8 @@ function viteLegacyPlugin(options = {}) {
215
220
  }
216
221
  if (genLegacy) {
217
222
  overriddenBuildTarget = config2.build.target !== void 0;
218
- config2.build.target = modernTargetsEsbuild;
223
+ overriddenDefaultModernTargets = options.modernTargets !== void 0;
224
+ config2.build.target = options.modernTargets ? esbuildPluginBrowserslist.resolveToEsbuildTarget(browserslist__default(options.modernTargets)) : modernTargetsEsbuild;
219
225
  }
220
226
  }
221
227
  return {
@@ -232,6 +238,13 @@ function viteLegacyPlugin(options = {}) {
232
238
  )
233
239
  );
234
240
  }
241
+ if (overriddenDefaultModernTargets) {
242
+ config2.logger.warn(
243
+ colors.yellow(
244
+ `plugin-legacy 'modernTargets' option overrode the builtin targets of modern chunks. Some versions of browsers between legacy and modern may not be supported.`
245
+ )
246
+ );
247
+ }
235
248
  }
236
249
  };
237
250
  const legacyGenerateBundlePlugin = {
@@ -308,6 +321,8 @@ function viteLegacyPlugin(options = {}) {
308
321
  }
309
322
  targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
310
323
  isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets);
324
+ modernTargets = options.modernTargets || modernTargetsBabel;
325
+ isDebug && console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
311
326
  const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
312
327
  if (!fileNames) {
313
328
  return path__default.posix.join(config.build.assetsDir, defaultFileName);
@@ -316,8 +331,8 @@ function viteLegacyPlugin(options = {}) {
316
331
  let fileName = typeof fileNames === "function" ? fileNames(chunkInfo) : fileNames;
317
332
  if (fileName.includes("[name]")) {
318
333
  fileName = fileName.replace("[name]", "[name]-legacy");
319
- } else if (fileName.includes("[hash]")) {
320
- fileName = fileName.replace(/[.-]?\[hash\]/, "-legacy$&");
334
+ } else if (nonLeadingHashInFileNameRE.test(fileName)) {
335
+ fileName = fileName.replace(prefixedHashInFileNameRE, "-legacy$&");
321
336
  } else {
322
337
  fileName = fileName.replace(/(.+?)\.(.+)/, "$1-legacy.$2");
323
338
  }
@@ -352,7 +367,7 @@ function viteLegacyPlugin(options = {}) {
352
367
  }
353
368
  if (!isLegacyChunk(chunk, opts)) {
354
369
  if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
355
- await detectPolyfills(raw, modernTargetsBabel, modernPolyfills);
370
+ await detectPolyfills(raw, modernTargets, modernPolyfills);
356
371
  }
357
372
  const ms = new MagicString__default(raw);
358
373
  if (genLegacy && chunk.isEntry) {
package/dist/index.d.cts CHANGED
@@ -7,6 +7,10 @@ interface Options {
7
7
  targets?: string | string[] | {
8
8
  [key: string]: string;
9
9
  };
10
+ /**
11
+ * default: 'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'
12
+ */
13
+ modernTargets?: string | string[];
10
14
  /**
11
15
  * default: true
12
16
  */
package/dist/index.d.mts CHANGED
@@ -7,6 +7,10 @@ interface Options {
7
7
  targets?: string | string[] | {
8
8
  [key: string]: string;
9
9
  };
10
+ /**
11
+ * default: 'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'
12
+ */
13
+ modernTargets?: string | string[];
10
14
  /**
11
15
  * default: true
12
16
  */
package/dist/index.d.ts CHANGED
@@ -7,6 +7,10 @@ interface Options {
7
7
  targets?: string | string[] | {
8
8
  [key: string]: string;
9
9
  };
10
+ /**
11
+ * default: 'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'
12
+ */
13
+ modernTargets?: string | string[];
10
14
  /**
11
15
  * default: true
12
16
  */
package/dist/index.mjs CHANGED
@@ -6,6 +6,7 @@ import { build, normalizePath } from 'vite';
6
6
  import MagicString from 'magic-string';
7
7
  import require$$0 from 'tty';
8
8
  import browserslist from 'browserslist';
9
+ import { resolveToEsbuildTarget } from 'esbuild-plugin-browserslist';
9
10
 
10
11
  function getDefaultExportFromCjs (x) {
11
12
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -142,9 +143,12 @@ function toAssetPathFromHtml(filename, htmlPath, config) {
142
143
  }
143
144
  const legacyEnvVarMarker = `__VITE_IS_LEGACY__`;
144
145
  const _require = createRequire(import.meta.url);
146
+ const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/;
147
+ const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/;
145
148
  function viteLegacyPlugin(options = {}) {
146
149
  let config;
147
150
  let targets;
151
+ let modernTargets;
148
152
  const modernTargetsEsbuild = [
149
153
  "es2020",
150
154
  "edge79",
@@ -191,6 +195,7 @@ function viteLegacyPlugin(options = {}) {
191
195
  });
192
196
  }
193
197
  let overriddenBuildTarget = false;
198
+ let overriddenDefaultModernTargets = false;
194
199
  const legacyConfigPlugin = {
195
200
  name: "vite:legacy-config",
196
201
  config(config2, env) {
@@ -203,7 +208,8 @@ function viteLegacyPlugin(options = {}) {
203
208
  }
204
209
  if (genLegacy) {
205
210
  overriddenBuildTarget = config2.build.target !== void 0;
206
- config2.build.target = modernTargetsEsbuild;
211
+ overriddenDefaultModernTargets = options.modernTargets !== void 0;
212
+ config2.build.target = options.modernTargets ? resolveToEsbuildTarget(browserslist(options.modernTargets)) : modernTargetsEsbuild;
207
213
  }
208
214
  }
209
215
  return {
@@ -220,6 +226,13 @@ function viteLegacyPlugin(options = {}) {
220
226
  )
221
227
  );
222
228
  }
229
+ if (overriddenDefaultModernTargets) {
230
+ config2.logger.warn(
231
+ colors.yellow(
232
+ `plugin-legacy 'modernTargets' option overrode the builtin targets of modern chunks. Some versions of browsers between legacy and modern may not be supported.`
233
+ )
234
+ );
235
+ }
223
236
  }
224
237
  };
225
238
  const legacyGenerateBundlePlugin = {
@@ -296,6 +309,8 @@ function viteLegacyPlugin(options = {}) {
296
309
  }
297
310
  targets = options.targets || browserslistLoadConfig({ path: config.root }) || "last 2 versions and not dead, > 0.3%, Firefox ESR";
298
311
  isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets);
312
+ modernTargets = options.modernTargets || modernTargetsBabel;
313
+ isDebug && console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets);
299
314
  const getLegacyOutputFileName = (fileNames, defaultFileName = "[name]-legacy-[hash].js") => {
300
315
  if (!fileNames) {
301
316
  return path.posix.join(config.build.assetsDir, defaultFileName);
@@ -304,8 +319,8 @@ function viteLegacyPlugin(options = {}) {
304
319
  let fileName = typeof fileNames === "function" ? fileNames(chunkInfo) : fileNames;
305
320
  if (fileName.includes("[name]")) {
306
321
  fileName = fileName.replace("[name]", "[name]-legacy");
307
- } else if (fileName.includes("[hash]")) {
308
- fileName = fileName.replace(/[.-]?\[hash\]/, "-legacy$&");
322
+ } else if (nonLeadingHashInFileNameRE.test(fileName)) {
323
+ fileName = fileName.replace(prefixedHashInFileNameRE, "-legacy$&");
309
324
  } else {
310
325
  fileName = fileName.replace(/(.+?)\.(.+)/, "$1-legacy.$2");
311
326
  }
@@ -340,7 +355,7 @@ function viteLegacyPlugin(options = {}) {
340
355
  }
341
356
  if (!isLegacyChunk(chunk, opts)) {
342
357
  if (options.modernPolyfills && !Array.isArray(options.modernPolyfills) && genModern) {
343
- await detectPolyfills(raw, modernTargetsBabel, modernPolyfills);
358
+ await detectPolyfills(raw, modernTargets, modernPolyfills);
344
359
  }
345
360
  const ms = new MagicString(raw);
346
361
  if (genLegacy && chunk.isEntry) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-legacy",
3
- "version": "5.2.0",
3
+ "version": "5.3.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "files": [
@@ -35,22 +35,23 @@
35
35
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
36
36
  "funding": "https://github.com/vitejs/vite?sponsor=1",
37
37
  "dependencies": {
38
- "@babel/core": "^7.23.3",
39
- "@babel/preset-env": "^7.23.3",
40
- "browserslist": "^4.22.1",
41
- "core-js": "^3.33.2",
38
+ "@babel/core": "^7.23.7",
39
+ "@babel/preset-env": "^7.23.8",
40
+ "browserslist": "^4.22.2",
41
+ "core-js": "^3.35.0",
42
+ "esbuild-plugin-browserslist": "^0.10.0",
42
43
  "magic-string": "^0.30.5",
43
- "regenerator-runtime": "^0.14.0",
44
- "systemjs": "^6.14.2"
44
+ "regenerator-runtime": "^0.14.1",
45
+ "systemjs": "^6.14.3"
45
46
  },
46
47
  "peerDependencies": {
47
48
  "terser": "^5.4.0",
48
49
  "vite": "^5.0.0"
49
50
  },
50
51
  "devDependencies": {
51
- "acorn": "^8.11.2",
52
+ "acorn": "^8.11.3",
52
53
  "picocolors": "^1.0.0",
53
- "vite": "5.0.2"
54
+ "vite": "5.1.0-beta.3"
54
55
  },
55
56
  "scripts": {
56
57
  "dev": "unbuild --stub",