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
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

  • By
    Create a Sexy Persistent Header with Opacity Using MooTools or jQuery

    I've been working with the Magento eCommerce solution a lot lately and I've taken a liking to a technique they use with the top bar within their administrative control panel. When the user scrolls below a specified threshold, the top bar becomes attached to the...

Discussion

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