{"id":22236,"date":"2015-05-07T14:00:50","date_gmt":"2015-05-07T14:00:50","guid":{"rendered":"https:\/\/blog.jetbrains.com\/webstorm\/?p=7497"},"modified":"2021-11-30T14:59:27","modified_gmt":"2021-11-30T13:59:27","slug":"ecmascript-6-in-webstorm-transpiling","status":"publish","type":"webstorm","link":"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2015\/05\/ecmascript-6-in-webstorm-transpiling","title":{"rendered":"ECMAScript 6 in WebStorm: Transpiling"},"content":{"rendered":"<p><em>Note: This post was updated in September 2021.<\/em><\/p>\n<p>WebStorm supports the ECMAScript 6+ syntax. This support includes  ECMAScript 2015 and later standards and even some proposals to the language, such as <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Nullish_coalescing_operator\" target=\"_blank\" rel=\"noopener\">nullish coalescing operators<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Classes#private_field_declarations\" target=\"_blank\" rel=\"noopener\">private fields<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Optional_chaining\" target=\"_blank\" rel=\"noopener\">optional chaining<\/a>, and more.<\/p>\n<p>But even as ECMAScript 6+ features get more and more support in modern browsers and runtimes (see the <a href=\"http:\/\/kangax.github.io\/compat-table\/es6\/\" target=\"_blank\" rel=\"noopener\">Kangax compatibility table<\/a>), to deploy your ES6 code you still often need to compile it to ES5.1, the JavaScript version supported by all browsers.<\/p>\n<p>In this blog post, we\u2019ll take a look at some of the options that WebStorm offers to help you with this task.<\/p>\n<p><em>ECMAScript 6+<\/em> is set as the default JavaScript version in <em>Preferences \/ Settings | Languages &amp; Frameworks | JavaScript<\/em>. This setting will work for React applications as well.<\/p>\n<p><img decoding=\"async\" alt=\"Select JavaScript language version ES6+.\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_set_js_version.png\"><\/p>\n<p>With this setting, WebStorm provides smart coding assistance for ES6 and some of the latest proposals, including proper syntax highlighting, code completion, on-the-fly inspections, navigation for modules and classes, and more.<\/p>\n<p><img decoding=\"async\" alt=\"Coding assistance for ES6+.\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_coding_assistance.png\"><\/p>\n<p><a href=\"https:\/\/babeljs.io\/\" target=\"_blank\" rel=\"noopener\">Babel<\/a>, the most commonly used transpiler, can be configured and used via WebStorm\u2019s File Watchers. This approach is good for compiling files on the fly for some experiments and debugging, though for client-side code you may still need some additional tools, such as module bundlers.<\/p>\n<p>For production-ready code, it might be better to supplement Babel in your build process with <a href=\"https:\/\/webpack.js.org\/\" target=\"_blank\" rel=\"noopener\">webpack<\/a>, <a href=\"https:\/\/parceljs.org\/\" target=\"_blank\" rel=\"noopener\">Parcel<\/a>, <a href=\"https:\/\/rollupjs.org\/guide\/en\/\" target=\"_blank\" rel=\"noopener\">Rollup.js<\/a>, <a href=\"https:\/\/www.snowpack.dev\/\" target=\"_blank\" rel=\"noopener\">Snowpack<\/a>, or npm \u2013 and WebStorm can also help you here too.<\/p>\n<p>If you created your application with a <a href=\"https:\/\/www.jetbrains.com\/help\/webstorm\/creating-projects-in-product.html#ws_generate_project_from_framework\" target=\"_blank\" rel=\"noopener\">project template<\/a>, such as <a href=\"https:\/\/github.com\/facebook\/create-react-app\" target=\"_blank\" rel=\"noopener\">create-react-app<\/a>, <a href=\"https:\/\/angular.io\/cli\" target=\"_blank\" rel=\"noopener\">@angular\/cli<\/a>, or <a href=\"https:\/\/cli.vuejs.org\/\" target=\"_blank\" rel=\"noopener\">@vue\/cli<\/a>, Babel is already installed and configured and works out of the box without any additional setup.<\/p>\n<p>In this blog post we will cover the following important steps:<\/p>\n<ul>\n<li><a href=\"#install_babel_and_its_presets\">Install Babel and its presets<\/a><\/li>\n<li><a href=\"#set_up_babel_file_watcher\">Set up Babel File Watcher<\/a><\/li>\n<li><a href=\"#use_es_in_node_js\">Use ES6 in Node.js<\/a><\/li>\n<li><a href=\"#generate_source_maps_for_debugging\">Generate source maps for debugging<\/a><\/li>\n<\/ul>\n<h2 id=\"install_babel_and_its_presets\">Install Babel and its presets<\/h2>\n<p>Install <a href=\"https:\/\/babeljs.io\/docs\/en\/usage#overview\" target=\"_blank\" rel=\"noopener\">Babel<\/a> and <a href=\"https:\/\/babeljs.io\/docs\/en\/babel-preset-env\" target=\"_blank\" rel=\"noopener\">@babel\/preset-env<\/a> in your project using npm (or yarn). Open the built-in terminal and type:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">npm install --save-dev @babel\/core @babel\/cli @babel\/preset-env<\/pre>\n<p class=\"no_space\">To use Babel with React and JSX, install <a href=\"https:\/\/babeljs.io\/docs\/en\/babel-preset-react\" target=\"_blank\" rel=\"noopener\">@babel\/preset-react<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">npm install --save-dev @babel\/preset-react<\/pre>\n<h2 id=\"set_up_babel_file_watcher\">Set up Babel File Watcher<\/h2>\n<p>A File Watcher is a WebStorm system that tracks changes to your files and launches a third-party standalone application as soon as it detects a file change. WebStorm has a pre-configured File Watcher for Babel. Let\u2019s see how to enable it.<\/p>\n<p>Go to <em>Preferences \/ Settings | Tools | File Watchers<\/em>, click <em>+<\/em> on the toolbar, and select <em>Babel<\/em> from the list.<\/p>\n<p><img decoding=\"async\" alt=\"Create a Babel file watcher: select Babel template.\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_create_babel_fw_1.png\"><\/p>\n<p>In the File Watcher configuration, check the path to <a href=\"https:\/\/babeljs.io\/docs\/en\/usage#overview\" target=\"_blank\" rel=\"noopener\">Babel<\/a>: it should be in the project <em>node_modules<\/em> folder (e.g. <em>node_modules\/.bin\/babel <\/em>on macOS). On Windows it has to be an .<em>exe<\/em>, .<em>bat<\/em>, or .<em>cmd<\/em> file.<\/p>\n<p>All other File Watcher settings are predefined, so it is now ready to use. With this default setup, compiled files will be located in the <em>dist<\/em> folder.<\/p>\n<p><img decoding=\"async\" alt=\"Create Babel wale Watcher: default settings.\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_create_babel_fw_2.png\"><\/p>\n<p>Of course, you can modify the configuration or add more compilation options to the <a href=\"https:\/\/babeljs.io\/docs\/en\/configuration\" target=\"_blank\" rel=\"noopener\">Babel configuration file<\/a> to make it fit your project\u2019s needs better.<\/p>\n<h3>Babel File Watcher: additional configuration<\/h3>\n<p>Here are the settings that you can modify in the File Watcher:<\/p>\n<p><strong><em>File type<\/em><\/strong> \u2014 specify the file type that the watcher will \u201cwatch\u201d for you and run the program. If you\u2019re using the <em>.jsx<\/em> file extension, select <em>React JSX<\/em> from the drop-down list.<\/p>\n<p><img decoding=\"async\" alt=\"Configure Babel File Watcher: File Type\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_customize_babel_fw_file_extension.png\"><\/p>\n<p><strong><em>Scope<\/em><\/strong> \u2014 select the scope to watch. By default, the scope includes all project files.<\/p>\n<p><img decoding=\"async\" alt=\"ws_blog_post_es6_customize_babel_fw_select_scope\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_customize_babel_fw_select_scope.png\"><\/p>\n<p>You may want to exclude your test files or some other files that you want to edit without triggering the watcher. To do so, you can create a new scope and set the exclusion rules in <em>Preferences \/ Settings | Appearance &amp; Behavior | Scopes<\/em>.<\/p>\n<p><img decoding=\"async\" alt=\"Configure Babel  File Watcher: configure a custom scope.\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_customize_babel_fw_scope_create_custom_scope.png\"><\/p>\n<p><strong><em>Arguments<\/em><\/strong> \u2014 this is where you can specify the options for <a href=\"https:\/\/babeljs.io\/docs\/usage\/cli\/\" target=\"_blank\" rel=\"noopener\">Babel CLI<\/a>.<\/p>\n<p>With the default configuration in WebStorm, the generated ES5 files and source maps will be saved in the <em>dist<\/em> folder in the project root. The file names and their parent directories will be preserved.<\/p>\n<p><img decoding=\"async\" alt=\"Dist folder with compiled ES5 files and source maps.\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_dist_folder.png\"><\/p>\n<p><strong><em>Output paths to refresh<\/em><\/strong><strong> <\/strong>\u2014 in this field, specify the path to the compiled files. You can enter a directory where the files are saved or the files themselves, separated with a colon. The IDE will watch these paths for external changes and make sure that the changes are synchronized with the project.<\/p>\n<p><strong><em>Working directory<\/em><\/strong> (under <em>Working Directory and Environment Variables<\/em>) \u2014 the directory where the program is executed. By default this is the directory of the file.<\/p>\n<p><strong><em>Auto-save edited files to trigger the watcher<\/em><\/strong> (under <em>Advanced Options<\/em>) \u2014 disable this option if you want to run the watcher only when you save the file.<\/p>\n<p>For more details on File Watchers, please visit <a href=\"https:\/\/www.jetbrains.com\/webstorm\/help\/using-file-watchers.html\" target=\"_blank\" rel=\"noopener\">WebStorm\u2019s Help portal<\/a>.<\/p>\n<h2 id=\"use_es_in_node_js\">Use ES6 in Node.js<\/h2>\n<p>When you\u2019re developing a Node.js application in ES6, the recommended way to run and test it is to use <a href=\"https:\/\/babeljs.io\/docs\/en\/babel-register\" target=\"_blank\" rel=\"noopener\">@babel\/register<\/a>.<\/p>\n<ol>\n<li>Make sure you have <a href=\"https:\/\/babeljs.io\/docs\/en\/usage#overview\" target=\"_blank\" rel=\"noopener\">@babel\/cli<\/a> and <a href=\"https:\/\/babeljs.io\/docs\/en\/babel-register\" target=\"_blank\" rel=\"noopener\">@babel\/register<\/a> installed in your project. In the built-in terminal, type:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">npm install --save-dev @babel\/core @babel\/register<\/pre>\n<\/li>\n<li>Make sure you have added a <a href=\"https:\/\/babeljs.io\/docs\/en\/config-files\" target=\"_blank\" rel=\"noopener\">Babel configuration file<\/a> where <code>@babel\/register<\/code> is specified in the <code>presets<\/code> section:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">{\n  &quot;presets&quot;: [\n    [\n      &quot;@babel\/env&quot;\n    ]\n  ]\n}<\/pre>\n<\/li>\n<li>Open your Node.js run\/debug configuration and in the <em>Node parameters<\/em> field, type <code>-r @babel\/register<\/code>. Don\u2019t forget to specify the path to the JavaScript file to run.<br \/>\n<img decoding=\"async\" alt=\"ws_blog_post_es6_with_node_run_debug_config\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_with_node_run_debug_config.png\"><\/li>\n<li>Save the configuration and click <em>Run<\/em> or <em>Debug<\/em> on the toolbar.<\/li>\n<\/ol>\n<p>Although @babel\/register is strongly recommended, you can alternatively use <a href=\"https:\/\/babeljs.io\/docs\/en\/babel-node\" target=\"_blank\" rel=\"noopener\">@babel\/node<\/a>.<\/p>\n<ol>\n<li>Install @babel\/node. In the built-in terminal, type:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">npm install --save-dev @babel\/core @babel\/node<\/pre>\n<\/li>\n<li>Open your Node.js run\/debug configuration and in the <em>Node interpreter<\/em> field, specify the path to the <em>babel-node<\/em> executable in your <em>node_modules<\/em> folder (<em>.bin\/babel-node<\/em> on macOS, <em>babel-node.cmd<\/em> or <em>babel-node.exe<\/em> on Windows).<br \/>\n<img decoding=\"async\" alt=\"Node.js run\/debug configuration with Babel.\" width=\"600\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_babel_node_run_config.gif\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/09\/ws_blog_post_es6_babel_node_run_config.png\"><\/li>\n<\/ol>\n<h2 id=\"generate_source_maps_for_debugging\">Generate source maps for debugging<\/h2>\n<p>To debug your code in WebStorm or Chrome, make sure the tools you\u2019re using generate source maps. Source maps allow you to put breakpoints in the original source files in the IDE and be sure that they are hit when the compiled code is executed in the browser.<\/p>\n<p>When using Babel only, add the <em>&#8220;sourceMaps&#8221;: &#8220;both&#8221;<\/em> <a href=\"https:\/\/babeljs.io\/docs\/en\/options#sourcemaps\" target=\"_blank\" rel=\"noopener\">option<\/a> to your <a href=\"https:\/\/babeljs.io\/docs\/en\/config-files\" target=\"_blank\" rel=\"noopener\">Babel configuration file<\/a> (for example, <em>.babelrc.json <\/em>or<em> babel.config.json<\/em>) or pass it as a command-line argument.<\/p>\n<p>If you\u2019re using Babel as part of a more complex build process, you might need an additional configuration for generating source maps, such as <a href=\"https:\/\/www.npmjs.com\/package\/gulp-sourcemaps\" target=\"_blank\" rel=\"noopener\">gulp-sourcemaps<\/a> for working with Gulp or the <em>devtool: &#8220;source-map&#8221;<\/em> <a href=\"https:\/\/webpack.js.org\/configuration\/#options\" target=\"_blank\" rel=\"noopener\">option<\/a> when using Webpack.<\/p>\n<p>Apart from choosing a transpiler, there are some other choices you need to make to start using ES6. For example, you need to select a module system. We recommend consulting Axel Rauschmayer\u2019s excellent blog post on <a href=\"http:\/\/www.2ality.com\/2015\/04\/deploying-es6.html\" target=\"_blank\" rel=\"noopener\">Deploying ES6<\/a>.<\/p>\n<p><em>The WebStorm team<\/em><\/p>\n","protected":false},"author":221,"featured_media":180380,"comment_status":"open","ping_status":"open","template":"","categories":[601],"tags":[2798,2797,400,2734],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22236"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/webstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/221"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=22236"}],"version-history":[{"count":5,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22236\/revisions"}],"predecessor-version":[{"id":208050,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22236\/revisions\/208050"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/180380"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=22236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=22236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=22236"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=22236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}