or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast.mdcli.mdindex.mdtransform.md
tile.json

tessl/npm-uglify-js

JavaScript parser, minifier, compressor and beautifier toolkit

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/uglify-js@3.19.x

To install, run

npx @tessl/cli install tessl/npm-uglify-js@3.19.0

index.mddocs/

UglifyJS

UglifyJS is a comprehensive JavaScript parser, minifier, compressor and beautifier toolkit. It provides both programmatic APIs and command-line tools for processing JavaScript code, offering advanced features like AST manipulation, source map generation, property mangling, scope analysis, and various compression optimizations.

Package Information

  • Package Name: uglify-js
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install uglify-js

Core Imports

const UglifyJS = require("uglify-js");

Individual exports:

const { minify, parse, Dictionary, TreeWalker, TreeTransformer } = require("uglify-js");

Basic Usage

const UglifyJS = require("uglify-js");

// Basic minification
const code = "function add(first, second) { return first + second; }";
const result = UglifyJS.minify(code);

if (result.error) {
    console.error("Error:", result.error);
} else {
    console.log(result.code); // minified output: function add(n,d){return n+d}
}

// Multiple files
const files = {
    "file1.js": "function add(a, b) { return a + b; }",
    "file2.js": "console.log(add(1, 2));"
};
const multiResult = UglifyJS.minify(files);
console.log(multiResult.code);

Architecture

UglifyJS is built around several key components:

  • Parser: Converts JavaScript code into Abstract Syntax Trees (AST)
  • Compressor: Applies various optimization techniques to reduce code size
  • Mangler: Renames variables and properties to shorter names
  • Output Generator: Converts AST back to JavaScript code with configurable formatting
  • AST Classes: Comprehensive hierarchy of node types representing JavaScript constructs
  • Tree Navigation: Walker and transformer utilities for AST traversal and modification

Capabilities

Code Minification

The primary API for processing JavaScript code with minification, compression, and mangling capabilities.

/**
 * Minify JavaScript code with comprehensive optimization options
 * @param files - Input files: string, array of strings, object with filename keys, or AST_Node
 * @param options - Minification options for parsing, compression, mangling, and output
 * @returns MinifyResult object with code, map, ast, error, warnings, and timings
 */
function minify(files, options);

Core Minification API

JavaScript Parsing

Parse JavaScript source code into Abstract Syntax Tree (AST) representation for analysis and manipulation.

/**
 * Parse JavaScript source code into AST
 * @param code - JavaScript source code string to parse
 * @param options - Parser configuration options
 * @returns AST_Toplevel node representing the parsed program
 */
function parse(code, options);

The parse function converts JavaScript source code into UglifyJS's internal AST representation, enabling programmatic analysis and transformation of JavaScript code.

Usage Examples:

const UglifyJS = require("uglify-js");

// Basic parsing
const ast = UglifyJS.parse("function test() { return 42; }");

// Parse with options
const ast2 = UglifyJS.parse("return 42;", {
    bare_returns: true  // Allow return outside functions
});

// Parse as single expression
const expr = UglifyJS.parse("42 + 24", {
    expression: true
});

AST Classes and Navigation

Comprehensive AST node hierarchy and utilities for traversing and transforming JavaScript syntax trees.

function TreeWalker(callback);
function TreeTransformer(before, after);
class Dictionary();

AST Classes and Navigation

Code Transformation

Advanced AST transformation capabilities for custom JavaScript code modifications and optimizations.

function TreeTransformer(before, after);
const List = require("uglify-js").List;
const push_uniq = require("uglify-js").push_uniq;

Code Transformation

Command Line Interface

Comprehensive CLI tool for batch processing JavaScript files with full configuration support.

uglifyjs [input files] [options]

Command Line Interface

Minification Options

Core Options

interface MinifyOptions {
  /** Parse options for input processing */
  parse?: ParseOptions;
  /** Compression options (default: {}) */
  compress?: CompressOptions | false;
  /** Name mangling options (default: true) */
  mangle?: MangleOptions | false;
  /** Output formatting options */
  output?: OutputOptions;
  /** Source map generation options */
  sourceMap?: SourceMapOptions | false;
  /** Process as ES module (default: true) */
  module?: boolean;
  /** Enable top-level variable/function mangling */
  toplevel?: boolean;
  /** Cache for mangled names across invocations */
  nameCache?: object;
  /** Return warnings in result.warnings */
  warnings?: boolean | "verbose";
  /** Handle comment annotations (/*@__PURE__*/) */
  annotations?: boolean;
  /** Parse as single expression (e.g., JSON) */
  expression?: boolean;
  /** Enclose output in IIFE with specified arguments */
  enclose?: string[] | false;
  /** Preserve function arguments from mangling */
  keep_fargs?: boolean;
  /** Preserve function names from mangling */
  keep_fnames?: boolean;
  /** Internet Explorer compatibility mode */
  ie?: boolean;
  /** Internet Explorer 8 compatibility mode */
  ie8?: boolean;
  /** Safari/WebKit compatibility mode */
  webkit?: boolean;
  /** Chrome/Node.js compatibility mode */
  v8?: boolean;
  /** Enable timing information collection */
  timings?: boolean;
  /** Enable AST validation */
  validate?: boolean;
  /** Wrap output in a function with specified name */
  wrap?: string | false;
}

Result Object

interface MinifyResult {
  /** Minified JavaScript code */
  code?: string;
  /** Generated AST (if output.ast is true) */
  ast?: AST_Toplevel;
  /** Source map (if sourceMap option enabled) */
  map?: string;
  /** Runtime error information */
  error?: {
    message: string;
    filename: string;
    line: number;
    col: number;
    pos: number;
  };
  /** Compilation warnings (if warnings option enabled) */
  warnings?: string[];
  /** Timing information (if timings option enabled) */
  timings?: {
    parse: number;
    rename: number;
    compress: number;
    scope: number;
    mangle: number;
    properties: number;
    output: number;
    total: number;
  };
}

Parse Options

interface ParseOptions {
  /** Allow return statements outside functions */
  bare_returns?: boolean;
  /** Parse as single expression (e.g., JSON) */
  expression?: boolean;
  /** Filename for error reporting */
  filename?: string;
  /** Support HTML-style comments */
  html5_comments?: boolean;
  /** Parse as ES module */
  module?: boolean;
  /** Preserve shebang line */
  shebang?: boolean;
  /** Parse in strict mode */
  strict?: boolean;
}

Utility Functions

Dictionary Class

Key-value store with special handling for the __proto__ key.

/**
 * Creates a new Dictionary instance
 * @constructor
 */
function Dictionary();

interface Dictionary {
  /** Set a key-value pair */
  set(key: string, value: any): Dictionary;
  /** Get value for key */
  get(key: string): any;
  /** Check if key exists */
  has(key: string): boolean;
  /** Delete a key */
  del(key: string): Dictionary;
  /** Add value to array for key */
  add(key: string, value: any): Dictionary;
  /** Iterate over key-value pairs */
  each(callback: (value: any, key: string) => void): void;
}

Utility Functions

/**
 * Add element to array if not already present
 * @param array - Target array
 * @param element - Element to add
 * @returns true if element was added
 */
function push_uniq(array, element);

/**
 * Check if AST node is a statement
 * @param node - AST node to check
 * @returns true if node is a statement
 */
function is_statement(node);

Usage Examples:

const UglifyJS = require("uglify-js");

// Add unique elements to an array
const deps = [];
UglifyJS.push_uniq(deps, "lodash");   // returns true, added
UglifyJS.push_uniq(deps, "react");    // returns true, added  
UglifyJS.push_uniq(deps, "lodash");   // returns false, already present

// Check if node is a statement
const ast = UglifyJS.parse("var x = 1; x + 2;");
ast.body.forEach(node => {
    console.log(UglifyJS.is_statement(node)); // true, false
});

Types

AST Node

Base interface for all AST nodes.

interface AST_Node {
  /** Start position in source */
  start: {
    line: number;
    col: number;
    pos: number;
  };
  /** End position in source */
  end: {
    line: number;
    col: number;
    pos: number;
  };
  /** Clone the node */
  clone(deep?: boolean): AST_Node;
  /** Transform node using TreeTransformer */
  transform(transformer: TreeTransformer): AST_Node;
  /** Walk through node tree using TreeWalker */
  walk(walker: TreeWalker): void;
}