Webpack TerserPlugin -排除节点模块

nhn9ugyo  于 2023-11-19  发布在  Webpack
关注(0)|答案(2)|浏览(195)

使用自定义的Angular Builder,我试图从缩小/优化中排除整个模块。
Webpack .md文件显示:

排除

类型:字符串|RegExp|数组默认值:未定义
要排除的文件
我可以使用此设置来排除整个目录(即节点模块)吗?
现行规范

  1. export default class CustomizeTerserBrowserBuilder extends BrowserBuilder {
  2. public buildWebpackConfig(root: any, projectRoot: any, host: any, options: any): any {
  3. const webpackConfig = super.buildWebpackConfig(root, projectRoot, host, options);
  4. if (
  5. webpackConfig.optimization &&
  6. webpackConfig.optimization.minimizer &&
  7. Array.isArray(webpackConfig.optimization.minimizer)
  8. ) {
  9. const terserPlugin = (webpackConfig.optimization.minimizer as any[]).find(
  10. minimizer => minimizer instanceof TerserPlugin
  11. );
  12. if (terserPlugin) {
  13. terserPlugin.options.exclude = [/node_modules/];
  14. }
  15. }
  16. return webpackConfig;
  17. }
  18. }

字符串

ql3eal8s

ql3eal8s1#

也许你在找这个

  1. module.exports = {
  2. optimization: {
  3. minimizer: [
  4. new TerserPlugin({
  5. exclude: /node_modules/,
  6. }),
  7. ],
  8. },
  9. };

字符串

nwo49xxi

nwo49xxi2#

在Webpack5的生产环境中也遇到了同样的问题,最终出现了类似以下的问题,这将排除node_modulesdist下的特定文件,这将导致Terser的“积极”缩小尝试被破坏。

  1. optimization: {
  2. minimize: NODE_ENV !== 'development', //only minimize on production
  3. //JS minimizer when "minimize === true",
  4. minimizer: [
  5. new TerserPlugin({
  6. test: /\.js$/,
  7. exclude: /(?:node_modules|dist)\/(update-notifier|numeral|jackspeak|bulma-extensions)\//, //whatever causes u a problem, either as RegExp or as a string[]
  8. terserOptions: {
  9. parse: {
  10. bare_returns: true //allow code with returns outside of functions, solved a ton of issues.
  11. },
  12. },
  13. })],
  14. // rest of config.
  15. // ...
  16. }

字符串
老实说,我不确定排除/node_modules下的所有文件是否是一个好主意。

展开查看全部

相关问题