如何让 Babel 6 编译为 ES5 javascript? [英] How do I get Babel 6 to compile to ES5 javascript?
问题描述
我已经安装了最新版本的 babel.目前是 6.4.0.我创建了一个名为 myclass.js 的文件,其中包含以下代码.
I've installed the latest version of babel. Currently 6.4.0. I create a file called myclass.js that has the following code.
class MyClass {
constructor(name) {
console.log("I am a MyClass object .. ", name);
}
}
var myclass = new MyClass('1234');
创建我的类后,我在命令行中执行以下操作.
after creating my class I do the following in the command line.
$> babel ./src/myclass.js --out-file ./out/app.js
我希望我的 app.js 文件有 es5 编译的 javascript,但它包含与 myclass.js 文件完全相同的代码.我错过了什么可能导致这种情况?
I would expect my app.js file to have es5 compiled javascript but it has the same exact code in it that the myclass.js file has. What am I missing that may be causing this?
推荐答案
使用 babel 和 nodejs(并使用 CLI)的确切答案:
The exact answer using babel with nodejs (and using CLI):
npm install babel-cli babel-preset-es2015
使用 npx 执行
npx babel ./src/myclass.js --out-file ./out/app.js --presets babel-preset-es2015
注意
全局安装 babel-cli 时可能无法正常工作.所以这是我的最终解决方案
Note
When babel-cli is globally installed could be not working. So this is my definitive solution
这篇关于如何让 Babel 6 编译为 ES5 javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!