ECMAScript 6 in WebStorm: Transpiling
In this blog post, we’ll have a look at some of the options that WebStorm offers to help you with this task.
WebStorm will start providing smart coding assistance for ES6 and some of the latest proposals, including code completion, on-the-fly inspections, navigation for modules and classes, and more.
At the moment Babel is the most commonly used transpiler. You can configure and use it via WebStorm’s File watchers. This approach is good for compiling files on the fly for some experiments and debugging (note that for the client-side code you may still need some additional such as module bundlers). For production-ready code, it might be worth using Babel in your build process with webpack, Parcel, Grunt, Gulp, or npm — and WebStorm can also help you here too.
In this blog post:
Setting up Babel File watcher
File watcher is a WebStorm built-in tool that allows you to automatically run some command-line tools on file changes. For Babel WebStorm has pre-configured File watchers. Let’s see how to enable it.
If you’re using React and JSX, you would also need to install @babel/preset-react.
Now go to Preferences | Tools | File watchers, click + button and select Babel file watcher from the list.
In the File watcher configuration, check the path to Babel: it should be in the project node_modules folder (e.g. node_modules/.bin/babel on OS X). On Windows it has to be an exe, bat, or cmd file.
All other Watcher settings are predefined, so it is now ready to use. With this default setup, compiled files will be located in the dist folder.
Of course, you can modify the configuration or add more compilation options to the .babelrc configuration file to better fit your project needs.
Configuring File watcher
Here’re the settings that you can modify in the File watcher:
File type — specify the file type that the watcher will “watch” for you and run the program. If you’re using file extensions .es6 or .jsx, select ECMAScript 6 or React JSX respectively in the drop-down list.
Scope — select the scope the watcher will “watch.” By default the scope includes all project files; however, you may want to exclude your test files or some other files that you want to edit without triggering the watcher. You can create a new Scope and set the exclusion rules in Preferences | Appearance & Behavior | Scopes.
Arguments — this is where you can specify the options for Babel CLI.
With the default configuration in WebStorm, the generated ES5 files and source maps will be saved in the dist folder in the project root. The file names and their parent directories will be preserved.
Output paths to refresh — in this field you should specify the path to the compiled files (it could be a directory where the files are saved or the files themselves, separated with colon). The IDE will watch these paths for external changes and make sure that the changes are synchronized with the project.
Working directory (under Working Directory and Environment Variables) — the directory where the program is executed. By default that’s the directory of the file.
Auto-save edited files to trigger the watcher (under Advanced Option) – disable this option if you want to run watcher only when you save the file.
Visit WebStorm’s Help portal for more details on File watchers.
Setting up task runners: Grunt and Gulp
As mentioned earlier, you can set up Babel as a task for your build tool or task runner. Since WebStorm provides integration with Grunt and Gulp, you can start the task for traspiling your ES6 files from the Show tasks window.
Make sure the project’s gruntfile.js (or gulpfile.js) is ready and all the required dependencies are installed (as well as the task runner itself). Then press Alt+F11 to see Run Grunt/Gulp task popup or open Grunt or Gulp tool window, select the task for transpiling you’ve created, and double-click it or press Enter to run.
You can create a new Run configuration for your task and start it with the Run… action (Ctrl-Alt-R on Mac or Crtl-Shift-F10 on Windows and Linux).
Using ES6 in Node.js
When you’re developing a Node.js application in ES6, one of the ways to run and test it is using @babel/register.
- Make sure you have @babel/cli and @babe/register installed in your project.
- In your Node.js run/debug configuration in the Node parameters field add:
- Save configuration and hit run or debug.
Alternatively (but we would recommend @babel/register), you can use @babel/node. To use it, you need to install it and the specify the path to the babel-node executable (in the .bin folder on macOS, .cmd or .exe file on Windows) in your node_modules folder in the Node interpreter field in the Node.js run/debug configuration.
Enabling source maps for debug
If you’d like to debug your code using WebStorm or Chrome, make sure the tools you’re using generates source maps. For example, when using Babel only, you need to add
"sourceMaps": "both" option to your .babelrc file or pass it as a command-line argument.
If you’re using Babel as a part of a more complex build process, you might need an additional configuration for generating source maps, e.g. use gulp-sourcemaps with Gulp or add
devtool: "source-map" option when using Webpack.
Source maps allow you to put breakpoints in the original source files in the IDE and be sure that they are hit then compiled code is executed in the browser.
Apart from choosing a transpiler, there are some other choices you need to make to start using ES6, like choosing a module system. We recommend consulting Axel Rauschmayer’s excellent blog post on Deploying ES6.
Develop with pleasure!
– JetBrains WebStorm Team