javascript - 如何使用 babel-cli 从转译后的代码中删除注释

标签 javascript babeljs

我一直在寻找一些 .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/

相关文章:

javascript - 在 Promise 中返回 Promise 的值

javascript - 更新数组中的嵌入文档 - Mongodb + Node Driver

javascript - Node 服务器问题

javascript - 使用 Elvis (?) 运算符后,如何使 VSCode 为 .JS 文件保持正确的语法突出显示?

javascript - knockout : How to bindings to html bind recursively

javascript - 使用 javascript 调整 html Canvas 大小

javascript - ES2017 Async/await 函数 - 它们只适用于 promises 吗?

javascript - Gulp/Webpack 不转译 Vue 文件

webpack - babel-polyfill 和 webpack 错误

javascript - NPM 运行生成错误 : "Trace: The node type RestProperty has been renamed to RestElement" with new Babel 7. 0.0