【问题标题】:Unable to strip comments in webpack bundle js file无法删除 webpack 捆绑 js 文件中的注释
【发布时间】:2023-03-31 01:06:01
【问题描述】:

我一直在尝试在 webpack 捆绑的 js 文件中剥离 cmets。 我已经尝试了几种方法,但它仍然无法正常工作,我得到了类似的 cmets

"/**\n * Copyright 2013-present, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under the BSD-style license found in the\ ...

为此,捆绑的文件变得越来越大。目前很大,大小为 1.6mb。 这个我试过了

new webpack.optimize.UglifyJsPlugin({
        sourceMap: false,
        compress: {
            sequences: true,
            dead_code: true,
            conditionals: true,
            booleans: true,
            unused: true,
            if_return: true,
            join_vars: true,
            drop_console: true
        },
        mangle: {
            except: ['$super', '$', 'exports', 'require']
        },
        output: {
            comments: false
        }
    })

还有这个

new webpack.optimize.UglifyJsPlugin({
        compress: { warnings: false },
        sourceMap: false,
        output: false
    })

同时将环境设置为生产环境

set NODE_ENV=production

我无法理解我错在哪里。 请帮忙。 提前致谢。

【问题讨论】:

标签: javascript reactjs module webpack


【解决方案1】:

UglifyJsPlugin 不要删除 @licence cmets 即使您出于法律原因设置了 comments: false。你可以阅读它on webpack GitHub issue

如果您想删除此类 cmets(风险自负),您应该搜索其他加载程序,例如 webpack-comment-remover-loaderstripcomment-loader

【讨论】:

    【解决方案2】:

    这是你需要的:

    new UglifyJsPlugin({
        comments: false,
    }),
    

    来自here


    这是来自 Webpack 的那句话,@Everettss 是对的。

    File: /webpack/lib/optimize/UglifyJsPlugin.js
    097:                        let output = {};
    098:                        output.comments = Object.prototype.hasOwnProperty.call(options, "comments") ? options.comments : /^\**!|@preserve|@license/;
    099:                        output.beautify = options.beautify;
    100:                        for(let k in options.output) {
    101:                            output[k] = options.output[k];
    102:                        }
    

    您可以检查确认 Sokra stated 的正则表达式。

    我不确定 UglifyJsPlugin,但通常如果您在其他地方提供法律声明,您应该删除所有 cmets。

    如果您的律师确认这没问题,您可以尝试调整 /*!,使正则表达式失败,cmets 将不再存在。

    【讨论】:

      【解决方案3】:

      对于 webpack 4,这对我有用:

      // webpack.config.js
      
      const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
      
      module.exports = {
        // other config properties...
      
        optimization: {
          minimizer: [
            new UglifyJsPlugin({
              uglifyOptions: {
                output: {
                  comments: false
                }
              }
            })
          ]
        }
      };
      

      您可以在official docs找到更多信息。

      【讨论】:

        【解决方案4】:

        如果您也想删除 @words,并且无法在线找到解决方案,我建议您执行查找和替换。
        编写一个正则表达式来查找并用空白字符替换它。

        【讨论】:

          猜你喜欢
          • 2017-02-13
          • 2020-07-17
          • 2019-01-02
          • 2019-08-23
          • 1970-01-01
          • 2017-10-15
          • 2017-09-08
          • 1970-01-01
          相关资源
          最近更新 更多