我具有以下结构:
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 api
和ng build api
中的所有文件?
如果没有,那是实现这一目标的最简单的webpack配置?
答案 0 :(得分:1)
参考:https://indepth.dev/configuring-typescript-compiler/
TS递归搜索根目录中的所有文件 和子目录并进行编译。
假设您正在引用它们,则它们应该编译
TS编译器还将编译文件列表中任何文件内引用的文件。例如,如果main.ts从a.ts导入导出,则该文件也会被编译。
文件-告诉编译器仅在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"]
}