Skip to content

Commit bffaa89

Browse files
authored
Merge pull request #393 from toimc/patch-2
Update recipes.md
2 parents aac9777 + 8e8e2fa commit bffaa89

File tree

1 file changed

+350
-1
lines changed

1 file changed

+350
-1
lines changed

10/recipes.md

+350-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,356 @@ export class UsersResolver {
267267

268268
## SWC (快速编译器)
269269

270-
(待翻译)
270+
## SWC
271+
272+
[SWC](https://swc.rs/)(Speedy Web Compiler)是一个可扩展的基于Rust的平台,可用于编译和打包。使用Nest CLI与SWC结合是加快开发流程的一种简单而有效的方法。
273+
274+
> SWC编译器的速度大约是默认的TypeScript编译器的**20倍**
275+
276+
### 安装
277+
278+
要开始使用,请先安装几个软件包:
279+
280+
```
281+
$ npm i --save-dev @swc/cli @swc/core
282+
```
283+
284+
### 开始
285+
286+
安装过程完成后,您可以将 `swc` 构建器与 Nest CLI 一起使用,如下所示:
287+
288+
```
289+
$ nest start -b swc
290+
# OR nest start --builder swc
291+
```
292+
293+
> 如果您的存储库是 [monorepo](#mongorepo),请查看此部分。
294+
295+
除了传递标志之外, `-b` 您还可以 `"swc"``nest-cli.json` 文件中将 `compilerOptions.builder` 属性设置为,如下所示:
296+
297+
```json
298+
{
299+
"compilerOptions": {
300+
"builder": "swc"
301+
}
302+
}
303+
```
304+
305+
要自定义构建器的行为,可以传递包含两个属性 `type``"swc"` ) 和 `options` 的对象,如下所示:
306+
307+
```json
308+
"compilerOptions": {
309+
"builder": {
310+
"type": "swc",
311+
"options": {
312+
"swcrcPath": "infrastructure/.swcrc",
313+
}
314+
}
315+
}
316+
```
317+
318+
若要在监视模式下运行应用程序,请使用以下命令:
319+
320+
```bash
321+
$ nest start -b swc -w
322+
# OR nest start --builder swc --watch
323+
```
324+
325+
### 类型检查
326+
327+
SWC 本身不执行任何类型检查(与默认的 TypeScript 编译器相反),因此要打开它,您需要使用以下 `--type-check` 标志:
328+
329+
```bash
330+
$ nest start -b swc --type-check
331+
```
332+
333+
此命令将指示 Nest CLI 与 SWC 一起在 `noEmit` 模式下运行 `tsc` ,SWC 将异步执行类型检查。同样,除了传递标志之外 `--type-check` ,您还可以 `true``nest-cli.json` 文件中将 `compilerOptions.typeCheck` 属性设置为,如下所示:
334+
335+
```json
336+
{
337+
"compilerOptions": {
338+
"builder": "swc",
339+
"typeCheck": true
340+
}
341+
}
342+
```
343+
344+
### CLI 插件 (SWC)
345+
346+
`--type-check` 标志将自动执行 NestJS CLI 插件并生成一个序列化的元数据文件,然后应用程序可以在运行时加载该文件。
347+
348+
### SWC 配置
349+
350+
SWC 构建器经过预配置,可满足 NestJS 应用程序的要求。但是,您可以通过在根目录中创建 `.swcrc` 文件并根据需要调整选项来自定义配置。
351+
352+
```json
353+
{
354+
"$schema": "https://json.schemastore.org/swcrc",
355+
"sourceMaps": true,
356+
"jsc": {
357+
"parser": {
358+
"syntax": "typescript",
359+
"decorators": true,
360+
"dynamicImport": true
361+
},
362+
"baseUrl": "./"
363+
},
364+
"minify": false
365+
}
366+
```
367+
368+
### Monorepo
369+
370+
如果您的存储库是 monorepo,那么 `swc` 您必须配置 `webpack` 为使用 `swc-loader` .
371+
372+
首先,让我们安装所需的软件包:
373+
374+
```bash
375+
$ npm i --save-dev swc-loader
376+
```
377+
378+
安装完成后,在应用程序的根目录中创建一个包含以下内容 `webpack.config.js` 的文件:
379+
380+
```js
381+
const swcDefaultConfig = require('@nestjs/cli/lib/compiler/defaults/swc-defaults').swcDefaultsFactory().swcOptions;
382+
383+
module.exports = {
384+
module: {
385+
rules: [
386+
{
387+
test: /\.ts$/,
388+
exclude: /node_modules/,
389+
use: {
390+
loader: 'swc-loader',
391+
options: swcDefaultConfig,
392+
},
393+
},
394+
],
395+
},
396+
};
397+
```
398+
399+
### Monorepo 和 CLI 插件
400+
401+
现在,如果您使用 CLI 插件, `swc-loader` 则不会自动加载它们。相反,您必须创建一个单独的文件来手动加载它们。为此,请在 `main.ts` 文件附近声明一个包含以下内容 `generate-metadata.ts` 的文件:
402+
403+
```ts
404+
import { PluginMetadataGenerator } from '@nestjs/cli/lib/compiler/plugins';
405+
import { ReadonlyVisitor } from '@nestjs/swagger/dist/plugin';
406+
407+
const generator = new PluginMetadataGenerator();
408+
generator.generate({
409+
visitors: [new ReadonlyVisitor({ introspectComments: true, pathToSource: __dirname })],
410+
outputDir: __dirname,
411+
watch: true,
412+
tsconfigPath: 'apps/<name>/tsconfig.app.json',
413+
});
414+
```
415+
416+
> **提示**
417+
> 在这个例子中,我们使用了 `@nestjs/swagger` 插件,但你可以使用你选择的任何插件。
418+
419+
`generate()` 方法接受以下选项:
420+
421+
| `watch` | 是否观察项目的变化。 |
422+
| ------------------ | ------------------------------------------------------------ |
423+
| `tsconfigPath` | `tsconfig.json` 文件的路径。相对于当前工作目录 ( `process.cwd()` )。 |
424+
| `outputDir` | 将保存元数据文件的目录的路径。 |
425+
| `visitors` | 将用于生成元数据的访问者数组。 |
426+
| `filename` | 元数据文件的名称。默认值为 `metadata.ts`|
427+
| `printDiagnostics` | 是否将诊断程序打印到控制台。默认值为 `true`|
428+
429+
最后,您可以使用以下命令在单独的终端窗口中运行脚本 `generate-metadata`
430+
431+
```bash
432+
$ npx ts-node src/generate-metadata.ts
433+
# OR npx ts-node apps/{YOUR_APP}/src/generate-metadata.ts
434+
```
435+
436+
### 常见陷阱
437+
438+
如果您在应用程序中使用 TypeORM/MikroORM 或任何其他 ORM,您可能会偶然发现循环导入问题。SWC 不能很好地处理循环导入,因此应使用以下解决方法:
439+
440+
```typescript
441+
@Entity()
442+
export class User {
443+
@OneToOne(() => Profile, (profile) => profile.user)
444+
profile: Relation<Profile>; // <--- see "Relation<>" type here instead of just "Profile"
445+
}
446+
```
447+
448+
> **提示**
449+
> `Relation` type 将从 `typeorm` 包中导出。
450+
451+
这样做可以防止属性类型保存在属性元数据的转译代码中,从而防止循环依赖关系问题。
452+
453+
如果您的 ORM 没有提供类似的解决方法,您可以自行定义包装器类型:
454+
455+
```typescript
456+
/**
457+
* Wrapper type used to circumvent ESM modules circular dependency issue
458+
* caused by reflection metadata saving the type of the property.
459+
*/
460+
export type WrapperType<T> = T; // WrapperType === Relation
461+
```
462+
463+
对于项目中的所有循环依赖注入,您还需要使用上述自定义包装器类型:
464+
465+
```typescript
466+
@Injectable()
467+
export class UserService {
468+
constructor(
469+
@Inject(forwardRef(() => ProfileService))
470+
private readonly profileService: WrapperType<ProfileService>,
471+
) {};
472+
}
473+
```
474+
475+
### Jest + SWC
476+
477+
要将 SWC 与 Jest 一起使用,您需要安装以下软件包:
478+
479+
```bash
480+
$ npm i --save-dev jest @swc/core @swc/jest
481+
```
482+
483+
安装完成后,使用以下内容更新 `package.json` / `jest.config.js` 文件(具体取决于您的配置):
484+
485+
```json
486+
{
487+
"jest": {
488+
"transform": {
489+
"^.+\\.(t|j)s?$": ["@swc/jest"]
490+
}
491+
}
492+
}
493+
```
494+
495+
此外,还需要将以下 `transform` 属性添加到文件中 `.swcrc``legacyDecorator``decoratorMetadata`
496+
497+
```json
498+
{
499+
"$schema": "https://json.schemastore.org/swcrc",
500+
"sourceMaps": true,
501+
"jsc": {
502+
"parser": {
503+
"syntax": "typescript",
504+
"decorators": true,
505+
"dynamicImport": true
506+
},
507+
"transform": {
508+
"legacyDecorator": true,
509+
"decoratorMetadata": true
510+
},
511+
"baseUrl": "./"
512+
},
513+
"minify": false
514+
}
515+
```
516+
517+
如果您在项目中使用 NestJS CLI 插件,则必须手动运行 `PluginMetadataGenerator` 。导航到此部分以了解更多信息。
518+
519+
### Vitest
520+
521+
Vitest 是一款快速轻量级的测试运行器,旨在与 Vite 配合使用。它提供了一个现代、快速且易于使用的测试解决方案,可以与 NestJS 项目集成。
522+
523+
#### 安装
524+
525+
若要开始,请先安装所需的软件包:
526+
527+
```bash
528+
$ npm i --save-dev vitest unplugin-swc @swc/core @vitest/coverage-v8
529+
```
530+
531+
#### 配置
532+
533+
在应用程序的根目录中创建一个包含以下内容 `vitest.config.ts` 的文件:
534+
535+
```ts
536+
import swc from 'unplugin-swc';
537+
import { defineConfig } from 'vitest/config';
538+
539+
export default defineConfig({
540+
test: {
541+
globals: true,
542+
root: './',
543+
},
544+
plugins: [
545+
// This is required to build the test files with SWC
546+
swc.vite({
547+
// Explicitly set the module type to avoid inheriting this value from a `.swcrc` config file
548+
module: { type: 'es6' },
549+
}),
550+
],
551+
});
552+
```
553+
554+
此配置文件设置 Vitest 环境、根目录和 SWC 插件。您还应该为 e2e 测试创建一个单独的配置文件,其中包含一个指定测试路径正则表达式的附加 `include` 字段:
555+
556+
```ts
557+
import swc from 'unplugin-swc';
558+
import { defineConfig } from 'vitest/config';
559+
560+
export default defineConfig({
561+
test: {
562+
include: ['**/*.e2e-spec.ts'],
563+
globals: true,
564+
root: './',
565+
},
566+
plugins: [swc.vite()],
567+
});
568+
```
569+
570+
此外,还可以设置 `alias` 选项以支持测试中的 TypeScript 路径:
571+
572+
```ts
573+
import swc from 'unplugin-swc';
574+
import { defineConfig } from 'vitest/config';
575+
576+
export default defineConfig({
577+
test: {
578+
include: ['**/*.e2e-spec.ts'],
579+
globals: true,
580+
alias: {
581+
'@src': './src',
582+
'@test': './test',
583+
},
584+
root: './',
585+
},
586+
resolve: {
587+
alias: {
588+
'@src': './src',
589+
'@test': './test',
590+
},
591+
},
592+
plugins: [swc.vite()],
593+
});
594+
```
595+
596+
#### 在 E2E 测试中更新导入
597+
598+
将任何 E2E 测试导入更改为 `import * as request from 'supertest'` `import request from 'supertest'` 。这是必要的,因为 Vitest 在与 Vite 捆绑在一起时,需要 supertest 的默认导入。使用命名空间导入可能会导致此特定设置出现问题。
599+
600+
最后,将 package.json 文件中的测试脚本更新为以下内容:
601+
602+
```json
603+
{
604+
"scripts": {
605+
"test": "vitest run",
606+
"test:watch": "vitest",
607+
"test:cov": "vitest run --coverage",
608+
"test:debug": "vitest --inspect-brk --inspect --logHeapUsage --threads=false",
609+
"test:e2e": "vitest run --config ./vitest.config.e2e.ts"
610+
}
611+
}
612+
```
613+
614+
这些脚本配置 Vitest 以运行测试、监视更改、生成代码覆盖率报告和调试。test:e2e 脚本专门用于使用自定义配置文件运行 E2E 测试。
615+
616+
通过此设置,您现在可以享受在 NestJS 项目中使用 Vitest 的好处,包括更快的测试执行和更现代的测试体验。
617+
618+
> **提示**
619+
> 您可以[在此存储库](https://github.com/TrilonIO/nest-vitest)中查看工作示例
271620
272621
## Passport (认证)
273622

0 commit comments

Comments
 (0)