Skip to main content
The 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

Once the build completes, optimized JavaScript files are generated in the 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.
Directories removed:
  • dist/ - API build output
  • web/dist/ - Web build output
  • web-dist/ - Copied Web output

2. Prepare API Build Configuration

Determines the tsdown configuration file to use for the API build.
Using custom config:
Using default config:

3. Build API Project

Compiles TypeScript to JavaScript.
1

Build start

2

Run compilation

Build tool: tsdown with OXC/Rolldown
3

Complete

4. Build Web Project (Optional)

If a Web project exists, builds it and copies to the API project.
1

Build start

2

Vite build

Optimizes and bundles static assets.
3

Copy files

Copies for serving from API server.
4

Complete

Web build output:
  • web/dist/ - Original build result
  • web-dist/ - Copy for serving from API server

Build Configuration

Customizing API Build Configuration

Create a tsdown.config.ts file in the project root to override the default API build.
tsdown.config.ts
Key options:
If no tsdown.config.ts file exists, Sonamu falls back to its bundled default API build config.

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 error
Solution:

tsdown Configuration Error

Problem: tsdown.config.ts configuration error
Solution:

Web Build Failure

Problem: Vite build error
Solution:

Running After Build

After build completes, run the production server with the start command.
Production execution features:
  • 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 of pnpm start:
Option description:
  • --enable-source-maps: Show original TypeScript file location on errors
  • dist/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
Key step descriptions:
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
Benefits of multi-stage builds:

Docker Compose

Use Docker Compose to run with database:
docker-compose.yml
Run:

Performance Comparison

Production builds are much faster and more efficient than development server. Why production is faster:
  1. Pre-compiled: Don’t transform TypeScript in real-time
  2. Code optimization: Remove unnecessary code, compression
  3. No HMR overhead: No file watching and reloading costs
  4. Production mode: Node.js and dependencies run in optimized mode

Actual Performance Measurement

Compare development server and production build performance directly:
API response time comparison:

Next Steps

start

Run the built server

dev

Return to development server