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
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