A browserify transform which minifies your code using Terser
npx @tessl/cli install tessl/npm-uglifyify@5.0.0Uglifyify 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.
npm install uglifyifyconst uglifyify = require('uglifyify');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.jsUglifyify operates as a Browserify transform that:
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;
}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']
});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/*'
]
});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 });Pass custom options to the Terser minifier:
bundler.transform(uglifyify, {
compress: {
conditionals: false,
drop_console: true
},
mangle: {
reserved: ['$', 'jQuery']
},
output: {
comments: false
}
});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.jsThe transform emits 'error' events for:
const stream = fs.createReadStream('input.js')
.pipe(uglifyify('input.js'));
stream.on('error', (err) => {
console.error('Minification failed:', err.message);
});bundler
.transform('babelify') // First: transpile ES6+ to ES5
.transform('envify') // Second: replace environment variables
.transform(uglifyify); // Last: minify the resultconst bundler = browserify('./src/index.js');
if (process.env.NODE_ENV === 'production') {
bundler.transform(uglifyify, {
global: true,
compress: {
drop_console: true,
drop_debugger: true
}
});
}