babeljs - 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
。
我错过了什么简单的事情?
解决方案
该transform
函数还需要filename
提供的选项来开始查找.babelrc
与该文件名相关的文件。在你的情况下:
babel.transform(js, {filename: "using_babelrc.js"}).code;
将读取与using_babelrc.js
.
推荐阅读
- python - Django:从 JSON 数据更改日期格式
- php - 如何在带有 if 语句的循环之后有一个 else 语句?
- javascript - Ionic firebase 一旦快照得到奇怪的错误
- java - 使用通配符验证参考文件的算法
- android - 从本地或远程加载的 Glide ModelLoader
- android - 如何确定导致应用程序停止运行的原因
- python - 有没有办法确保一个函数在另一个函数开始之前运行到最后?
- apache-spark - Pyspark 在 groupby 中创建字典
- python - pyomo 中的无效索引/值出错,有什么提示吗?
- javascript - 将子索引动态传递给选择器列表