pnpm build command builds the project into optimized code for deployment to production environments. It compiles TypeScript to JavaScript and removes unnecessary code to improve execution speed.
Basic Usage
dist directory.
Subcommands
You can build API and Web separately.Build Process
The build proceeds in the following steps:1. Remove Existing Build Artifacts
Cleanly removes residual files from previous builds.dist/- API build outputweb/dist/- Web build outputweb-dist/- Copied Web output
2. Prepare API Build Configuration
Determines the tsdown configuration file to use for the API build.3. Build API Project
Compiles TypeScript to JavaScript.4. Build Web Project (Optional)
If a Web project exists, builds it and copies to the API project.1
Build start
2
Vite build
3
Copy files
4
Complete
web/dist/- Original build resultweb-dist/- Copy for serving from API server
Build Configuration
Customizing API Build Configuration
Create atsdown.config.ts file in the project root to override the default API build.
tsdown.config.ts
API Zod AOT
Whenvalidation.zodCompiler.api is "aot", sonamu build api reads only the source configuration
to select the policy. It does not run Sonamu.init() or initialize the database. The build applies
an explicit, compact, inline Rolldown plugin only to
src/application/sonamu.validators.generated.ts. It fails if an untransformed compile() boundary
or virtual runtime import remains.
Existing custom tsdown.config.ts files remain valid. Sonamu creates a temporary wrapper in the
resolved API root, delegates to the existing default export, and appends the compiler plugin. Object,
array, and function exports and existing plugin arrays are preserved. The wrapper is deleted after
the build.
Running tsdown directly bypasses the wrapper and stale-registry checks. For an AOT project, run
pnpm sonamu sync followed by pnpm sonamu build api. Existing opt-in projects must also install
zod-compiler@1.23.6 as an API development dependency. This application-local dependency is only
required for AOT; JIT uses Sonamuβs runtime dependency. To roll back AOT, remove the setting and the
application-local dependency, then run sync and build. Sync deletes the generated registry and the
build returns to the existing tsdown path. Web and Expo AOT are not available in this phase.
validation.zodCompiler.targets is also rejected; this phase accepts only the API policy.
Build Output
API Build Result
Features:- TypeScript β JavaScript conversion
- Decorator transformation complete
- Source maps included (
.js.map) - Import path resolution complete
Web Build Result (Optional)
Features:- Code minification
- Asset hashing (cache busting)
- Tree shaking (remove unused code)
Build Optimization
1. Separate Type Checking
To speed up builds, perform type checking separately.2. Incremental Builds
Use the development server to rebuild only changed files.3. Cache Utilization
tsdown reuses incremental dependency analysis across builds.Troubleshooting
Build Failure
Problem: Build fails with TypeScript errortsdown Configuration Error
Problem:tsdown.config.ts configuration error
Web Build Failure
Problem: Vite build errorRunning After Build
After build completes, run the production server with thestart command.
- Fast startup: Run pre-compiled JavaScript
- Low memory: No TypeScript transformation overhead
- Source map support: Show original file location on errors
Direct Execution
You can also run directly with Node.js instead ofpnpm start:
--enable-source-maps: Show original TypeScript file location on errorsdist/index.js: Built entry point
Loading Environment Variables
When environment variables are needed:CI/CD Configuration
Setting up CI/CD pipelines enables automatic building and deployment whenever code is pushed. Various tools like GitHub Actions, GitLab CI, Jenkins, etc. can be used.Why is CI/CD Needed?
GitHub Actions
GitHub Actions is a CI/CD platform integrated with GitHub. Define workflows by writing YAML files in the.github/workflows/ directory.
.github/workflows/deploy.yml
1
Code checkout
Use
actions/checkout@v3 to fetch repository code.2
Install pnpm
Install pnpm package manager with
pnpm/action-setup@v2.3
Setup Node.js
Install Node.js and enable pnpm cache with
actions/setup-node@v3. Cache makes dependency
installation much faster.4
Install dependencies
Install project dependencies with
pnpm install.5
Build
Perform production build with
pnpm build.6
Deploy
Upload build output to server. Various methods like rsync, scp, FTP can be used.
Adding Tests
Run tests before deployment to catch bugs early:.github/workflows/deploy.yml
Environment-based Deployment
Separate Staging and Production environments:Docker
Using Docker guarantees a consistent execution environment. Development, staging, and production all run in the same environment.Dockerfile
Docker Compose
Use Docker Compose to run with database:docker-compose.yml
Performance Comparison
Production builds are much faster and more efficient than development server.
Why production is faster:
- Pre-compiled: Donβt transform TypeScript in real-time
- Code optimization: Remove unnecessary code, compression
- No HMR overhead: No file watching and reloading costs
- Production mode: Node.js and dependencies run in optimized mode
Actual Performance Measurement
Compare development server and production build performance directly:Next Steps
start
Run the built server
dev
Return to development server