首页 > 解决方案 > babel 没有使用 .babelrc。为什么?

问题描述

我正在尝试学习 babel。我得到了 babel-core 模块的工作,但我正在尝试使用.babelrc它并没有做任何事情。

这是我的.babelrc文件。

{
    "plugins":["transform-es3-property-literals"]
}

这是我的代码:

var babel = require("babel-core");

var js = `var x = { catch: 4, bar: 7 };`;

var notUsingBabelRc = babel.transform(js,{
    plugins: ["transform-es3-property-literals"]
}).code;

var usingBabelRc = babel.transform(js).code

console.log(notUsingBabelRc == usingBabelRc);
//false, but should be true. Adding plugins as an option transforms the code.

console.log(usingBabelRc == js);
//true, but should be false. The code is not changed from its original form.

.babelrc在项目的根目录中有该文件。我还有我的脚本文件,称为using_babelrc.js项目的根目录。

然后我打电话node using_babelrc,我得到false true即使我期待true false

我错过了什么简单的事情?

标签: babeljs

解决方案


transform函数还需要filename提供的选项来开始查找.babelrc与该文件名相关的文件。在你的情况下:

babel.transform(js, {filename: "using_babelrc.js"}).code;

将读取与using_babelrc.js.


推荐阅读