Skip to content

Commit

Permalink
chore(apps/app): Setup SWC build compilation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
roalcantara committed Jun 3, 2024
1 parent 263c82a commit 7175602
Show file tree
Hide file tree
Showing 5 changed files with 1,103 additions and 71 deletions.
22 changes: 22 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": true,
"module": {
"type": "commonjs",
"strict": true,
"strictMode": true
},
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"target": "es2021"
},
"minify": true
}
29 changes: 28 additions & 1 deletion apps/app/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@
"projectType": "application",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:swc",
"outputs": [
"{options.outputPath}"
],
"defaultConfiguration": "production",
"options": {
"target": "node",
"compiler": "swc",
"swcrc": ".swcrc",
"outputPath": "dist/nxnest-demo",
"main": "apps/app/src/main.ts",
"tsConfig": "apps/app/tsconfig.app.json",
"assets": [
"src/assets"
],
"isolatedConfig": true
},
"configurations": {
"development": {
"swcrc": ".swcrc"
},
"production": {
"swcrc": ".swcrc"
}
}
},
"serve": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
Expand All @@ -21,4 +48,4 @@
}
}
}
}
}
55 changes: 40 additions & 15 deletions apps/app/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
const { join } = require('path')
const swcDefaultConfig =
require('@nestjs/cli/lib/compiler/defaults/swc-defaults').swcDefaultsFactory()
.swcOptions
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin')
const { merge } = require('webpack-merge')

module.exports = {
output: {
path: join(__dirname, '../../dist/apps/app')
},
plugins: [
new NxAppWebpackPlugin({
target: 'node',
compiler: 'tsc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
optimization: false,
outputHashing: 'none'
})
]
module.exports = () => {
// Get the base Nx webpack config
const baseConfig = {
output: {
path: join(__dirname, '../../dist/apps/app')
},
plugins: [
new NxAppWebpackPlugin({
target: 'node',
compiler: 'swc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
optimization: false,
outputHashing: 'none'
})
]
}

// Modify the base config to use swc-loader
const customConfig = {
module: {
rules: [
{
test: /\.ts?$/,
exclude: /node_modules/,
use: {
loader: 'swc-loader',
options: swcDefaultConfig
}
}
]
}
}

return merge(baseConfig, customConfig)
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"tslib": "^2.6.2"
},
"devDependencies": {
"@nestjs/cli": "^10.3.2",
"@nestjs/schematics": "^10.1.1",
"@nestjs/testing": "^10.3.9",
"@nx/eslint": "19.1.2",
Expand All @@ -34,8 +35,9 @@
"@nx/webpack": "19.1.2",
"@nx/workspace": "19.1.2",
"@swc-node/register": "~1.9.1",
"@swc/core": "~1.5.24",
"@swc/helpers": "~0.5.11",
"@swc/cli": "^0.3.12",
"@swc/core": "^1.5.24",
"@swc/helpers": "^0.5.11",
"@types/jest": "^29.5.12",
"@types/node": "~20.14.0",
"@typescript-eslint/eslint-plugin": "^7.12.0",
Expand All @@ -54,6 +56,7 @@
"jest-environment-node": "^29.7.0",
"nx": "19.1.2",
"prettier": "^3.3.0",
"swc-loader": "^0.2.6",
"ts-jest": "^29.1.4",
"ts-node": "10.9.2",
"typescript": "~5.4.5",
Expand Down
Loading

0 comments on commit 7175602

Please sign in to comment.