Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(terser): add maxWorkers option for terserOptions #13858

Merged
2 changes: 2 additions & 0 deletions docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ npm add -D terser

Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser.

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

- **Type:** `boolean`
Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -143,8 +142,11 @@ export interface BuildOptions {
/**
* Options for terser
* https://terser.org/docs/api-reference#minify-options
*
* 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
terserOptions?: TerserOptions
/**
* Will be merged with internal rollup options.
* https://rollupjs.org/configuration-options/
Expand Down
17 changes: 16 additions & 1 deletion packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +34,8 @@ const loadTerserPath = (root: string) => {
}

export function terserPlugin(config: ResolvedConfig): Plugin {
const { maxWorkers, ...terserOptions } = config.build.terserOptions

const makeWorker = () =>
new Worker(
async (
Expand All @@ -36,6 +48,9 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
const terser = require(terserPath)
return terser.minify(code, options) as Terser.MinifyOutput
},
{
max: maxWorkers,
},
)

let worker: ReturnType<typeof makeWorker>
Expand Down Expand Up @@ -67,7 +82,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',
Expand Down
Loading