CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-terser

JavaScript parser, mangler/compressor and beautifier toolkit for ES6+

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Terser

Terser is a comprehensive JavaScript minification and optimization library for ES6+ code. It provides advanced parsing, compression, mangling, and code generation capabilities with extensive configuration options and both programmatic and CLI interfaces.

Package Information

  • Package Name: terser
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install terser

Core Imports

import { minify, minify_sync, to_ascii } from "terser";

For CommonJS:

const { minify, minify_sync, to_ascii } = require("terser");

Basic Usage

import { minify } from "terser";

// Minify JavaScript code
const code = "function hello() { console.log('Hello, World!'); }";

const result = await minify(code, {
  compress: {
    dead_code: true,
    drop_console: true
  },
  mangle: {
    keep_fnames: false
  }
});

console.log(result.code);
// Output: function hello(){}

Architecture

Terser is built around several key components:

  • Parser: Converts JavaScript code into Abstract Syntax Trees (AST) using Acorn
  • Compressor: Applies various optimization passes to reduce code size and improve performance
  • Mangler: Renames variables, functions, and properties to shorter names for size reduction
  • Output Generator: Converts optimized AST back to JavaScript code with configurable formatting
  • CLI Interface: Command-line tool for batch processing and build system integration
  • Source Maps: Maintains mapping between original and minified code for debugging

Capabilities

Core Minification

Primary minification functionality providing asynchronous and synchronous interfaces for JavaScript code optimization.

function minify(
  files: string | string[] | { [file: string]: string },
  options?: MinifyOptions
): Promise<MinifyOutput>;

function minify_sync(
  files: string | string[] | { [file: string]: string },
  options?: MinifyOptions
): MinifyOutput;

function to_ascii(base64: string): string;

interface MinifyOutput {
  code?: string;
  map?: string;
  decoded_map?: object | null;
  timings?: {
    parse: number;
    rename: number;
    compress: number;
    scope: number;
    mangle: number;
    properties: number;
    format: number;
    total: number;
  };
}

Minification

Configuration and Options

Comprehensive configuration system with separate option categories for parsing, compression, mangling, and output formatting.

interface MinifyOptions {
  compress?: boolean | CompressOptions;
  mangle?: boolean | MangleOptions;
  format?: FormatOptions;
  parse?: ParseOptions;
  sourceMap?: boolean | SourceMapOptions;
  ecma?: ECMA;
  enclose?: boolean | string;
  ie8?: boolean;
  keep_classnames?: boolean | RegExp;
  keep_fnames?: boolean | RegExp;
  module?: boolean;
  nameCache?: object;
  safari10?: boolean;
  toplevel?: boolean;
}

type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;

Configuration

Command Line Interface

Full-featured CLI tool with extensive options for batch processing, build system integration, and advanced configuration.

async function run_cli(config: {
  program: import('commander').Command;
  packageJson: { name: string; version: string; [key: string]: any };
  fs: typeof import('fs');
  path: typeof import('path');
}): Promise<void>;

Command Line Interface

Advanced Features

Advanced functionality including AST manipulation, custom identifier mangling, and Mozilla SpiderMonkey AST support.

async function _default_options(): Promise<object>;

interface SimpleIdentifierMangler {
  get(n: number): string;
}

interface WeightedIdentifierMangler extends SimpleIdentifierMangler {
  consider(chars: string, delta: number): number;
  reset(): void;
  sort(): void;
}

Advanced Features

Types

interface MinifyOutput {
  code?: string;
  map?: string;
  decoded_map?: object | null;
  timings?: {
    parse: number;
    rename: number;
    compress: number;
    scope: number;
    mangle: number;
    properties: number;
    format: number;
    total: number;
  };
}

function to_ascii(base64: string): string;

type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;

type ConsoleProperty = keyof typeof console;

enum InlineFunctions {
  Disabled = 0,
  SimpleFunctions = 1,
  WithArguments = 2,
  WithArgumentsAndVariables = 3
}

enum OutputQuoteStyle {
  PreferDouble = 0,
  AlwaysSingle = 1,
  AlwaysDouble = 2,
  AlwaysOriginal = 3
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/terser@5.44.x
Publish Source
CLI
Badge
tessl/npm-terser badge