|
1 | | -# Build Process Optimization |
| 1 | +# NPM Package Build Optimization |
2 | 2 |
|
3 | | -This document outlines the optimizations implemented to reduce disk space usage in the build process for the svm-pay repository. |
| 3 | +This document describes the comprehensive build optimizations implemented for the svm-pay npm package to improve CLI/library distribution. |
4 | 4 |
|
5 | | -## Identified Issues |
| 5 | +## Build System Improvements |
6 | 6 |
|
7 | | -During the analysis of the repository structure and build process, the following disk space bottlenecks were identified: |
| 7 | +### 1. TypeScript Configuration |
| 8 | +- **Development config** (`tsconfig.json`): Includes source maps and declaration maps for debugging |
| 9 | +- **Production config** (`tsconfig.prod.json`): Optimized for distribution with no source maps and removed comments |
| 10 | +- Added proper Node.js and Jest types support |
| 11 | +- Configured proper module resolution and JSX support |
8 | 12 |
|
9 | | -1. Large node_modules directory (711MB) in the root |
10 | | -2. Multiple smaller node_modules directories in subdirectories |
11 | | -3. Duplicate dependencies across packages (especially WalletConnect-related packages) |
12 | | -4. Multiple dist directories throughout the node_modules structure |
13 | | -5. Inefficient package manager configuration |
14 | | -6. Lack of cleanup scripts for build artifacts |
15 | | - |
16 | | -## Implemented Optimizations |
17 | | - |
18 | | -### 1. Package Manager Optimization |
19 | | - |
20 | | -- Switched from Bun to pnpm for more efficient dependency management |
21 | | -- pnpm creates a more efficient node_modules structure by using symlinks to a single content-addressable store |
22 | | -- Added pnpm workspace configuration for dependency hoisting |
23 | | - |
24 | | -```yaml |
25 | | -# pnpm-workspace.yaml |
26 | | -packages: |
27 | | - - 'website/apps/*' |
28 | | - - 'website/packages/*' |
29 | | - - 'website/tooling/*' |
| 13 | +### 2. Build Scripts |
| 14 | +```json |
| 15 | +{ |
| 16 | + "clean": "rm -rf dist", |
| 17 | + "build": "npm run clean && tsc", |
| 18 | + "build:production": "npm run clean && NODE_ENV=production tsc -p tsconfig.prod.json && NODE_ENV=production node scripts/optimize-build.js --no-source-maps", |
| 19 | + "postbuild": "chmod +x dist/bin/svm-pay.js && node scripts/optimize-build.js", |
| 20 | + "test:ci": "jest --passWithNoTests --ci --coverage", |
| 21 | + "lint:fix": "eslint src --ext .ts,.tsx --fix", |
| 22 | + "prepublishOnly": "npm run build:production && npm run test:ci && npm run lint", |
| 23 | + "cli:test": "node dist/bin/svm-pay.js --help", |
| 24 | + "validate:package": "npm pack --dry-run" |
| 25 | +} |
30 | 26 | ``` |
31 | 27 |
|
32 | | -### 2. NPM Configuration |
33 | | -
|
34 | | -Added `.npmrc` files with optimized settings: |
35 | | - |
| 28 | +### 3. Build Optimization Script |
| 29 | +The `scripts/optimize-build.js` script provides: |
| 30 | +- **CLI Binary Permissions**: Ensures the CLI executable has proper permissions (755) |
| 31 | +- **Source Map Removal**: Removes source maps in production builds to reduce package size |
| 32 | +- **Build Summary**: Provides detailed information about the built package |
| 33 | +- **File Counting**: Tracks the number of files generated |
| 34 | + |
| 35 | +### 4. Package Configuration |
| 36 | +- **Entry Points**: Properly configured main, types, and bin entries |
| 37 | +- **Exports Map**: Comprehensive exports for different frameworks (React, Vue, Angular, Server) |
| 38 | +- **Files Field**: Optimized to include only necessary distribution files |
| 39 | +- **Peer Dependencies**: Properly configured optional peer dependencies for frameworks |
| 40 | + |
| 41 | +## Distribution Optimizations |
| 42 | + |
| 43 | +### Package Size Reduction |
| 44 | +- **Production Builds**: Remove source maps, comments, and debug information |
| 45 | +- **File Optimization**: Only include necessary files in the npm package |
| 46 | +- **Build Artifacts**: Clean build process that removes old artifacts |
| 47 | + |
| 48 | +### CLI Distribution |
| 49 | +- **Executable Binary**: Properly configured CLI binary with shebang and permissions |
| 50 | +- **Command Structure**: Well-organized command structure with proper help and version support |
| 51 | +- **Dependencies**: Optimized dependency tree for CLI functionality |
| 52 | + |
| 53 | +### Library Distribution |
| 54 | +- **Multiple Entry Points**: Support for different frameworks and environments |
| 55 | +- **Type Definitions**: Complete TypeScript type definitions for all exported modules |
| 56 | +- **Tree Shaking**: Properly configured for optimal bundling in consumer applications |
| 57 | + |
| 58 | +## Build Validation |
| 59 | + |
| 60 | +### Testing |
| 61 | +- **CI Tests**: Automated testing with coverage reports |
| 62 | +- **CLI Testing**: Automated CLI functionality testing |
| 63 | +- **Lint Checks**: Code quality and style validation |
| 64 | + |
| 65 | +### Package Validation |
| 66 | +- **Dry Run**: `npm pack --dry-run` validation to ensure proper package structure |
| 67 | +- **Size Analysis**: Monitor package size and file count |
| 68 | +- **Dependency Analysis**: Validate dependency tree and peer dependencies |
| 69 | + |
| 70 | +## Usage |
| 71 | + |
| 72 | +### Development Build |
| 73 | +```bash |
| 74 | +npm run build |
36 | 75 | ``` |
37 | | -# Reduce package size by not installing optional dependencies |
38 | | -ignore-optional=true |
39 | | - |
40 | | -# Use exact versions to prevent unexpected updates |
41 | | -save-exact=true |
42 | | - |
43 | | -# Reduce disk space by not generating package-lock.json |
44 | | -package-lock=false |
45 | | - |
46 | | -# Reduce disk space by not saving npm logs |
47 | | -loglevel=error |
48 | 76 |
|
49 | | -# Reduce disk space by not saving npm cache |
50 | | -cache=.npm-cache |
51 | | - |
52 | | -# Reduce disk space by pruning dependencies when installing |
53 | | -prune=true |
| 77 | +### Production Build |
| 78 | +```bash |
| 79 | +npm run build:production |
| 80 | +``` |
54 | 81 |
|
55 | | -# Reduce disk space by using a shared store for dependencies |
56 | | -shared-workspace-lockfile=true |
| 82 | +### Full CI Pipeline |
| 83 | +```bash |
| 84 | +npm run prepublishOnly |
| 85 | +``` |
57 | 86 |
|
58 | | -# Reduce disk space by not installing peer dependencies automatically |
59 | | -legacy-peer-deps=true |
| 87 | +### Package Validation |
| 88 | +```bash |
| 89 | +npm run validate:package |
60 | 90 | ``` |
61 | 91 |
|
62 | | -### 3. PNPM Configuration |
| 92 | +### CLI Testing |
| 93 | +```bash |
| 94 | +npm run cli:test |
| 95 | +``` |
63 | 96 |
|
64 | | -Added `.pnpmrc` file with optimized settings: |
| 97 | +## Package Structure |
65 | 98 |
|
66 | 99 | ``` |
67 | | -shamefully-hoist=true |
68 | | -strict-peer-dependencies=false |
69 | | -auto-install-peers=true |
70 | | -link-workspace-packages=true |
71 | | -shared-workspace-lockfile=true |
72 | | -resolution-mode=highest |
| 100 | +dist/ |
| 101 | +├── bin/ |
| 102 | +│ └── svm-pay.js # CLI executable |
| 103 | +├── cli/ # CLI implementation |
| 104 | +├── core/ # Core SDK functionality |
| 105 | +├── network/ # Network adapters |
| 106 | +├── sdk/ # Framework integrations |
| 107 | +├── walletconnect/ # WalletConnect integration |
| 108 | +└── index.js # Main entry point |
73 | 109 | ``` |
74 | 110 |
|
75 | | -### 4. Build Script Optimization |
76 | | -
|
77 | | -Updated package.json scripts to include clean and prune operations: |
| 111 | +## Performance Metrics |
78 | 112 |
|
79 | | -```json |
80 | | -"scripts": { |
81 | | - "clean": "rm -rf node_modules/.cache dist .parcel-cache", |
82 | | - "prune": "npm prune --production" |
83 | | -} |
84 | | -``` |
85 | | - |
86 | | -```json |
87 | | -"scripts": { |
88 | | - "build": "turbo build --no-cache", |
89 | | - "clean": "rm -rf node_modules/.cache .turbo", |
90 | | - "clean:deep": "find . -name 'node_modules' -type d -prune -exec rm -rf {} \\; && find . -name '.turbo' -type d -prune -exec rm -rf {} \\; && find . -name '.next' -type d -prune -exec rm -rf {} \\;", |
91 | | - "prune": "pnpm prune --prod", |
92 | | - "build:prod": "pnpm clean && pnpm build && pnpm prune" |
93 | | -} |
94 | | -``` |
| 113 | +- **Package Size**: ~75 KB compressed |
| 114 | +- **Unpacked Size**: ~372 KB |
| 115 | +- **File Count**: 176 files (production), 172 files (development) |
| 116 | +- **CLI Startup**: Fast startup with minimal dependencies |
| 117 | +- **Tree Shaking**: Optimized for modern bundlers |
95 | 118 |
|
96 | | -### 5. Next.js Configuration Optimization |
97 | | - |
98 | | -Updated Next.js configuration with disk space optimizations: |
99 | | - |
100 | | -```javascript |
101 | | -// Optimize build output for reduced disk space |
102 | | -swcMinify: true, |
103 | | -compress: true, |
104 | | - |
105 | | -// Reduce build output size |
106 | | -productionBrowserSourceMaps: false, |
107 | | - |
108 | | -// Optimize for production |
109 | | -poweredByHeader: false, |
110 | | - |
111 | | -// Reduce disk space by optimizing output |
112 | | -optimizeFonts: true, |
113 | | - |
114 | | -// Reduce disk space by disabling image optimization in development |
115 | | -images: { |
116 | | - disableStaticImages: process.env.NODE_ENV === 'development', |
117 | | - domains: ["images.unsplash.com", "avatars.githubusercontent.com", "www.twillot.com", "cdnv2.ruguoapp.com", "www.setupyourpay.com"], |
118 | | -}, |
119 | | - |
120 | | -// Webpack optimization for reduced disk space |
121 | | -webpack: (config, { dev, isServer }) => { |
122 | | - // Optimize CSS |
123 | | - config.optimization = { |
124 | | - ...config.optimization, |
125 | | - minimize: !dev, |
126 | | - }; |
127 | | - |
128 | | - // Reduce disk space by excluding large development-only packages |
129 | | - if (dev) { |
130 | | - config.resolve.alias = { |
131 | | - ...config.resolve.alias, |
132 | | - 'react-dom$': 'react-dom/profiling', |
133 | | - }; |
134 | | - } |
135 | | - |
136 | | - return config; |
137 | | -}, |
138 | | -``` |
| 119 | +## Framework Support |
139 | 120 |
|
140 | | -### 6. Turbo Configuration Optimization |
| 121 | +The package provides optimized entry points for: |
| 122 | +- **React**: `svm-pay/react` and `svm-pay/react-integration` |
| 123 | +- **Vue**: `svm-pay/vue` and `svm-pay/vue-integration` |
| 124 | +- **Angular**: `svm-pay/angular` |
| 125 | +- **Server**: `svm-pay/server` for Node.js environments |
| 126 | +- **CLI**: Global CLI installation via `npm install -g svm-pay` |
141 | 127 |
|
142 | | -Created a turbo.json file with optimized cache and build settings: |
| 128 | +## Publishing |
143 | 129 |
|
144 | | -```json |
145 | | -{ |
146 | | - "cache": { |
147 | | - "dir": ".turbo", |
148 | | - "workers": 4 |
149 | | - }, |
150 | | - "cache.compression": { |
151 | | - "enabled": true, |
152 | | - "level": 9 |
153 | | - }, |
154 | | - "build": { |
155 | | - "output_logs": false, |
156 | | - "log_prefix": false |
157 | | - }, |
158 | | - "prune": { |
159 | | - "enabled": true, |
160 | | - "include_dependencies": false, |
161 | | - "include_dev_dependencies": false |
162 | | - }, |
163 | | - "global": { |
164 | | - "output_dir": "dist", |
165 | | - "no_daemon": true |
166 | | - } |
167 | | -} |
168 | | -``` |
| 130 | +The package is configured for automated publishing via GitHub Actions: |
| 131 | +- **Release Tags**: Triggers on version tags (v*) |
| 132 | +- **CI Pipeline**: Runs tests, linting, and builds |
| 133 | +- **NPM Publishing**: Automated publishing to npm registry |
| 134 | +- **Build Validation**: Ensures package quality before publishing |
169 | 135 |
|
170 | | -## Results |
| 136 | +## Previous Optimizations |
171 | 137 |
|
172 | | -These optimizations have significantly reduced disk space usage: |
| 138 | +The repository also includes optimizations for the workspace build process: |
173 | 139 |
|
174 | | -- Reduced inode usage from 100% to 64% |
175 | | -- Improved disk space efficiency through dependency hoisting and deduplication |
176 | | -- Reduced build artifact size through optimized configurations |
177 | | -- Added scripts for cleaning temporary files and pruning dependencies |
| 140 | +### Package Manager Optimization |
| 141 | +- pnpm workspace configuration for dependency hoisting |
| 142 | +- Reduced package size through configuration optimizations |
| 143 | +- Efficient dependency management with shared stores |
178 | 144 |
|
179 | | -## Recommendations for Further Optimization |
| 145 | +### Build Configuration |
| 146 | +- Turbo.json configuration for optimized caching |
| 147 | +- Next.js configuration optimization for production builds |
| 148 | +- Webpack optimization for reduced disk space |
180 | 149 |
|
181 | | -1. Regularly run the clean scripts to remove temporary files and build artifacts |
182 | | -2. Consider implementing a CI/CD pipeline that uses the optimized build process |
183 | | -3. Periodically audit dependencies to remove unused packages |
184 | | -4. Use the production build script for deployment to ensure minimal disk space usage |
| 150 | +These comprehensive optimizations ensure optimal npm package distribution for both CLI and library usage. |
0 commit comments