从Angular CLI中的ng-build中排除模块...无法正常工作

时间:2018-05-14 16:16:22

标签: angular typescript angular-cli angular5 tsconfig

我有一个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"
      }
    }
  ],

“admin”应用程序将包含所有文件。 但是,应用程序数组中的第一个应用程序“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",
  },

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

有没有人在上面的片段中看到任何错过/忽略的内容? 有没有人有类似的问题,并找到了解决方案?

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

2 个答案:

答案 0 :(得分:3)

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

TS将构建它看到的任何文件&#39; import&#39;声明,无论它是否排除在&#39;排除&#39; tsconfig.json中的属性。

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

答案 1 :(得分:0)

在我的应用程序上,我出错了,因为我有两个模块,其中包括几个模块/服务...一个专用于测试(使用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文件,除非它们位于排除中,但是如果不排除的文件需要它们,则将加载它们。

这很有道理。