You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular编译器同时存在于main和polyfills chunk中,如何修复?

解决Angular 18中Compiler被重复打包到main和polyfills chunk的问题

问题背景

项目从Angular 5历经多次升级,最近从Angular 13升级到18,出现Angular compiler同时被打包进main和polyfills chunk的情况,导致包体积冗余。

排查方向

  • 检查AOT编译配置:当前angular.json中build.options和production配置均设置"aot": false,JIT模式下Angular compiler会被强制打包,且可能因加载逻辑重复出现在多个chunk中。
  • 分析chunk依赖关系:配置中已开启statsJson: true,可通过npx webpack-bundle-analyzer dist/hagai/stats.json生成可视化分析报告,定位compiler被引入到两个chunk的具体来源。
  • 验证依赖版本一致性:执行npm ls @angular/compiler检查是否存在多版本的compiler依赖,旧版本残留可能导致重复打包。
  • 检查代码中的JIT相关逻辑:查看main.ts、polyfills.ts及业务代码,是否存在手动引入@angular/compiler或使用JIT专属API(如Compiler.compileModuleAndAllComponentsAsync)的情况,这类代码会触发compiler打包。

修复方案

  1. 开启AOT编译
    将angular.json中build.options.aot和configurations.production.aot均设置为true:

    "aot": true
    

    Angular 18默认推荐AOT编译,开启后compiler不会被打包到最终产物,从根源解决重复打包问题。

  2. 清理重复依赖
    若npm ls @angular/compiler显示多版本依赖,执行以下步骤:

    • 删除node_modules和package-lock.json
    • 运行npm install重新安装依赖,或直接执行npm dedupe自动合并重复依赖
  3. 移除不必要的Compiler引入
    检查polyfills.ts及所有业务代码,删除手动引入@angular/compiler的语句;若存在动态编译组件的逻辑,改为使用Angular的AOT兼容方案(如预编译组件)。

  4. 同步CLI配置到Angular 18规范
    对比全新创建的Angular 18项目的angular.json,清理过时配置项(如旧版本遗留的无用参数),确保当前项目配置与新版本CLI兼容。

用户提供的angular.json配置

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "cli": {
    "analytics": "ba4eae7a-6f3a-4bca-af89-b7b2859cf746"
  },
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "hug-moms": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss",
          "skipTests": true
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist/hagai",
            "index": "src/index_dev.html",
            "browser": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "externalDependencies": [
              "jsonwebtoken",
              "password-hash",
              "nodemailer",
              "fs",
              "aws-sdk"
            ],
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "aot": false,
            "extractLicenses": false,
            "sourceMap": true,
            "statsJson": true,
            "optimization": false,
            "namedChunks": true
          },
          "configurations": {
            "production": {
              "index": {
                "input": "src/index.html",
                "output": "index.html"
              },
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "aot": false,
              "outputHashing": "all",
              "sourceMap": true,
              "statsJson": true,
              "namedChunks": true,
              "extractLicenses": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "10mb"
                }
              ]
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true,
              "statsJson": true
            }
          },
          "defaultConfiguration": ""
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "proxyConfig": "proxy.conf.json"
          },
          "configurations": {
            "production": {
              "buildTarget": "hug-moms:build:production"
            },
            "development": {
              "buildTarget": "hug-moms:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n"
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  }
}

内容的提问来源于stack exchange,提问作者Noam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 04:34:59