So you've heard about ECMAScript 2015 and want to start using the new and shiny language features? Great! This post gives you some suggestions as to how to introduce ECMAScript 2015 in your projects.
Use ECMAScript 2015 with Node.js
Are you running a current version of Node.js in one of your projects? In that case, you can dive into ECMAScript 2015 right away. Starting with v4.0.0, many of the new features are supported natively, no additional tooling required:
As of February 2016, the most recent version of Node.js supports the following ECMAScript 2015 features without experimental flags:
- let and const
- Map and WeakMap
- Set and WeakSet
- Typed Arrays
- Binary and Octal Literals
- Enhanced Object Literals
- New String Methods
- Template Strings
- Arrow Functions
- Spread Operator
Write Separate Scripts in ECMAScript 2015
If you feel uncomfortable introducing ECMAScript 2015 in your production applications right away, consider starting at a smaller scale. The next time you find yourself writing a little internal Node.js script for your team, start using some of the new features that are already supported natively.
That way, you enjoy a low barrier to entry and at the same time take only a limited risk. It's not your production code you're updating, but merely a little helper script that can easily be replaced if need be.
Write Tests in ECMAScript 2015
The idea is to write your new tests using ECMAScript 2015 features. Don't worry, there's no need to rewrite all your existing tests. An arguably reasonable strategy might be to update tests that you're modifying for whatever reason (e.g. because of changing requirements). That way, you're doing a creeping migration over time.
Start with Easy-to-Transpile Features
Some ECMAScript 2015 features are easier for a transpiler to rewrite than others. Arrow functions, for example, can be expressed by semantically equivalent function expressions with proper
this-binding. Some other features that can be transpiled cleanly are:
All of the above features can be rewritten using only language contructs supported in ECMAScript 5. Also, no additional libraries or polyfills have to be included to make the generated code work at runtime.
Turn It up to Eleven
Some ECMAScript 2015 features require additional work on your side to seamlessly integrate with the rest of your application. Native modules, for example, are a great way to structure your code. However, you'll need another tool like Browserify or Rollup to bundle those modules and resolve their dependencies. Higher effort, higher reward!
Getting started with ECMAScript 2015 can be as easy as using the features that Node.js already implements natively. Consider starting to use some of the new language features in your scripts or unit tests.
For client-side web development and the diverse browser landscape, add a transpiler such as Babel to your build steps. Finally, a module bundler enables you to use the new native modules to structure your code base.