ES6 JavaScript Minifier: babili

By  on  
Babel

The Babel toolchain is amazing.  We've used Babel to write ES6 JavaScript well before ES6 features hit browsers, we use it to parse JavaScript and write JSX, and much more.  Babel can do just about anything with the JavaScript language and I've just learned of another use for the Babel toolchain:  babili, the ES6+ aware JavaScript minifier.

At the time of its creation, many other JavaScript minifiers weren't capable of ES6 minifying but when you have an amazing, forward-thinking parser like Babel, creating an ES6+ aware minifier becomes much easier.

Using babili from Command Line

I prefer to use babili from command line when doing a quick minification:

// Sample source code:
class Mangler {
  constructor(program) {
    this.program = program;
  }
}
new Mangler(); // without this it would just output nothing since Mangler isn't used

// Before:
// ES2015+ code -> Babel -> Babili/Uglify -> Minified ES5 Code
var a=function a(b){_classCallCheck(this,a),this.program=b};new a;

// After:
// ES2015+ code -> Babili -> Minified ES2015+ Code
class a{constructor(b){this.program=b}}new a;

Note that babili does not transpile ES5 JavaScript before minifying -- you'll need to use an excellent tool like lebab to get ES6+ from ES5.  Assuming you do have ES6 in your source file, you'll notice all of your code has been elegantly minified by babili!

Using babili via Node.js

Being a JavaScript parser implemented in JavaScript, babili is also available for use via Node.js APIs.  There are also dozens of babili plugins you can use to customize your minification.  Check out the documentation for impressive benchmarks and Node.js usage information.

Babel has had as much of an impact on writing JavaScript for the web as any popular JavaScript frameworks and will continue to do so in the future.  If you haven't explored the Babel toolchain and it's plethora of plugins, take the time to give them a look.  Being able to write and experiment with edge JavaScript language features before they hit the browsers is an advantage to any developer, whether it be in getting them a job or simply keeping them up to speed with the language.

Recent Features

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Ana Tudor’s Favorite CodePen Demos

    Cocoon I love canvas, I love interactive demos and I don't think I have ever been more impressed by somebody's work than when I discovered what Tiffany Rayside has created on CodePen. So I had to start off with one of her interactive canvas pens, even though...

  • By
    Spatial Navigation

    Spatial navigation is the ability to navigate to focusable elements based on their position in a given space.  Spatial navigation is a must when your site or app must respond to arrow keys, a perfect example being a television with directional pad remote.  Firefox OS TV apps are simply...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!