首页 > 解决方案 > 在不使用优化的情况下删除 Angular 构建中的供应商评论

问题描述

我有一个 Angular 库项目,我在我的主要 Angular 项目中使用(该库托管在我的私人 npm 注册表中)。当我在我的主项目中使用我的库时,我必须能够访问他的评论(函数评论、类评论、属性评论等......)。为此,我removeComments在我的库 tsconfig.json 文件中将该属性设置为 false 以保留关于构建的注释。

但是现在,我想在没有任何评论的情况下构建我的主要 Angular 项目(因为一些评论包含私人数据)。在我的 angular.json 文件中,我设置vendorChunk为 false 以将所有源保存在一个文件中(这样我只有 main.js 文件,我没有 vendor.js 文件)。

我不能使用 Angular 构建优化(optimizationangular.json 中的属性),因为我的代码中有很多反射,我必须保留类名(当我尝试将 Terser 选项 keep_classnames 设置为 true 并将 Angular 构建优化设置为 true 时,我仍然有一个运行时错误:The key hYe.e is already present in the container (existing value: class e extends hYe {constructor ()...,这就是为什么我不能使用优化)

如何删除所有评论(我不希望有库评论或主要项目评论)?

我尝试以这种方式在我的 angular.json 中使用自定义 webpack 构建器:

角.json:

"builder": "@angular-builders/custom-webpack:browser",
  "options": {
    "customWebpackConfig": {
      "path": "./extra-webpack.config.js",
      "mergeStrategies": {
        "externals": "replace"
      }
    },

额外的 webpack.config.js:

const TerserPlugin = require('terser-webpack-plugin');

module.exports = config => {

  config.optimization.minimizer.push(new TerserPlugin({
    terserOptions: {
      output: {
        comments: false,
      },
    },
  }));
  return config;
  
};

主要的项目评论如预期的那样消失了,但图书馆的评论仍然存在。

标签: angularwebpackbuildcommentsterser

解决方案


推荐阅读