Skip to content

Commit

Permalink
feat(terser): add maxWorkers option for terserOptions (#13858)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
sangquangnguyen and bluwy committed Sep 28, 2023
1 parent 17c5928 commit 884fc3d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
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
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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

0 comments on commit 884fc3d

Please sign in to comment.