webpack/terser:如何将包装排除在缩影之外,但仍在包装结果中包含该软件包?

发布于 2025-02-04 14:16:01 字数 1956 浏览 0 评论 0原文

我找到了许多用于将特定模块排除在Minific中的解决方案,但是到目前为止,我所看到的所有解决方案不仅跳过了这些包装的缩影,它们导致这些包装完全省略了 WebPack的输出,要求您通过其他一些方式提供省略的代码。

我要做的是继续将所有需要的代码打包到一个单个输出文件中,但只需将其输出的部分提供未限制的部分即可。有可能吗?

在这种特殊情况下,我要这样做的原因是mySQL软件包在缩小后正在失败。目前,我禁用了所有的缩影,但是我宁愿以这种方式解决这个问题。

const webpack = require('webpack');
const LicensePlugin = require('webpack-license-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const path = require('path');
const mode = process.env.NODE_ENV || 'production';

// noinspection JSUnresolvedFunction
module.exports = {
  mode,
  entry: './app/app.ts',
  target: 'node',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.js'
  },
  node: {
    __dirname: false,
    __filename: false,
    global: true
  },
  resolve: {
    extensions: ['.ts', '.js'],
    mainFields: ['fesm2015', 'module', 'main']
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          'ts-loader',
        ]
      }
    ]
  },
  optimization: {
    // TODO: Minification breaks mysql. How to exclude mysql from minification, but include in output?
    minimize: false, // mode === 'production',
    minimizer: [new TerserPlugin({
      exclude: /node_modules\/mysql/, // <-- This doesn't work
      terserOptions: {
        output: { max_line_len: 511 },
      }
    })],
  },
  devtool: 'source-map',
  ignoreWarnings: [{ message: /require function is used in a way|the request of a dependency is an expression/ }],
  plugins: [
    new webpack.BannerPlugin({ banner: '#!/usr/bin/env node', raw: true }),
    new LicensePlugin({
      outputFilename: '3rdpartylicenses.txt',
      excludedPackageTest: name => /^(asynclist|emitter)/.test(name)
    })
  ]
};

Update mySQL和Minification的特定问题似乎是变量和/或函数名称的混音。如果我将mangle选项设置为false,我可以成功地缩小所有代码,包括mysql。我尚未弄清楚哪些特定名称不会在不引起问题的情况下被弄脏。

I've found a number of solutions for excluding particular modules from minification, but all of the solutions I've seen so far not only skip minification of those packages, they cause those packages to be completely omitted from webpack's output, requiring you to provide the omitted code by some other means.

What I want to do is continue to package all needed code into one single output file, but simply have sections of that output non-minified. Is that possible?

The reason that I want to do this in this particular case is that the mysql package is failing after minification. For the moment I've disabled all minification, but I'd rather not solve this problem that way.

const webpack = require('webpack');
const LicensePlugin = require('webpack-license-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const path = require('path');
const mode = process.env.NODE_ENV || 'production';

// noinspection JSUnresolvedFunction
module.exports = {
  mode,
  entry: './app/app.ts',
  target: 'node',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.js'
  },
  node: {
    __dirname: false,
    __filename: false,
    global: true
  },
  resolve: {
    extensions: ['.ts', '.js'],
    mainFields: ['fesm2015', 'module', 'main']
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          'ts-loader',
        ]
      }
    ]
  },
  optimization: {
    // TODO: Minification breaks mysql. How to exclude mysql from minification, but include in output?
    minimize: false, // mode === 'production',
    minimizer: [new TerserPlugin({
      exclude: /node_modules\/mysql/, // <-- This doesn't work
      terserOptions: {
        output: { max_line_len: 511 },
      }
    })],
  },
  devtool: 'source-map',
  ignoreWarnings: [{ message: /require function is used in a way|the request of a dependency is an expression/ }],
  plugins: [
    new webpack.BannerPlugin({ banner: '#!/usr/bin/env node', raw: true }),
    new LicensePlugin({
      outputFilename: '3rdpartylicenses.txt',
      excludedPackageTest: name => /^(asynclist|emitter)/.test(name)
    })
  ]
};

Update: The particular issue with mysql and minification seems to be mangling of variable and/or function names. If I set the mangle option to false, I can minify all of the code, including mysql, successfully. I have yet to figure out which specific names can't be mangled without causing problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文