Skip to content

Commit 7175602

Browse files
committed
chore(apps/app): Setup SWC build compilation
To speed up the build compilation. hyperfine --warmup 5 --prepare "nx reset && nx clear-cache" \ --runs 100 "nx run-many -t lint" Time (mean ± σ): 2.765s ± 0.033s [User: 3.343s, System: 0.224s] Range (min … max): 2.694s … 3.007s 100 runs hyperfine --warmup 5 --prepare "nx reset && nx clear-cache" \ --runs 100 "nx run-many -t build" Time (mean ± σ): 1.816s ± 0.013s [User: 1.759s, System: 0.140s] Range (min … max): 1.792s … 1.883s 100 runs hyperfine --warmup 5 --prepare "nx reset && nx clear-cache" \ --runs 100 "nx run-many -t test" Time (mean ± σ): 1.667s ± 0.008s [User: 0.786s, System: 0.136s] Range (min … max): 1.629s … 1.688s 100 runs More: - https://docs.nestjs.com/recipes/swc#monorepo - nrwl/nx#8900
1 parent 263c82a commit 7175602

File tree

5 files changed

+1103
-71
lines changed

5 files changed

+1103
-71
lines changed

.swcrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "https://json.schemastore.org/swcrc",
3+
"sourceMaps": true,
4+
"module": {
5+
"type": "commonjs",
6+
"strict": true,
7+
"strictMode": true
8+
},
9+
"jsc": {
10+
"parser": {
11+
"syntax": "typescript",
12+
"decorators": true,
13+
"dynamicImport": true
14+
},
15+
"transform": {
16+
"legacyDecorator": true,
17+
"decoratorMetadata": true
18+
},
19+
"target": "es2021"
20+
},
21+
"minify": true
22+
}

apps/app/project.json

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@
55
"projectType": "application",
66
"tags": [],
77
"targets": {
8+
"build": {
9+
"executor": "@nx/js:swc",
10+
"outputs": [
11+
"{options.outputPath}"
12+
],
13+
"defaultConfiguration": "production",
14+
"options": {
15+
"target": "node",
16+
"compiler": "swc",
17+
"swcrc": ".swcrc",
18+
"outputPath": "dist/nxnest-demo",
19+
"main": "apps/app/src/main.ts",
20+
"tsConfig": "apps/app/tsconfig.app.json",
21+
"assets": [
22+
"src/assets"
23+
],
24+
"isolatedConfig": true
25+
},
26+
"configurations": {
27+
"development": {
28+
"swcrc": ".swcrc"
29+
},
30+
"production": {
31+
"swcrc": ".swcrc"
32+
}
33+
}
34+
},
835
"serve": {
936
"executor": "@nx/js:node",
1037
"defaultConfiguration": "development",
@@ -21,4 +48,4 @@
2148
}
2249
}
2350
}
24-
}
51+
}

apps/app/webpack.config.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
11
const { join } = require('path')
2+
const swcDefaultConfig =
3+
require('@nestjs/cli/lib/compiler/defaults/swc-defaults').swcDefaultsFactory()
4+
.swcOptions
25
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin')
6+
const { merge } = require('webpack-merge')
37

4-
module.exports = {
5-
output: {
6-
path: join(__dirname, '../../dist/apps/app')
7-
},
8-
plugins: [
9-
new NxAppWebpackPlugin({
10-
target: 'node',
11-
compiler: 'tsc',
12-
main: './src/main.ts',
13-
tsConfig: './tsconfig.app.json',
14-
assets: ['./src/assets'],
15-
optimization: false,
16-
outputHashing: 'none'
17-
})
18-
]
8+
module.exports = () => {
9+
// Get the base Nx webpack config
10+
const baseConfig = {
11+
output: {
12+
path: join(__dirname, '../../dist/apps/app')
13+
},
14+
plugins: [
15+
new NxAppWebpackPlugin({
16+
target: 'node',
17+
compiler: 'swc',
18+
main: './src/main.ts',
19+
tsConfig: './tsconfig.app.json',
20+
assets: ['./src/assets'],
21+
optimization: false,
22+
outputHashing: 'none'
23+
})
24+
]
25+
}
26+
27+
// Modify the base config to use swc-loader
28+
const customConfig = {
29+
module: {
30+
rules: [
31+
{
32+
test: /\.ts?$/,
33+
exclude: /node_modules/,
34+
use: {
35+
loader: 'swc-loader',
36+
options: swcDefaultConfig
37+
}
38+
}
39+
]
40+
}
41+
}
42+
43+
return merge(baseConfig, customConfig)
1944
}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"tslib": "^2.6.2"
2323
},
2424
"devDependencies": {
25+
"@nestjs/cli": "^10.3.2",
2526
"@nestjs/schematics": "^10.1.1",
2627
"@nestjs/testing": "^10.3.9",
2728
"@nx/eslint": "19.1.2",
@@ -34,8 +35,9 @@
3435
"@nx/webpack": "19.1.2",
3536
"@nx/workspace": "19.1.2",
3637
"@swc-node/register": "~1.9.1",
37-
"@swc/core": "~1.5.24",
38-
"@swc/helpers": "~0.5.11",
38+
"@swc/cli": "^0.3.12",
39+
"@swc/core": "^1.5.24",
40+
"@swc/helpers": "^0.5.11",
3941
"@types/jest": "^29.5.12",
4042
"@types/node": "~20.14.0",
4143
"@typescript-eslint/eslint-plugin": "^7.12.0",
@@ -54,6 +56,7 @@
5456
"jest-environment-node": "^29.7.0",
5557
"nx": "19.1.2",
5658
"prettier": "^3.3.0",
59+
"swc-loader": "^0.2.6",
5760
"ts-jest": "^29.1.4",
5861
"ts-node": "10.9.2",
5962
"typescript": "~5.4.5",

0 commit comments

Comments
 (0)