我正在开发一个 Nest.js 应用程序,我想第一次使用 SWC。 我按照 Nest.js 文档说明进行操作 (+),但出现错误。
我在项目中使用 Prisma,并在根目录中创建了一个文件夹
prisma
,与 src
具有相同级别。ROOT DIRECTORY STRUCTURE:
.
├── prisma
│ ├── migrations
│ ├── prisma.service.ts
│ └── schema.prisma
├── src
│ ├── common
│ ├── config
│ ├── modules
│ ├── types
│ ├── app.module.ts
│ ├── main.ts
│ ├── metadata.ts
│ └── schema.gpl
├── bun.lockb
├── nest-cli.json
├── package.json
├── README.md
├── tsconfig.build.json
└── tsconfig.json
我希望
dist
的文件夹结构与 SWC 编译后的根目录相同(即 prisma
和 src
都已编译且处于同一级别),但我得到的是:
DIST DIRECTORY STRUCTURE:
.
├── common
│ └── decorators
├── config
│ ├── environment.config.js
│ ├── environment.config.js.map
│ ├── index.js
│ └── index.js.map
├── modules
│ ├── auth
│ ├── comments
│ ├── posts
│ └── users
├── app.module.js
├── app.module.js.map
├── main.js
├── main.js.map
├── metadata.js
├── metadata.js.map
└── tsconfig.build.tsbuildinfo
仅编译了
src
,并且是我在 dist
中所能找到的全部内容。
这是
tsconfig.json
:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
这是
.swcrc
:
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": true,
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"baseUrl": "./"
},
"minify": false
}
这是我用来运行项目的脚本:
scripts: {
"start:swc": "dotenv -e .dev.env -- nest start -b swc -w --type-check",
}
由于
prisma
目录未编译,运行时无法从其中导入任何内容 src
,因此出现错误:
当我在没有 SWC 的情况下运行项目时,一切正常。
我已经尝试过这个answer,它修复了导入的起始路径(
prisma/
到../../../prisma/
)。
为什么 SWC 不编译
prisma
文件夹?
运行时和工具:
1.0.7
10.0.0
@swc/cli
- 0.1.63
@swc/core
- 1.3.96
为了让 SWC 编译 prisma 文件夹,您需要在嵌套构建后显式编译此类文件夹:
nest build && swc prisma --out-dir dist