有没有办法保留许可证注释?

Alv*_*aro 5 javascript webpack babeljs babel-loader

babel-loader在我的webpack.config.js文件中使用,但我注意到它删除了表单的许可注释:

/*! whatever **/
Run Code Online (Sandbox Code Playgroud)

有没有办法保存它们?我注意到 babel 有一个comments选项,但我想这会保留任何评论,而不仅仅是许可评论。

const webpack = require('webpack');

module.exports = {
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.js'
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            scss: 'vue-style-loader!css-loader!sass-loader',
            js: 'babel-loader'
          }
        }
      },
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader',
        }
      }
    ]
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
        drop_console: false,
      }
    })
  ],
};
Run Code Online (Sandbox Code Playgroud)

我已经尝试过:

plugins: [
    new webpack.optimize.UglifyJsPlugin({
      output:{
        comments: true
      }
})
Run Code Online (Sandbox Code Playgroud)

以及comments: '/^!/'comments: /^!/。什么都行不通。

如果我plugins从 webpack 配置中删除整个选项,它只会保留评论。

我还尝试使用许可证注释,例如:

/** comments */

/*! comments */

/*! @license comments */

/*! @preserve comments */
Run Code Online (Sandbox Code Playgroud)

Ale*_*shy 3

这是一个bug,自 2015 年以来一直存在于 webpack/uglify 中,并且从未得到修复。

据说他们通过添加来修复它,但它仍然对某些人不起作用,所以一年后的 2019 年,另一个 PR被打开,据称可以修复它。extractCommentstrue

new UglifyJSPlugin({
  sourceMap: true,
  extractComments: true
})
Run Code Online (Sandbox Code Playgroud)

所以这是一个已知的错误,已经存在多年了。也许黑客确实存在,但这是一般情况。