terserPlugin() — vite Function Reference
Architecture documentation for the terserPlugin() function in terser.ts from the vite codebase.
Entity Profile
Dependency Diagram
graph TD 087bc308_b1da_3dca_40fd_0cf762502823["terserPlugin()"] be81372e_415b_426f_d3c1_132b2d458ce6["terser.ts"] 087bc308_b1da_3dca_40fd_0cf762502823 -->|defined in| be81372e_415b_426f_d3c1_132b2d458ce6 8c4db194_5dfd_4391_cc9a_833655009196["resolveBuildPlugins()"] 8c4db194_5dfd_4391_cc9a_833655009196 -->|calls| 087bc308_b1da_3dca_40fd_0cf762502823 39ac1178_9b7e_dd67_f753_f2fb1b78e04a["loadTerserPath()"] 087bc308_b1da_3dca_40fd_0cf762502823 -->|calls| 39ac1178_9b7e_dd67_f753_f2fb1b78e04a 310ed049_c1b4_c917_b399_81bab290e5a2["generateCodeFrame()"] 087bc308_b1da_3dca_40fd_0cf762502823 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2 style 087bc308_b1da_3dca_40fd_0cf762502823 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/vite/src/node/plugins/terser.ts lines 38–139
export function terserPlugin(config: ResolvedConfig): Plugin {
const { maxWorkers, ...terserOptions } = config.build.terserOptions
const makeWorker = () =>
new WorkerWithFallback(
() =>
async (
terserPath: string,
code: string,
options: TerserMinifyOptions,
) => {
const terser: typeof import('terser') = await import(terserPath)
try {
return (await terser.minify(code, options)) as TerserMinifyOutput
} catch (e) {
// convert to a plain object as additional properties of Error instances are not
// sent back to the main thread
throw { stack: e.stack /* stack is non-enumerable */, ...e }
}
},
{
shouldUseFake(_terserPath, _code, options) {
return !!(
(typeof options.mangle === 'object' &&
(options.mangle.nth_identifier?.get ||
(typeof options.mangle.properties === 'object' &&
options.mangle.properties.nth_identifier?.get))) ||
typeof options.format?.comments === 'function' ||
typeof options.output?.comments === 'function' ||
options.nameCache
)
},
max: maxWorkers,
},
)
let worker: ReturnType<typeof makeWorker>
return {
name: 'vite:terser',
applyToEnvironment(environment) {
// We also need the plugin even if minify isn't 'terser' as we force
// terser in plugin-legacy
return !!environment.config.build.minify
},
async renderChunk(code, chunk, outputOptions) {
// This plugin is included for any non-false value of config.build.minify,
// so that normal chunks can use the preferred minifier, and legacy chunks
// can use terser.
if (
config.build.minify !== 'terser' &&
!this.environment.config.isOutputOptionsForLegacyChunks?.(outputOptions)
) {
return null
}
// Lazy load worker.
worker ||= makeWorker()
const terserPath = pathToFileURL(loadTerserPath(config.root)).href
try {
const res = await worker.run(terserPath, code, {
safari10: true,
...terserOptions,
format: {
...terserOptions.format,
// For ES lib mode, preserve comments to keep pure annotations for tree-shaking
preserve_annotations:
config.build.lib && outputOptions.format === 'es'
? true
: terserOptions.format?.preserve_annotations,
},
sourceMap: !!outputOptions.sourcemap,
module: outputOptions.format.startsWith('es'),
toplevel: outputOptions.format === 'cjs',
})
return {
code: res.code!,
map: res.map as any,
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does terserPlugin() do?
terserPlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/terser.ts.
Where is terserPlugin() defined?
terserPlugin() is defined in packages/vite/src/node/plugins/terser.ts at line 38.
What does terserPlugin() call?
terserPlugin() calls 2 function(s): generateCodeFrame, loadTerserPath.
What calls terserPlugin()?
terserPlugin() is called by 1 function(s): resolveBuildPlugins.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free