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, 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. For production-ready code, it might be worth using Babel in your build process — 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.
Install Babel CLI in your project via npm:
npm install --save-dev babel-cli
Now go to Preferences | Tools | File watchers, click + button and select Babel file watcher from the list.
In the File watcher configuration, specify the path to Babel CLI 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.
In addition to that, you need to install Babel 6 preset for transpiling ECMAScript 6 locally in your project (for JSX you need babel-preset-react plugin instead). To do that open the Terminal at your application root directory and run:
npm install --save-dev babel-preset-es2015
Add .babelrc file to your project with the following code to enable the preset:
All other Watcher settings are predefined, so it is now ready to use. With this default setup, compiled files will be located next to the original files.
Of course, you can modify the configuration or add more compilation options to the .babelrc 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 JSX Harmony 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 next to ES6 files with “-compiled” added to the file name.
Working directory — the directory where the program is executed. By default that’s the directory of the file.
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.
Let’s see how we can configure Babel File watcher to make it save all the compiled files in out directory. We’ll need to:
1. Create directory out, right-click on it in the Project view and select Mark as excluded.
2. Change the arguments to:
$FilePathRelativeToProjectRoot$ --source-maps --out-dir out (so that the files would be compiled and saved with their parent folders in out folder)
3. Set Working directory to the project root: $ProjectFileDir$
4. Set out as Output paths to refresh.
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).
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 a addition 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