【问题标题】:Exclude modules from ng-build in Angular CLI... not working在 Angular CLI 中从 ng-build 中排除模块...不工作
【发布时间】:2018-10-24 09:06:55
【问题描述】:

我有一个 Angular CLI 项目,其中包含两个应用程序。两个应用程序是相同的,但一个应用程序包含构建中的所有文件,另一个需要排除一个模块。

Angular CLI 版本:1.7.4 角度版本:5.2.10

我在 angular-cli.json 文件中添加了两个应用程序,每个应用程序都有一个单独的 ts-config 文件。

  "apps": [
    {
      "name": "app",   <---- first app
      "root": "src",
      "outDir": "/wwwroot",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",       <----first TS config
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.scss"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    },
    {
      "name": "admin",   <---- second app
      "root": "src",
      "outDir": "/wwwroot",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.admin.json", <----- second ts config
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.scss"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],

“管理员”应用将包含所有文件。 但是,app 数组中的第一个应用程序“app”将排除管理模块。

tsconfig.app.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts",
    "app/modules/admin/"    <----- note: I want this entire directory excluded from the build. 
  ]
}

我在 package.json 文件中设置了两个“ng-build”脚本。

package.json 构建脚本:

"scripts": {
    "build-app": "ng build -app app --prod",
    "build-admin": "ng build -app admin  --prod",
  },

为了测试这一点,我在管理模块中更改了一些文本和样式并在本地运行构建。我注意到应用程序的管理版本(应该构建所有文件)可以工作,但是应用程序的“应用程序”版本没有排除管理模块。

有没有人在上面的 sn-ps 中看到任何遗漏/忽略的内容? 有没有人遇到过类似的问题并找到了解决方案?

angular-cli repo 关闭了这个问题,但没有提供修复。 see issue here

【问题讨论】:

    标签: angular typescript angular-cli angular5 tsconfig


    【解决方案1】:

    如果有人正在阅读本文并寻找答案:

    TS 将构建它看到“import”语句的任何文件,无论它是否位于 tsconfig.json 的“exclude”属性中。

    我的 app.modules.ts 文件中有一个 import 语句,当我删除它时...它已成功从构建中排除。

    【讨论】:

    • 我猜,排除一直工作到 Angular 4。,..但是在迁移到 Angular 6 之后,即使我在排除中提到它,它也说要包含该模块..你有什么解决方案来排除ts 文件?
    • 如果你使用LazyLoading,它不会起作用,因为你不应该在app.modules.ts中导入lazyLoaded模块
    【解决方案2】:

    在我的应用程序中,我有一个错误,因为我有两个模块,包括几个模块/服务...一个专用于测试(使用 HttpClientTestingModule 而不是 HttpClientModule)。 由于它在 testing/index.ts 文件中,它在构建时自动加载,因此一切都失败了。 即使它仅包含在需要它的 *.spec.ts 文件中。 我试图用 src/tsconfig.app.json 排除它,它起作用了:

    {
      "extends": "../tsconfig.json",
      "compilerOptions": {
        "outDir": "../out-tsc/app",
        "baseUrl": "./",
        "module": "es2015",
        "types": []
      },
      "exclude": [
        "test.ts",
        "app/indalo-api/testing/*",
        "**/*.spec.ts"
      ]
    }
    

    然后我推断 Angular 6 及更高版本会自动加载所有 .ts 文件,除了它们在排除中,但如果未被排除的文件需要,它们将被加载。

    这是有道理的。

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 1970-01-01
      • 2019-01-14
      • 2020-05-03
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多