Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法保存许可证评论?_Javascript_Webpack_Babeljs_Babel Loader - Fatal编程技术网

Javascript 有没有办法保存许可证评论?

Javascript 有没有办法保存许可证评论?,javascript,webpack,babeljs,babel-loader,Javascript,Webpack,Babeljs,Babel Loader,我在我的webpack.config.js文件中使用了babel loader,但我注意到它删除了表单的许可评论: /*! whatever **/ 有办法保存它们吗? 我注意到babel有一个注释选项,但我想这将保留任何注释,而不仅仅是许可证注释 const webpack = require('webpack'); module.exports = { resolve: { alias: { 'vue$': 'vue/dist/vue.js' } },

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

/*! whatever **/
有办法保存它们吗? 我注意到babel有一个
注释
选项,但我想这将保留任何注释,而不仅仅是许可证注释

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,
      }
    })
  ],
};
我已经试过了:

plugins: [
    new webpack.optimize.UglifyJsPlugin({
      output:{
        comments: true
      }
})
以及
注释:'/^!/'
注释:/^/。
什么都不管用

只有当我从webpack配置中删除整个
插件
选项时,它才会保留注释

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

/** comments */

/*! comments */

/*! @license comments */

/*! @preserve comments */

尝试添加一次
输出
选项

plugins : [
  new webpack.optimize.UglifyJsPlugin({
    compress: {
      warnings: false,
      drop_console: false
    },
    output: {
      comments: '/^!/'
    }
  })
]
我不确定它是否会起作用,因为我从未使用过它。 有关更多详细信息,请参阅

请让我知道它是否有效。

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

据说他们是通过将
extractComments
添加到
true
来修复它的,但它仍然无法工作,因此一年后的2019年已经开始修复它

new UglifyJSPlugin({
  sourceMap: true,
  extractComments: true
})
因此,这是一个已经存在多年的已知bug。也许黑客确实存在,但这是一般情况。

工作黑客:

const WrapperPlugin = require('wrapper-webpack-plugin');
let licenseComments = [];

module.exports = {
    ...
    optimization: {
        minimize: true,
        minimizer: [
            new UglifyJsPlugin({}),
            new WrapperPlugin({
                header: function () {
                    var unique = licenseComments.filter((v, i, a) => a.indexOf(v) === i); 
                    return unique.map(x => '/*' + x + '*/').join('\n');
                }
            })
        ],
    },
    module: {
        {
            test: /\.js$/,
            use: {
                loader: 'babel-loader',
                options: {
                    shouldPrintComment: (val) => {
                        if (/license/.test(val)) {
                            licenseComments.push(val);
                        }
                        return false;
                    },
                }
            }
        }
    ...

在TerserPlugin中添加这两个选项同样有效,并创建一个单独的filename.js.LICENSE文件,其中只包含许可证注释。酷