有没有一种方法可以使用angular在src中编译文件?

问题描述 投票:0回答:1

我具有以下结构:

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配置文件,但我想使其尽可能简单,只需尝试对ng使用一些配置。

我尝试在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"]
}

[运行_migrations_seeds时仅切换某些配置或类似方法,是否有一种方法可以编译ng serve aping build api中的所有文件?如果没有,那么实现这一目标的最简单的webpack配置是什么?

angular typescript nestjs
1个回答
0
投票

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

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

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

如果没有更多文件,我们可以使用include选项使用类似glob的文件模式指定目录,而不是手动列出每个文件

因此,在您的情况下,这应该起作用

{
      "extends": "./tsconfig.json",
      "compilerOptions": {
        "outDir": "../../dist/out-tsc",
        "types": ["node"]
      },
      "exclude": ["**/*.spec.ts"],
      "include": ["src/_migrations/*.ts", "src/_seeds/**/*.ts"] 
  }
© www.soinside.com 2019 - 2024. All rights reserved.