Rspack's Dev Server provides developers with a powerful local development environment, supporting hot reloading, proxying, HTTPS, and more. Here's a detailed explanation of Rspack Dev Server:
Basic Configuration
Starting the Dev Server
javascript// rspack.config.js module.exports = { mode: 'development', devServer: { static: { directory: path.join(__dirname, 'public') }, compress: true, port: 9000 } }
Command Line Start
bash# Start in development mode npx rspack serve # Specify configuration file npx rspack serve --config rspack.config.js # Specify port npx rspack serve --port 8080
Core Features
1. Hot Module Replacement (HMR)
Rspack Dev Server has powerful built-in HMR functionality:
javascriptmodule.exports = { devServer: { hot: true, // Enable HMR liveReload: false // Disable automatic page reload } }
2. Static File Serving
javascriptmodule.exports = { devServer: { static: { directory: path.join(__dirname, 'public'), publicPath: '/', serveIndex: true, watch: true } } }
3. Proxy Configuration
Solve cross-origin issues in development:
javascriptmodule.exports = { devServer: { proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true, pathRewrite: { '^/api': '' } }, '/images': { target: 'http://example.com', changeOrigin: true } } } }
4. HTTPS Support
javascriptmodule.exports = { devServer: { https: { key: fs.readFileSync('path/to/private.key'), cert: fs.readFileSync('path/to/certificate.pem'), ca: fs.readFileSync('path/to/ca.pem') } } }
Advanced Configuration
1. Compression Configuration
javascriptmodule.exports = { devServer: { compress: true, client: { overlay: { errors: true, warnings: false } } } }
2. Enable Gzip
javascriptmodule.exports = { devServer: { compress: true, devMiddleware: { stats: 'errors-only' } } }
3. History API Fallback
javascriptmodule.exports = { devServer: { historyApiFallback: { index: '/index.html', rewrites: [ { from: /^\/api/, to: '/404.html' } ] } } }
4. Open Browser
javascriptmodule.exports = { devServer: { open: true, open: { app: { name: 'google chrome' } } } }
Performance Optimization
1. Cache Configuration
javascriptmodule.exports = { devServer: { devMiddleware: { index: true, writeToDisk: false, stats: 'minimal' } } }
2. Watch Options
javascriptmodule.exports = { devServer: { watchFiles: { paths: ['src/**/*.php', 'public/**/*'], options: { usePolling: false, interval: 1000 } } } }
3. Build Delay
javascriptmodule.exports = { devServer: { devMiddleware: { index: true, writeToDisk: false }, client: { logging: 'warn', overlay: { errors: true, warnings: false } } } }
Error Handling
1. Error Overlay
javascriptmodule.exports = { devServer: { client: { overlay: { errors: true, warnings: true } } } }
2. Error Logging
javascriptmodule.exports = { devServer: { devMiddleware: { stats: { colors: true, hash: false, version: false, timings: true, assets: false, chunks: false, modules: false, reasons: false, children: false, source: false, errors: true, errorDetails: true, warnings: true, publicPath: false } } } }
Middleware Configuration
Rspack Dev Server supports custom middleware:
javascriptconst express = require('express'); const app = express(); app.get('/some/path', function(req, res) { res.json({ custom: 'response' }); }); module.exports = { devServer: { setupMiddlewares: (middlewares, devServer) => { if (!devServer) { throw new Error('devServer is not defined'); } devServer.app.get('/some/path', function(req, res) { res.json({ custom: 'response' }); }); return middlewares; } } }
WebSocket Configuration
javascriptmodule.exports = { devServer: { client: { webSocketURL: 'auto://0.0.0.0:0/ws' }, webSocketServer: { type: 'ws', options: { host: 'localhost', port: 8080 } } } }
Best Practices
-
Environment Separation:
- Use Dev Server in development environment
- Use static file server in production
- Configure different environment variables
-
Proxy Configuration:
- Reasonably configure proxies to solve cross-origin issues
- Use environment variables to manage proxy addresses
- Consider using Mock services
-
Performance Optimization:
- Enable HMR to improve development experience
- Reasonably configure watch options
- Avoid unnecessary file watching
-
Error Handling:
- Enable error overlay for quick problem identification
- Configure appropriate log levels
- Use source maps for debugging
-
Security Considerations:
- Don't expose sensitive information in development
- Use HTTPS to test security features
- Configure appropriate CORS policies
Rspack Dev Server provides developers with a powerful and flexible development environment. Through reasonable configuration, it can greatly improve development efficiency and experience.