Babel文件未经转换即被复制 [英] Babel file is copied without being transformed
问题描述
我有这段代码:
"use strict";
import browserSync from "browser-sync";
import httpProxy from "http-proxy";
let proxy = httpProxy.createProxyServer({});
我安装了 babel-core
和全球通过npm babel-cli
。关键是当我尝试在我的终端上使用它进行编译时:
and I have installed babel-core
and babel-cli
globally via npm. The point is when I try to compile with this on my terminal:
babel proxy.js --out-file proxified.js
输出文件被复制而不是编译(我的意思是,它与源文件相同)。
The output file gets copied instead of compiled (I mean, it's the same as the source file).
我在这里缺少什么?
推荐答案
Babel是一个转型框架。在6.x之前,默认情况下它启用了某些转换,但随着Node版本的使用增加,本地支持许多ES6功能,事情可配置变得更加重要。默认情况下,Babel 6.x不执行任何转换。你需要告诉它要运行什么转换:
Babel is a transformation framework. Pre-6.x, it enabled certain transformations by default, but with the increased usage of Node versions which natively support many ES6 features, it has become much more important that things be configurable. By default, Babel 6.x does not perform any transformations. You need to tell it what transformations to run:
npm install babel-preset-env
并运行
babel --presets env proxy.js --out-file proxified.js
或创建 .babelrc
包含
{
"presets": [
"env"
]
}
并像以前一样运行它。
and run it just like you were before.
env
在这种情况下是一个预设,它基本上表示将所有标准ES *行为编译为ES5。如果您使用的是支持某些ES6的Node版本,您可能需要考虑这样做
env
in this case is a preset which basically says to compile all standard ES* behavior to ES5. If you are using Node versions that support some ES6, you may want to consider doing
{
"presets": [
["env", { "targets": { "node": "true" } }],
]
}
告诉预设仅处理Node版本不支持的内容。如果您需要浏览器支持,还可以在目标中包含浏览器版本。
to tell the preset to only process things that are not supported by your Node version. You can also include browser versions in your targets if you need browser support.
这篇关于Babel文件未经转换即被复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!