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
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Dynamically Load Stylesheets Using MooTools 1.2

    Theming has become a big part of the Web 2.0 revolution. Luckily, so too has a higher regard for semantics and CSS standards. If you build your pages using good XHTML code, changing a CSS file can make your website look completely different.

  • By
    Web Audio API

    The Web Audio API allows developers to load and decode audio on demand using JavaScript.  The more I evaluate awesome games for Firefox OS TVs, the more I get to learn about these APIs that I normally wouldn't touch.  The following is a very basic introduction to the WebAudio API...

Discussion

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