or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-uglifyify

A browserify transform which minifies your code using Terser

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/uglifyify@5.0.x

To install, run

npx @tessl/cli install tessl/npm-uglifyify@5.0.0

index.mddocs/

Uglifyify

Uglifyify is a Browserify transform that minifies JavaScript code using Terser (a maintained fork of UglifyJS2). It processes files individually before bundling, enabling more aggressive dead code elimination and conditional require optimization compared to post-bundle minification.

Package Information

  • Package Name: uglifyify
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install uglifyify

Core Imports

const uglifyify = require('uglifyify');

Basic Usage

const browserify = require('browserify');
const fs = require('fs');

// Basic usage as a transform
const bundler = browserify('./index.js');
bundler.transform(uglifyify);
bundler.bundle().pipe(fs.createWriteStream('./bundle.js'));

// Global transform to minify all modules
bundler.transform(uglifyify, { global: true });

// Command line usage
// browserify -t uglifyify ./index.js > bundle.js
// browserify -g uglifyify ./index.js > bundle.js

Architecture

Uglifyify operates as a Browserify transform that:

  • File-level Processing: Minifies each file individually before bundling
  • Stream Interface: Uses Node.js streams via the 'through' package
  • Selective Processing: Filters files by extension and ignore patterns
  • Source Map Integration: Preserves and generates source maps when debug mode is enabled
  • Dead Code Elimination: Removes unreachable code paths for conditional requires

Capabilities

Transform Function

Creates a transform stream that minifies JavaScript files using Terser.

/**
 * Creates a transform stream for minifying JavaScript files
 * @param {string} file - Path to the file being transformed
 * @param {Object} opts - Configuration options
 * @returns {Stream} Transform stream that processes the file
 */
function uglifyify(file, opts);

Configuration Options:

interface UglifyifyOptions {
  /** Glob patterns to ignore specific files */
  ignore?: string | string[];
  /** File extensions to process (e.g., ['.js', '.coffee']) */
  exts?: string[];
  /** Alternative way to specify file extensions */
  x?: string[];
  /** Whether to apply transform globally to all modules */
  global?: boolean;
  /** Enable/disable source map generation (auto-detected from debug flag) */
  sourceMap?: boolean;
  /** Terser compression options */
  compress?: boolean | object;
  /** Terser mangling options */
  mangle?: boolean | object;
  /** Terser parsing options */
  parse?: object;
  /** Terser beautify options */
  beautify?: boolean | object;
  /** Terser output options */
  output?: object;
  /** Global variable definitions for dead code elimination */
  define?: object;
  /** Internal flags object */
  _flags?: { debug?: boolean };
}

Command Line Option Mappings:

The following command line flags are automatically mapped to Terser options:

interface CommandLineMappings {
  /** -c flag maps to compress option */
  c?: boolean | object;
  /** -m flag maps to mangle option */
  m?: boolean | object;
  /** -p flag maps to parse option */
  p?: object;
  /** -b flag maps to beautify option */
  b?: boolean | object;
  /** -o flag maps to output option */
  o?: object;
  /** -d flag maps to define option */
  d?: object;
}

Usage Examples

File Extension Filtering

Process only specific file types to avoid errors with non-JavaScript files:

// Command line
// browserify -t [ uglifyify -x .js -x .coffee ]

// Programmatic
bundler.transform(uglifyify, {
  exts: ['.js'],
  x: ['.coffee']
});

Ignoring Files

Skip minification for specific files or patterns:

// Command line
// browserify -g [ uglifyify --ignore '**/node_modules/weakmap/*' ]

// Programmatic
bundler.transform(uglifyify, {
  global: true,
  ignore: [
    '**/node_modules/weakmap/*',
    '**/node_modules/async/*'
  ]
});

Source Maps

Enable source map generation with debug mode:

// Command line
// browserify -t uglifyify --debug index.js

// Programmatic
const bundler = browserify({ debug: true });
bundler.transform(uglifyify);

// Disable source maps explicitly
bundler.transform(uglifyify, { sourceMap: false });

Advanced Terser Options

Pass custom options to the Terser minifier:

bundler.transform(uglifyify, {
  compress: {
    conditionals: false,
    drop_console: true
  },
  mangle: {
    reserved: ['$', 'jQuery']
  },
  output: {
    comments: false
  }
});

Dead Code Elimination

Combine with envify for environment-based code elimination:

// This code in your module:
if (process.env.NODE_ENV === 'development') {
  module.exports = require('./development');
} else {
  module.exports = require('./production');
}

// Command line compilation:
// NODE_ENV=production browserify -t envify -t uglifyify index.js -o prod.js

Error Handling

The transform emits 'error' events for:

  • Minification Errors: When Terser fails to parse or minify code
  • File Processing Errors: When file reading or stream processing fails
  • Source Map Errors: When source map parsing or generation fails
const stream = fs.createReadStream('input.js')
  .pipe(uglifyify('input.js'));

stream.on('error', (err) => {
  console.error('Minification failed:', err.message);
});

File Processing Logic

  1. Ignore Check: Files matching ignore patterns return a passthrough stream
  2. Extension Filter: JSON files and non-matching extensions are skipped
  3. Content Accumulation: File content is buffered completely before processing
  4. Minification: Terser processes the buffered content with provided options
  5. Source Map Handling: Existing source maps are preserved and new ones generated
  6. Output: Minified code is emitted with optional source map comments

Integration Patterns

With Other Transforms

bundler
  .transform('babelify')      // First: transpile ES6+ to ES5
  .transform('envify')        // Second: replace environment variables
  .transform(uglifyify);      // Last: minify the result

Production Builds

const bundler = browserify('./src/index.js');

if (process.env.NODE_ENV === 'production') {
  bundler.transform(uglifyify, {
    global: true,
    compress: {
      drop_console: true,
      drop_debugger: true
    }
  });
}