Rspack's Source Map functionality is crucial for debugging and error tracking, allowing compiled code to be mapped back to the original source code. Here's a detailed explanation of Rspack's Source Map:
Basic Concepts of Source Map
Source Map is an information file that stores the mapping relationship between source code and compiled code. It allows developers to debug original source code in the browser instead of compiled code.
Source Map Types
Rspack supports multiple Source Map types, each with different trade-offs between performance and precision:
1. eval
The fastest Source Map type with the least information:
javascriptmodule.exports = { mode: 'development', devtool: 'eval' }
Characteristics:
- Fastest build speed
- Each module is executed using eval()
- Does not include column information
- Not suitable for production
2. eval-source-map
Generates independent Source Map for each module:
javascriptmodule.exports = { mode: 'development', devtool: 'eval-source-map' }
Characteristics:
- Relatively fast build speed
- Contains complete Source Map
- Suitable for development environment
- Larger file size
3. cheap-eval-source-map
Source Map without column information:
javascriptmodule.exports = { mode: 'development', devtool: 'cheap-eval-source-map' }
Characteristics:
- Fast build speed
- Does not include column information
- Suitable for development environment
- Balance between performance and precision
4. cheap-module-eval-source-map
Source Map including Loader transformations:
javascriptmodule.exports = { mode: 'development', devtool: 'cheap-module-eval-source-map' }
Characteristics:
- Includes Loader Source Map
- Does not include column information
- Suitable for development environment
- More accurate debugging information
5. source-map
Generates complete Source Map file:
javascriptmodule.exports = { mode: 'production', devtool: 'source-map' }
Characteristics:
- Generates independent .map file
- Contains complete debugging information
- Slower build speed
- Suitable for production environment
6. hidden-source-map
Generates Source Map but does not reference it:
javascriptmodule.exports = { mode: 'production', devtool: 'hidden-source-map' }
Characteristics:
- Generates independent Source Map
- Not referenced in bundle
- Used for error tracking services
- Does not affect user experience
7. nosources-source-map
Only shows error line numbers, not source code:
javascriptmodule.exports = { mode: 'production', devtool: 'nosources-source-map' }
Characteristics:
- Protects source code
- Only shows error location
- Suitable for production environment
- High security
Environment Configuration
Development Environment
javascriptmodule.exports = { mode: 'development', devtool: 'eval-cheap-module-source-map', devServer: { client: { overlay: { errors: true, warnings: false } } } }
Recommended Configuration:
eval-cheap-module-source-map: Balance performance and precisioneval-source-map: When complete debugging information is neededcheap-module-eval-source-map: For large projects
Production Environment
javascriptmodule.exports = { mode: 'production', devtool: 'source-map', optimization: { minimize: true } }
Recommended Configuration:
source-map: Complete debugging informationhidden-source-map: Error tracking servicesnosources-source-map: Protect source code
Source Map Optimization
1. Exclude Third-party Libraries
javascriptmodule.exports = { devtool: 'source-map', module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { sourceMaps: true } } } ] } }
2. Use Source Map DevTool
javascriptconst { SourceMapDevToolPlugin } = require('@rspack/core'); module.exports = { plugins: [ new SourceMapDevToolPlugin({ filename: '[file].map', exclude: [/node_modules/], append: '\n//# sourceMappingURL=[url]' }) ] }
3. Configure Source Map Options
javascriptmodule.exports = { devtool: 'source-map', output: { devtoolModuleFilenameTemplate: 'webpack://[resource-path]?[loaders]', devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]' } }
Source Map and Error Tracking
1. Error Tracking Service
javascriptmodule.exports = { mode: 'production', devtool: 'hidden-source-map', plugins: [ new SentryWebpackPlugin({ authToken: process.env.SENTRY_AUTH_TOKEN, org: 'your-org', project: 'your-project', include: './dist' }) ] }
2. Local Error Analysis
javascriptconst sourceMap = require('source-map'); async function analyzeError(stackTrace) { const consumer = await new sourceMap.SourceMapConsumer(sourceMapData); const originalPosition = consumer.originalPositionFor({ line: lineNumber, column: columnNumber }); return originalPosition; }
Performance Considerations
1. Build Time
Build time comparison for different Source Map types:
eval: Fastesteval-source-map: Relatively fastcheap-eval-source-map: Fastsource-map: Slower
2. File Size
Source Map increases file size:
- Development environment: Acceptable
- Production environment: Consider using CDN or separate storage
3. Memory Usage
Source Map uses additional memory:
- Large projects: Consider using
cheapoptions - Monitor memory usage
Best Practices
-
Development Environment:
- Use
eval-cheap-module-source-map - Enable error overlay
- Fast feedback
- Use
-
Production Environment:
- Use
source-maporhidden-source-map - Configure error tracking services
- Protect source code
- Use
-
Performance Optimization:
- Exclude Source Map for third-party libraries
- Use
cheapoptions to improve performance - Reasonably choose Source Map type
-
Security Considerations:
- Use
nosources-source-mapin production - Avoid exposing source code
- Configure access control
- Use
-
Debugging Experience:
- Configure browser Source Map
- Use debugging tools
- Provide clear error messages
Rspack's Source Map functionality provides developers with powerful debugging capabilities. Through reasonable configuration and usage, it can provide a good debugging experience while maintaining performance.