From a71cd207c37f7ee9a6b2774be11b7be0ccf5db0e Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sat, 15 Jul 2023 16:03:22 +0700 Subject: [PATCH 01/11] Adding new config for defining number of workers --- packages/vite/src/node/build.ts | 5 ++++- packages/vite/src/node/plugins/terser.ts | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 41a360a4fd87ed..e0cbdab3ce5fa8 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -151,8 +151,11 @@ export interface BuildOptions { /** * Options for terser * https://terser.org/docs/api-reference#minify-options + * + * In addition to the terser options, it is also possible to provide the `maxWorkers` option + * Max amount of workers to spawn. Defaults to the number of CPUs minus 1. */ - terserOptions?: Terser.MinifyOptions + terserOptions?: Terser.MinifyOptions & { maxWorkers?: number } /** * Will be merged with internal rollup options. * https://rollupjs.org/configuration-options/ diff --git a/packages/vite/src/node/plugins/terser.ts b/packages/vite/src/node/plugins/terser.ts index 40f28cb9daccaf..92491f1c7f1a7b 100644 --- a/packages/vite/src/node/plugins/terser.ts +++ b/packages/vite/src/node/plugins/terser.ts @@ -36,6 +36,9 @@ export function terserPlugin(config: ResolvedConfig): Plugin { const terser = require(terserPath) return terser.minify(code, options) as Terser.MinifyOutput }, + { + max: config.build.terserOptions?.maxWorkers, + }, ) let worker: ReturnType From 7ba77c2ca987f62c7de212511bbe497f037cbff3 Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sat, 15 Jul 2023 16:32:36 +0700 Subject: [PATCH 02/11] correct the right params for terser --- packages/vite/src/node/plugins/terser.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/terser.ts b/packages/vite/src/node/plugins/terser.ts index 92491f1c7f1a7b..52a745b8fb3191 100644 --- a/packages/vite/src/node/plugins/terser.ts +++ b/packages/vite/src/node/plugins/terser.ts @@ -24,6 +24,7 @@ const loadTerserPath = (root: string) => { } export function terserPlugin(config: ResolvedConfig): Plugin { + const { maxWorkers, ...terserOptions } = config.build.terserOptions const makeWorker = () => new Worker( async ( @@ -37,7 +38,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin { return terser.minify(code, options) as Terser.MinifyOutput }, { - max: config.build.terserOptions?.maxWorkers, + max: maxWorkers, }, ) @@ -70,7 +71,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin { const terserPath = loadTerserPath(config.root) const res = await worker.run(terserPath, code, { safari10: true, - ...config.build.terserOptions, + ...terserOptions, sourceMap: !!outputOptions.sourcemap, module: outputOptions.format.startsWith('es'), toplevel: outputOptions.format === 'cjs', From 723480378d2b0a3df16008df6a50634678f669b2 Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sat, 15 Jul 2023 16:53:24 +0700 Subject: [PATCH 03/11] add docs --- docs/config/build-options.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index 7552215ed5edfa..382ac1d22df504 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -198,10 +198,17 @@ npm add -D terser ## build.terserOptions -- **Type:** `TerserOptions` +- **Type:** `TerserOptions` & `maxWorkers` Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser. +In addition to the terser options, it is also possible to provide the `maxWorkers` option. + +- **Type:** `number` +- **Default:** `undefined` + +Amount of workers to spawn. Defaults to the number of CPUs minus 1. + ## build.write - **Type:** `boolean` From 9a4fe3462707d53687553b0a3fd4f255bdb4d308 Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sat, 15 Jul 2023 16:55:51 +0700 Subject: [PATCH 04/11] mofidy docs --- docs/config/build-options.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index 382ac1d22df504..9593682e9e45f4 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -198,11 +198,11 @@ npm add -D terser ## build.terserOptions -- **Type:** `TerserOptions` & `maxWorkers` +- **Type:** `TerserOptions` & `maxWorkers?: number` Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser. -In addition to the terser options, it is also possible to provide the `maxWorkers` option. +In addition to the terser options, it is also possible to provide the `terserOptions.maxWorkers` option. - **Type:** `number` - **Default:** `undefined` From fddab9e32b423fb1dc9ab6585edcbd0afa0a4f38 Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sat, 15 Jul 2023 16:56:53 +0700 Subject: [PATCH 05/11] adding missing word --- docs/config/build-options.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index 9593682e9e45f4..a5b4ea6dde0679 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -207,7 +207,7 @@ In addition to the terser options, it is also possible to provide the `terserOpt - **Type:** `number` - **Default:** `undefined` -Amount of workers to spawn. Defaults to the number of CPUs minus 1. +Max amount of workers to spawn. Defaults to the number of CPUs minus 1. ## build.write From bb95a6966ff6d83e11db4d6704dc1532dd644a5d Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sat, 15 Jul 2023 18:18:26 +0700 Subject: [PATCH 06/11] format docs --- docs/config/build-options.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index a5b4ea6dde0679..bbda1f898890ec 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -198,7 +198,7 @@ npm add -D terser ## build.terserOptions -- **Type:** `TerserOptions` & `maxWorkers?: number` +- **Type:** `TerserOptions` & `{ maxWorkers?: number }` Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser. From 700f701031296390549855629ae7033eb303c1d8 Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sat, 15 Jul 2023 22:49:58 +0700 Subject: [PATCH 07/11] edit docs --- docs/config/build-options.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index bbda1f898890ec..d1ded079cec3ad 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -203,11 +203,7 @@ npm add -D terser Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser. In addition to the terser options, it is also possible to provide the `terserOptions.maxWorkers` option. - -- **Type:** `number` -- **Default:** `undefined` - -Max amount of workers to spawn. Defaults to the number of CPUs minus 1. +Instructs the plugin to use a specific max amount of workers to spawn. Defaults to the number of CPUs minus 1. ## build.write From 330676982d2c57677b6cf2d0f6b83941d9393d26 Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sun, 16 Jul 2023 11:56:11 +0700 Subject: [PATCH 08/11] adding fallback to terserOptions spread operator, because terserOptions could be undefined --- packages/vite/src/node/plugins/terser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/terser.ts b/packages/vite/src/node/plugins/terser.ts index 52a745b8fb3191..c7d4eb7b616e5a 100644 --- a/packages/vite/src/node/plugins/terser.ts +++ b/packages/vite/src/node/plugins/terser.ts @@ -24,7 +24,7 @@ const loadTerserPath = (root: string) => { } export function terserPlugin(config: ResolvedConfig): Plugin { - const { maxWorkers, ...terserOptions } = config.build.terserOptions + const { maxWorkers, ...terserOptions } = config.build.terserOptions || {} const makeWorker = () => new Worker( async ( From 70e00e9aa4e77a1bc9438cf5fd38a470016cb2c4 Mon Sep 17 00:00:00 2001 From: Sang Quang Nguyen Date: Sun, 16 Jul 2023 14:17:15 +0700 Subject: [PATCH 09/11] modify comments for coding --- packages/vite/src/node/build.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index e0cbdab3ce5fa8..63fb62dc46d3e4 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -152,8 +152,8 @@ export interface BuildOptions { * Options for terser * https://terser.org/docs/api-reference#minify-options * - * In addition to the terser options, it is also possible to provide the `maxWorkers` option - * Max amount of workers to spawn. Defaults to the number of CPUs minus 1. + * In addition to the terser options, it is also possible to provide the `maxWorkers` option. + * Instructs the plugin to use a specific max amount of workers to spawn. Defaults to the number of CPUs minus 1. */ terserOptions?: Terser.MinifyOptions & { maxWorkers?: number } /** From 1a9eea9de645ea609047cec7797703381ff8cb7a Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 22 Sep 2023 15:35:33 +0800 Subject: [PATCH 10/11] refactor: clean up types --- docs/config/build-options.md | 5 ++--- packages/vite/src/node/build.ts | 9 ++++----- packages/vite/src/node/plugins/terser.ts | 13 ++++++++++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index 294f073d262549..0585be653671e6 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -205,12 +205,11 @@ npm add -D terser ## build.terserOptions -- **Type:** `TerserOptions` & `{ maxWorkers?: number }` +- **Type:** `TerserOptions` Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser. -In addition to the terser options, it is also possible to provide the `terserOptions.maxWorkers` option. -Instructs the plugin to use a specific max amount of workers to spawn. Defaults to the number of CPUs minus 1. +In addition, you can also pass a `maxWorkers: number` option to specify the max number of workers to spawn. Defaults to the number of CPUs minus 1. ## build.write diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index e98f631c054dd9..c1f5821e4a7b40 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -18,7 +18,6 @@ import type { RollupWatcher, WatcherOptions, } from 'rollup' -import type { Terser } from 'dep-types/terser' import commonjsPlugin from '@rollup/plugin-commonjs' import type { RollupCommonJSOptions } from 'dep-types/commonjs' import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars' @@ -27,7 +26,7 @@ import type { InlineConfig, ResolvedConfig } from './config' import { isDepsOptimizerEnabled, resolveConfig } from './config' import { buildReporterPlugin } from './plugins/reporter' import { buildEsbuildPlugin } from './plugins/esbuild' -import { terserPlugin } from './plugins/terser' +import { type TerserOptions, terserPlugin } from './plugins/terser' import { asyncFlatten, copyDir, @@ -144,10 +143,10 @@ export interface BuildOptions { * Options for terser * https://terser.org/docs/api-reference#minify-options * - * In addition to the terser options, it is also possible to provide the `maxWorkers` option. - * Instructs the plugin to use a specific max amount of workers to spawn. Defaults to the number of CPUs minus 1. + * In addition, you can also pass a `maxWorkers: number` option to specify the + * max number of workers to spawn. Defaults to the number of CPUs minus 1. */ - terserOptions?: Terser.MinifyOptions & { maxWorkers?: number } + terserOptions?: TerserOptions /** * Will be merged with internal rollup options. * https://rollupjs.org/configuration-options/ diff --git a/packages/vite/src/node/plugins/terser.ts b/packages/vite/src/node/plugins/terser.ts index c7d4eb7b616e5a..c1522065750dbf 100644 --- a/packages/vite/src/node/plugins/terser.ts +++ b/packages/vite/src/node/plugins/terser.ts @@ -4,6 +4,16 @@ import type { Plugin } from '../plugin' import type { ResolvedConfig } from '..' import { requireResolveFromRootWithFallback } from '../utils' +export interface TerserOptions extends Terser.MinifyOptions { + /** + * Vite-specific option to specify the max number of workers to spawn + * when minifying files with terser. + * + * @default number of CPUs minus 1 + */ + maxWorkers?: number +} + let terserPath: string | undefined const loadTerserPath = (root: string) => { if (terserPath) return terserPath @@ -24,7 +34,8 @@ const loadTerserPath = (root: string) => { } export function terserPlugin(config: ResolvedConfig): Plugin { - const { maxWorkers, ...terserOptions } = config.build.terserOptions || {} + const { maxWorkers, ...terserOptions } = config.build.terserOptions + const makeWorker = () => new Worker( async ( From 88e4a65f2316e5c5f3e8778c5355a5757bc45703 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 22 Sep 2023 17:00:29 +0800 Subject: [PATCH 11/11] chore: fix build --- packages/vite/src/node/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index b64e99942a9b1e..3d21243d0ab534 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -82,6 +82,7 @@ export type { ESBuildOptions, ESBuildTransformResult } from './plugins/esbuild' export type { Manifest, ManifestChunk } from './plugins/manifest' export type { ResolveOptions, InternalResolveOptions } from './plugins/resolve' export type { SplitVendorChunkCache } from './plugins/splitVendorChunk' +export type { TerserOptions } from './plugins/terser' export type { WebSocketServer,