|
| 1 | +# Using NestJS with SWC |
| 2 | + |
| 3 | +This experimental project aims to explore the possibilities of using `swc` with `NestJS`. |
| 4 | +Tries to mimic a non-trivial project with `TypeORM`, `nestjsx/crud`, and dynamically loaded entities. |
| 5 | + |
| 6 | +foo |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- dev-server with |
| 11 | + - experimental "hot-reloading" |
| 12 | + - transpiled by `swc` instead of `ts-loader` |
| 13 | + - does not write transpiled files to disk (no `dist` folder in dev) |
| 14 | + - supports source-maps (correct mappings of line numbers in errors to Typescript source files) |
| 15 | +- alternative webpack setup (disclaimer: totally not happy with this) |
| 16 | + - uses `swc-loader` instead of `ts-loader` |
| 17 | + - removes `ForkTsCheckerWebpackPlugin` |
| 18 | + - uses a HMR config based on the docs |
| 19 | + - ⚠️ does not seem reliable (code changes are sometimes not reflected in responses). Unclear if `swc-loader` might cause or if the setup is wrong. |
| 20 | + - ⚠️ does not have proper source-map support |
| 21 | + - ⚠️ does not support generic glob references to entities |
| 22 | +- testing (`jest`) |
| 23 | + - uses `@swc/jest` for transforms |
| 24 | + - supports coverage reports |
| 25 | + - supports watch mode |
| 26 | + - supports e2e-test. ⚠️ needed to change `import * as request from "supertest";` to `import request from "supertest";` to make it work |
| 27 | + |
| 28 | +## Benchmarks |
| 29 | + |
| 30 | +### jest |
| 31 | + |
| 32 | +```bash |
| 33 | +❯ hyperfine "npm run test" # with ts-jest |
| 34 | +Benchmark #1: npm run test |
| 35 | + Time (mean ± σ): 3.436 s ± 0.349 s [User: 4.680 s, System: 0.543 s] |
| 36 | + Range (min … max): 3.156 s … 4.211 s 10 runs |
| 37 | + |
| 38 | +❯ hyperfine "npm run test" # with @swc/jest |
| 39 | +Benchmark #1: npm run test |
| 40 | + Time (mean ± σ): 1.447 s ± 0.020 s [User: 1.268 s, System: 0.323 s] |
| 41 | + Range (min … max): 1.428 s … 1.500 s |
| 42 | +``` |
| 43 | + |
| 44 | +## Caveats |
| 45 | + |
| 46 | +- Sometimes, TypeORM does not close the connection properly. In this case, the server needs to be restarted manually |
| 47 | +- HMR is 100% experimental. Open to any ideas on how this could be implemented better. |
0 commit comments