【问题标题】:Is there a way to compile files in src using angular?有没有办法使用 Angular 在 src 中编译文件?
【发布时间】:2020-07-03 10:34:54
【问题描述】:

我有以下结构:

apps
|-api
  |-src
    |-_migrations
    |-_seeds
      |-dev
      |-test
    |-app
    |-assets
    |-environments
    |-main.ts
  |-tsconfig.app.json
  |-tsconfig.json

我正在使用 ng serve api 编译和服务 api 这是一个 NestJS 后端。这属于带有 Angular 前端的 monorepo。

_migrations_seeds 中的迁移和播种器在 TypeScript 中。要在服务后端时运行它们,我需要将它们编译为 JavaScript,并将编译后的文件定位到 dist/apps/api

我知道我可以在服务之前用tsc 编译文件,或者在angular.json 中指定一个webpack 配置文件,但我想保持它尽可能简单,并尝试使用@987654329 的一些配置@。

我尝试在tsconfig.app.json 中添加路径,该路径在angular.json 中使用。

// angular.json
"api": {
  "root": "apps/api",
  "sourceRoot": "apps/api/src",
  "projectType": "application",
  "prefix": "api",
  "schematics": {},
  "architect": {
    "build": {
      "builder": "@nrwl/node:build",
      "options": {
        "outputPath": "dist/apps/api",
        "main": "apps/api/src/main.ts",
        "tsConfig": "apps/api/tsconfig.app.json",
        "assets": ["apps/api/src/assets"]
      },
      "configurations": {
        "production": {
          "optimization": true,
          "extractLicenses": true,
          "inspect": false,
          "fileReplacements": [
            {
              "replace": "apps/api/src/environments/environment.ts",
              "with": "apps/api/src/environments/environment.prod.ts"
            }
          ]
        }
      }
    },
    "serve": {
      "builder": "@nrwl/node:execute",
      "options": {
        "buildTarget": "api:build"
      }
    }
  }
}
// tsconfig.app.json
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "types": ["node"]
  },
  "exclude": ["**/*.spec.ts"],
  "include": ["**/*.ts"], // or ["**/*.ts", "src/_migrations/*.ts", "src/_seeds/**/*.ts"] without "files"
  "files": ["src/_migrations/*.ts", "src/_seeds/**/*.ts"]
}

在运行ng serve aping build api 时,是否有办法编译_migrations_seeds 中的所有文件,只需切换一些配置或类似配置?
如果没有,实现这一点的最简单的 webpack 配置是什么?

【问题讨论】:

    标签: angular typescript nestjs


    【解决方案1】:

    参考:https://indepth.dev/configuring-typescript-compiler/

    1. TS 编译递归搜索根目录下的所有文件 和子目录并编译它们。

      假设您正在引用它们,它们应该编译

    2. TS 编译器还将编译在文件列表中的任何文件中引用的文件。例如,如果 main.ts 从 a.ts 导入导出,该文件也会被编译。

    files - 告诉编译器只编译 files[] 中的文件,而忽略其他所有内容。

    {
      "compilerOptions": { ... },
      "files": [
        "main.ts",
        "router/b.ts"
      ]
    }
    

    如果没有更多文件,我们可以使用 include 选项来指定目录,而不是手动列出每个文件,使用类似 glob 的文件模式 这只会编译include[]中指定目录中的文件 因此,在您的情况下,这只会编译迁移和种子中的文件。

    {
          "extends": "./tsconfig.json",
          "compilerOptions": {
            "outDir": "../../dist/out-tsc",
            "types": ["node"]
          },
          "exclude": ["**/*.spec.ts"],
          "include": ["src/_migrations/*.ts", "src/_seeds/**/*.ts"] 
      }
    

    【讨论】:

    • 感谢您的回答。我尝试使用tsc 进行编译,它确实编译了所有内容,但这不是我想要的。我想使用带有ng serveng build 的Angular 构建器来编译_migrations_seeds 中的文件。
    • 你是否在输入 ng-build 命令后检查它们是否正在编译?
    • 是的,这就是所有输出:```正在启动类型检查服务...使用 2 个具有 2048MB 内存限制的工作人员哈希:dfa3f3187e4fa63feb6d 构建于:03/22/2020 23:53:28 入口点main = main.js main.js.map chunk {main} main.js, main.js.map (main) 13.9 KiB [entry] [rendered] ``` 只有main.ts 被编译。
    • 这可能对你有帮助。stackoverflow.com/a/21425873/8029211
    • 谢谢。如果我找不到更好的解决方案,我可能会使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多