简体   繁体   English

使ES6在WebStorm中工作

[英]Getting ES6 to work in WebStorm

Any ideas on how to use ES6 on WebStorm 10? 关于如何在WebStorm 10上使用ES6的任何想法?

Here's what I have done so far: 到目前为止,这是我所做的:

  1. Installed Babel. 已安装Babel。
  2. Activated Babel with Settings > Tools > File watchers. 通过设置>工具>文件监视程序激活Babel。 checked Babel checkbox. 选中了Babel复选框。
  3. Edit configurations > Before launch file watchers > + then 'Run File Watchers". 编辑配置>启动文件监视程序之前> +,然后单击“运行文件监视程序”。
  4. Set code as ES6 将代码设置为ES6
  5. Changed my run configuration to use the compiled version. 将运行配置更改为使用编译版本。

Then created the following trivial piece of code: 然后创建以下琐碎的代码:

require("babel/register");
function* count(n){
    console.log(n);
}

This code shows up with no syntax errors, as it should. 该代码应正确显示,没有语法错误。 (A convenient way to make sure ES6 is turned on). (确保已打开ES6的便捷方法)。

Run it (run the compiled version, actually), and get this .... 运行它(实际上运行编译版本),然后获取它。

var marked0$0 = [count].map(regeneratorRuntime.mark);
                            ^
ReferenceError: regeneratorRuntime is not defined

Why? 为什么? How do I get it to precompile with Babel and then run? 如何使用Babel进行预编译然后运行? Isn't the regenerator supposed to be taken care of with this line: 这行不应该处理再生器吗?

require("babel/register");

(Windows 7, if that is important). (如果重要,则为Windows 7)。

Babel's require hook requires you to have BABEL_CACHE_PATH environment variable. Babel的require挂钩要求您具有BABEL_CACHE_PATH环境变量。 You might need to specify them as well in your File Watcher configuration in WebStorm. 您可能还需要在WebStorm的File Watcher配置中指定它们。

The "require hook" only works on files that you require after registering it, but not on the file that registers the hook itself. “要求挂钩”仅适用于您在注册后所需的文件,而不适用于注册挂钩本身的文件。

So this works: 所以这工作:

// index.js
require('babel/register');
require('./count');

// count.js
function* count(n){
  console.log(n);
};

I think there are two things you probably need to do to make it work based on the nature of the error you're describing. 我认为根据您描述的错误的性质,可能需要做两件事才能使其正常工作。

  1. npm install babel-core
  2. add --optional runtime as an argument to the invocation of babel 添加--optional runtime作为调用babel的参数

This is based on what's described in some detail here . 这是基于此处详细介绍的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM