パッケージの詳細

butternut

Rich-Harris34.3kMIT0.4.6

Experimental ES2015-aware minifier

javascript, transpilation, compilation, esnext

readme

Butternut

The fast, future-friendly minifier. Try before you buy at butternut.now.sh

Warning: this is alpha software. Test thoroughly before using in production! Consider using the check option. Please report any bugs you find!

Why?

Butternut is significantly faster than other JavaScript minifiers, and works with the latest version of JavaScript (ES2015, aka ES6, and beyond). It's typically around 3x faster than UglifyJS with default minify options, and 10-15x faster than Babili.

The compression is better than Babili and closure-compiler-js (in standard compilation mode — you can get better results with Closure in advanced mode, but only by writing your code in a very particular way). It's almost as good as Uglify in its current version.

You can test out the different tools with npm run bench.

Note: UglifyJS supports ES2015+ as of very recently — see uglify-es.

How?

The traditional approach to minification is this: parse your source code into an abstract syntax tree (AST) using something like Acorn, manipulate the AST, and finally generate code from it.

Butternut takes a different approach. It uses Acorn to generate an AST, but instead of steps 2 and 3 it then edits the code in place using magic-string — which is much less costly than AST manipulation and code generation.

Usage

The easiest way to use Butternut is to plug it into your existing build process:

Alternatively, you can use it directly via the CLI or the JavaScript API:

Command Line Interface

Install Butternut globally, then use the squash command:

npm install --global butternut # or npm i -g butternut
squash app.js > app.min.js

Run squash --help to see the available options.

JavaScript API

Install Butternut to your project...

npm install --save-dev butternut # or npm i -D butternut

...then use it like so:

const butternut = require('butternut');
const { code, map } = butternut.squash(source, options);

The options argument, if supplied, is an object that can have the following properties:

Option CLI equivalent Default value Description
check --check false Parse output. See below
allowDangerousEval n/a false Whether to allow direct eval calls
sourceMap -m, --sourcemap true Whether to create a sourcemap. Set to inline to append to the output (not recommended)
file n/a (automatic) null The output filename, used in sourcemap generation
source n/a (automatic) null The source filename, used in sourcemap generation
includeContent n/a true Whether to include the source file in the sourcesContent property of the generated sourcemap

The check option

Since Butternut is a new project, it hasn't yet been battle-tested. It may generate code that you don't expect. If you pass check: true (or use the --check flag, if using the CLI), Butternut will parse the generated output to verify that it is valid JavaScript. If not, it means it's messed something up, in which case it will try to help you find the code that it failed to minify correctly.

If you find bugs while using Butternut, please raise an issue!

License

MIT

更新履歴

butternut changelog

0.4.6

  • Too many fixes to list individually!

0.4.5

  • Remove unused function expression IDs (#98)
  • Rewrite Infinity as 1/0 (#99)
  • Remove parentheses from new expression without parameters (#100)
  • Remove leading zero from numbers, use e notation where appropriate (#102)
  • Fix order of edits around if statements (#109, #115, #117)
  • Preserve initialisers of unused variables if they may contain side-effects (#101)

0.4.4

  • Dozens of minor fixes (#91)
  • Prevent shadowing of non-aliased variables (#94)
  • Minify static class methods (#88)
  • Handle empty else blocks in if statements (#89)
  • Prevent a=a!==false becoming a!===!1 (#93)

0.4.3

  • Don't insert semicolons before else keywords that follow blocks, try statements or empty statements (#82)
  • More informative CLI error messages if repro is generated (#82)

0.4.2

  • Hoist var declarations out of removed blocks (#51)
  • More conservative constant folding (#74)
  • Allow reserved words (#79)
  • Preserve/insert parens around object literals as necessary (#77)

0.4.1

  • Remove certain side-effect-free statements (#27)
  • More efficient mangling (#55)
  • Fold constants in template literals (#62)
  • Fold array method calls in some cases (#67)
  • Remove redundant 'use strict' pragmas (#64)
  • Prevent over-eager folding of binary/logical expressions (#75)
  • Remove empty blocks (#70)

0.4.0

  • Disallow direct eval calls by default, and deopt if explicitly allowed (#31)
  • Fold exponentiation expressions (#50)
  • Remove code correctly from LogicalExpression nodes if neft hand side is truthy (#54)
  • Handle blocks with leading empty statements (#57)
  • Insert space before returned arrow function with single parameter (#52)

0.3.6

  • Correctly minify async and generator functions and methods (#43)
  • Only create scope for loops that declare variables in head (#46)
  • Remove curly braces around else-block in if-statement with falsy condition (#41)
  • Handle duplicate variables in sibling loops (#33)
  • Minify expressions in template literals (#34)
  • Handle loops with empty bodies (#38)

0.3.5

0.3.4

  • Remove ID of shadowed function expressions (#18)
  • Correctly separate SwitchCase consequent statements (#19)
  • Remove whitespace around NewExpression arguments (#16)

0.3.3

  • Remove curlies around blocks that don't need them, and vice versa (#9)

0.3.2

  • Handle duplicate variable declarations (#3)
  • Preserve shorthand property names (#6)

0.3.1

  • Fix browser build

0.3.0

  • Loads of fixes (now tested with many of the most popular packages on npm)
  • Add check option

0.2.0

  • Renamed to Butternut

0.1.2

  • Various

0.1.1

  • Better computed member expressions (#1)

0.1.0

  • First (experimental) release