我一直在寻找一些 .babelrc
选项来从转译的代码中删除注释,但我没有找到任何运气。我试过这个:
{
"comments": false
}
还有
{
"options": {
"comments": false
}
}
两者都不起作用。我没有想法,而且我无法在任何地方找到任何像样的文档。
最佳答案
始终建议使用 .babelrc
:
{
comments: false
}
如果使用 babel-cli
,您可以使用 --no-comments
选项来实现相同的行为。
babel-cli
的最新版本包括tests that check for this behaviour to be implemented correctly .
编辑
看起来确实是 babel CLI 忽略了 .babelrc
中的注释的问题,解决方法是使用 --no-comments
选项。
在你的package.json
"build": "babel ./index.js --out-dir ./dist/index.js --no-comments"
了解babel-cli的所有选项
./node_modules/.bin/babel -h
原创
你从哪里运行 babel?咕噜咕噜?
检查.babelrc
文件是否在被转译文件的同一目录或父目录中
来自 babeljs.io :
Babel will look for a .babelrc in the current directory of the file being transpiled. If one does not exist, it will travel up the directory tree until it finds either a .babelrc, or a package.json with a "babel": {} hash within.
我有一个具有这种结构的项目:
- 距离
- index.js
- .babelrc
- index.js
- gulpfile.js
- 节点模块
- ...
gulpfile.js
中的相关任务
gulp.task('babel', () => {
return gulp.src('index.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(gulp.dest('./dist/'));
});
.babelrc
的内容
{
"comments": false
}
正在成功删除评论。
例如,还要检查您是否没有在 gulpfile 中将 comments
选项设置为 true。
关于javascript - 如何使用 babel-cli 从转译后的代码中删除注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38210802/