Home / File/ terser.ts — vite Source File

terser.ts — vite Source File

Architecture documentation for terser.ts, a typescript file in the vite codebase. 10 imports, 2 dependents.

File typescript PluginSystem AssetManagement 10 imports 2 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  be81372e_415b_426f_d3c1_132b2d458ce6["terser.ts"]
  5abb8c87_ffcb_f2d4_7421_e36705d9e5c7["plugin.ts"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 5abb8c87_ffcb_f2d4_7421_e36705d9e5c7
  1dc2cf7d_5d97_c778_8c33_6449a7607aef["Plugin"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 1dc2cf7d_5d97_c778_8c33_6449a7607aef
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 031bc221_67a8_c579_f2bf_bb30a08beeb2
  310ed049_c1b4_c917_b399_81bab290e5a2["generateCodeFrame"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 310ed049_c1b4_c917_b399_81bab290e5a2
  46419eb2_654c_202c_b705_789bb65b53e8["nodeResolve.ts"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 46419eb2_654c_202c_b705_789bb65b53e8
  363641b4_de9b_dbe1_f421_1fd46e488386["nodeResolveWithVite"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 363641b4_de9b_dbe1_f421_1fd46e488386
  6feed3e8_55b8_cce7_b5ef_e40b872585f7["node:url"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 6feed3e8_55b8_cce7_b5ef_e40b872585f7
  08f1865f_7d4f_80e3_3c8f_b67de9d36d67["artichokie"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 08f1865f_7d4f_80e3_3c8f_b67de9d36d67
  eef951a0_aa1e_62bc_f4f7_5a835bfd58be["terserOptions"]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> eef951a0_aa1e_62bc_f4f7_5a835bfd58be
  2616bf8c_8895_7af5_fb6e_8424b9e71ea7[".."]
  be81372e_415b_426f_d3c1_132b2d458ce6 --> 2616bf8c_8895_7af5_fb6e_8424b9e71ea7
  63e3e6d8_8d0a_3775_f49f_290b042141cc["terser.spec.ts"]
  63e3e6d8_8d0a_3775_f49f_290b042141cc --> be81372e_415b_426f_d3c1_132b2d458ce6
  45981d85_cbdd_e969_8c88_c17072ea0eda["build.ts"]
  45981d85_cbdd_e969_8c88_c17072ea0eda --> be81372e_415b_426f_d3c1_132b2d458ce6
  style be81372e_415b_426f_d3c1_132b2d458ce6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { pathToFileURL } from 'node:url'
import { WorkerWithFallback } from 'artichokie'
import type {
  TerserMinifyOptions,
  TerserMinifyOutput,
} from '#types/internal/terserOptions'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '..'
import { _dirname, generateCodeFrame } from '../utils'
import { nodeResolveWithVite } from '../nodeResolve'

export interface TerserOptions extends TerserMinifyOptions {
  /**
   * 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
function loadTerserPath(root: string) {
  if (terserPath) return terserPath

  // Try resolve from project root first, then the current vite installation path
  const resolved =
    nodeResolveWithVite('terser', undefined, { root }) ??
    nodeResolveWithVite('terser', _dirname, { root })
  if (resolved) return (terserPath = resolved)

  // Error if we can't find the package
  throw new Error(
    'terser not found. Since Vite v3, terser has become an optional dependency. You need to install it.',
  )
}

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 !!(
// ... (80 more lines)

Domain

Subdomains

Frequently Asked Questions

What does terser.ts do?
terser.ts is a source file in the vite codebase, written in typescript. It belongs to the PluginSystem domain, AssetManagement subdomain.
What functions are defined in terser.ts?
terser.ts defines 2 function(s): loadTerserPath, terserPlugin.
What does terser.ts depend on?
terser.ts imports 10 module(s): .., Plugin, artichokie, generateCodeFrame, node:url, nodeResolve.ts, nodeResolveWithVite, plugin.ts, and 2 more.
What files import terser.ts?
terser.ts is imported by 2 file(s): build.ts, terser.spec.ts.
Where is terser.ts in the architecture?
terser.ts is located at packages/vite/src/node/plugins/terser.ts (domain: PluginSystem, subdomain: AssetManagement, directory: packages/vite/src/node/plugins).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free