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

NX工作区Angular库主包过大,拆分入口点遇EntryPoint不存在错误

Angular组件库打包问题排查与求助

问题背景

我在NX工作区构建了一个Angular组件库,包含约50个带独立模块的组件。此前曾提问过类似问题但未解决,如今积累了更多排查信息。

问题初始表现

应用中所有页面均为懒加载,每个页面展示库中的一个组件。但仅在AppComponent中使用BsNavbarModule并在AppModule中导入它,库内所有其他组件的JavaScript代码块就被打包进了主包中。(截图显示主包中包含多个仅应在懒加载页面出现的组件代码块,当前主包总大小为850 kB)

排查过程

SCSS排查

此前有人认为问题源于组件中的SCSS写法,例如badge.component.scss中的代码:

:host ::ng-deep {
    // Configuration
    @import "~bootstrap/scss/functions";
    @import "~bootstrap/scss/variables";
    @import "~bootstrap/scss/mixins";

    // Layout & components
    @import "~bootstrap/scss/badge";
}

但全局注释掉:host ::ng-deep后,主包中的多余代码块并未消失,因此这并非问题根源。

新增组件影响

主包原本大小为650 kB,新增组件后主包骤增200 kB,达到860 kB。

移除库直接集成组件

将所有组件移至应用内并移除库后,主包大小降至400 kB。

Angular工作区测试

测试Angular工作区的库间引用功能,结果仍相同:仅使用库中一个组件,主包就会包含整个库的代码。

移除所有组件SCSS

注释掉库中所有组件的SCSS代码,主包大小仍超过800 kB,无任何改善。

尝试优化方案及报错

此时已明确问题与SCSS无关,而是组件在库中导致的:使用库中一个组件就会将整个库打包进主包。参考相关解决方案后,我为每个组件文件夹添加了ng-package.json:

src/lib/badge/ng-package.json:

{
    "$schema": "../../../../../node_modules/ng-packagr/ng-package.schema.json",
    "dest": "../../../../../dist/libs/example-ng-bootstrap",
    "lib": {
      "entryFile": "index.ts"
    }
}

src/lib/badge/index.ts:

export * from './badge.component';
export * from './badge.module';

并重构代码,使用如下方式导入组件:

import { BsBadgeModule } from '@example/ng-bootstrap/badge';

但工作区无法编译,精简代码后仍报错:

npm run build -- --skip-nx-cache --configuration production --source-map

> mintplayer-ng-bootstrap@0.0.0 build
> nx build "--skip-nx-cache" "--configuration" "production" "--source-map"


   ×    1/4 dependent project tasks failed (see below)
   √    3/4 dependent project tasks succeeded [0 read from cache]

 —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————


> nx run mintplayer-ng-bootstrap:build:production

Building Angular Package

 >  NX   Entry point @mintplayer/ng-bootstrap/core which is required by @mintplayer/ng-bootstrap/src/lib/badge doesn't exists.

   Pass --verbose to see the stacktrace.


 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 

 >  NX   Ran target build for project ng-bootstrap-demo and 4 task(s) it depends on (7s)

         With additional flags:
           --source-map=true

    ×    1/4 failed

 >  NX   Entry point @mintplayer/ng-bootstrap/core which is required by @mintplayer/ng-bootstrap/src/lib/badge doesn't exists.

   Pass --verbose to see the stacktrace.


 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 

 >  NX   Ran target build for project ng-bootstrap-demo and 4 task(s) it depends on (6s)

         With additional flags:
           --source-map=true

    ×    1/4 failed
    √    3/4 succeeded [0 read from cache]

   View structured, searchable error logs at https://nx.app/runs/IAaoSSkobF

请问我仍存在哪些问题?ng-packagr是否无法确定入口点的构建顺序?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 05:25:51