我想在使用webpack构建时自动删除注释

时间:2017-05-28 22:59:03

标签: webpack

我想在使用webpack构建时自动删除评论。

·我想在//

之后删除评论

·我可以通过设置一些选项来删除评论吗?

·我该怎么办?

▼main.js

// src/main.js I want to delete this part
var m = require("mithril")

var UserList = require("./views/UserList")

m.mount(document.body, UserList)
//I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.I want to delete this part.

▼的package.json

 "scripts": {
     "build": "webpack src/index.js bin/main.js -p"
 }
$ npm start

1 个答案:

答案 0 :(得分:4)

要自定义Webpack默认配置,您需要使用所需选项创建特定文件。

要删除评论,您必须在UglifyJs插件中传递comments: false

module.exports = {
  // ...

  plugins: [
    new webpack.optimize.UglifyJsPlugin({
      comments: false
    }),
  ]
}

然后,您可以将配置写入webpack.prod.js并使用生产环境webpack --env=prod运行webpack。

请阅读official documentation了解详情。