Skip to main content
The minifier-js package provides JavaScript minification capabilities for Meteor applications using the Terser minifier. It’s automatically used during production builds to reduce bundle sizes.

Overview

This package exports the meteorJsMinify function that minifies JavaScript code asynchronously using Terser 5.31.0. It’s designed to work seamlessly with Meteor’s build system and includes smart defaults for optimal production code.

Dependencies

Installation

The package is included by default in Meteor’s standard build chain. For manual use:

API

meteorJsMinify(source)

Minifies JavaScript source code using Terser. Parameters:
  • source (string): JavaScript code to minify
Returns: Promise<Object>
  • code (string): Minified JavaScript code
  • minifier (string): Always returns 'terser'
Example:

Configuration

The minifier uses the following Terser options:

Compression Options

drop_debugger: false
  • Keeps debugger; statements in production
  • Allows debugging in production when needed
  • Can be useful for troubleshooting live issues
unused: false
  • Preserves unreferenced functions and variables
  • Prevents issues with dynamic code access
  • Safer for Meteor’s dynamic loading patterns
dead_code: true
  • Removes unreachable code paths
  • Eliminates code after return, throw, break, or continue
  • Reduces bundle size without breaking functionality
typeofs: false global_defs
  • Defines process.env.NODE_ENV at compile time
  • Enables dead code elimination for environment checks
  • Example:

Safari 10 Fix

safari10: true
  • Works around the Safari 10/11 async/await bug
  • Prevents runtime errors with async functions
  • See: Terser Issue #117

NODE_ENV Handling

The minifier respects the NODE_ENV environment variable:
Usage:
Code optimizations based on NODE_ENV:

Integration

Build Plugin

The minifier is used by Meteor’s build system during production builds:

Custom Minification

For custom build processes:

Performance

The minifier is:
  • Asynchronous: Uses async/await for non-blocking minification
  • Lazy-loaded: Terser is loaded on first use to improve startup time
  • Compatible: Works with Meteor’s caching system

Backwards Compatibility

The package maintains backwards compatibility:
Previous versions may have returned different properties, but current version ensures:
  • result.code - Always contains minified code
  • result.minifier - Always set to 'terser'

Best Practices

Environment-Specific Code

Use environment checks for code splitting:

Preserve Important Code

Avoid patterns that might be incorrectly removed:

Debugging Production

Since drop_debugger: false, you can use debugger statements:

Testing

The package includes comprehensive tests:
Run tests:

Common Issues

Issue: Code breaks after minification

Solution: Check for:
  • Missing semicolons
  • Reserved keyword usage
  • Eval or Function constructor usage
  • Dynamic property access

Issue: Larger bundle than expected

Solution: Verify:
  • unused: false prevents removal of seemingly unused code
  • Environment-specific code is properly wrapped in process.env.NODE_ENV checks
  • No circular dependencies preventing tree-shaking

Issue: Safari compatibility issues

Solution:
  • The safari10: true option is already enabled
  • Ensure you’re using supported async/await patterns
  • Test in Safari 10/11 if targeting those versions

Alternatives

For different minification strategies:

Version History

  • 3.1.0 - Current version using Terser 5.31.0
  • Uses Terser instead of legacy UglifyJS
  • Async-first API
  • Safari 10/11 compatibility built-in