Rspack's asset processing capability is an important part of its build functionality, capable of efficiently handling various types of static assets. Here's a detailed explanation of Rspack's asset processing:
Asset Types
Rspack can handle multiple types of assets:
- Image Assets: PNG, JPG, GIF, SVG, WebP, etc.
- Font Assets: WOFF, WOFF2, TTF, EOT, etc.
- Media Assets: MP4, WebM, OGG, etc.
- Data Assets: JSON, XML, CSV, etc.
- Other Assets: TXT, MD, etc.
Asset Module Types
Rspack provides four asset module types:
1. asset/resource
Emits the asset as a separate file and exports the URL:
javascriptmodule.exports = { module: { rules: [ { test: /\.(png|jpe?g|gif|svg)$/i, type: 'asset/resource', generator: { filename: 'images/[hash][ext][query]' } } ] } }
2. asset/inline
Inlines the asset as a data URI in the bundle:
javascriptmodule.exports = { module: { rules: [ { test: /\.svg$/i, type: 'asset/inline' } ] } }
3. asset/source
Exports the asset as source code:
javascriptmodule.exports = { module: { rules: [ { test: /\.txt$/i, type: 'asset/source' } ] } }
4. asset
Automatically chooses between resource and inline:
javascriptmodule.exports = { module: { rules: [ { test: /\.(png|jpe?g|gif|svg)$/i, type: 'asset', parser: { dataUrlCondition: { maxSize: 8192 // Inline files smaller than 8KB } } } ] } }
Image Processing
Basic Configuration
javascriptmodule.exports = { module: { rules: [ { test: /\.(png|jpe?g|gif|webp)$/i, type: 'asset', generator: { filename: 'images/[name].[hash:6][ext]' } } ] } }
SVG Processing
javascriptmodule.exports = { module: { rules: [ { test: /\.svg$/i, oneOf: [ { resourceQuery: /component/, use: ['@svgr/webpack'] }, { type: 'asset/resource' } ] } ] } }
Image Optimization
javascriptconst ImageMinimizerPlugin = require('image-minimizer-webpack-plugin'); module.exports = { module: { rules: [ { test: /\.(jpe?g|png|gif|svg)$/i, type: 'asset', parser: { dataUrlCondition: { maxSize: 8192 } } } ] }, optimization: { minimizer: [ new ImageMinimizerPlugin({ minimizer: { implementation: ImageMinimizerPlugin.imageminGenerate, options: { plugins: [ ['imagemin-gifsicle', { interlaced: true }], ['imagemin-mozjpeg', { progressive: true }], ['imagemin-pngquant', { quality: [0.65, 0.9] }], ['imagemin-svgo', { plugins: [{ removeViewBox: false }] }] ] } } }) ] } }
Font Processing
Basic Configuration
javascriptmodule.exports = { module: { rules: [ { test: /\.(woff|woff2|eot|ttf|otf)$/i, type: 'asset/resource', generator: { filename: 'fonts/[name].[hash:6][ext]' } } ] } }
Font Optimization
javascriptmodule.exports = { module: { rules: [ { test: /\.(woff|woff2)$/i, type: 'asset/resource', generator: { filename: 'fonts/[name].[hash:6][ext]' }, use: [ { loader: 'fontmin-webpack', options: { glyphs: ['\ue000-\uefff'] } } ] } ] } }
Media Asset Processing
Video Processing
javascriptmodule.exports = { module: { rules: [ { test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)$/i, type: 'asset/resource', generator: { filename: 'media/[name].[hash:6][ext]' } } ] } }
Data Asset Processing
JSON Processing
javascriptmodule.exports = { module: { rules: [ { test: /\.json$/i, type: 'json' } ] } }
XML Processing
javascriptmodule.exports = { module: { rules: [ { test: /\.xml$/i, use: 'xml-loader' } ] } }
Asset Loading Methods
1. ES Module Import
javascriptimport logo from './logo.png'; import data from './data.json'; console.log(logo); // Asset URL console.log(data); // JSON data
2. CommonJS Import
javascriptconst logo = require('./logo.png'); const data = require('./data.json'); console.log(logo); // Asset URL console.log(data); // JSON data
3. Dynamic Import
javascriptconst loadImage = async () => { const logo = await import('./logo.png'); return logo.default; };
Asset Naming Strategy
Hash Naming
javascriptmodule.exports = { output: { filename: '[name].[contenthash:8].js', assetModuleFilename: 'assets/[name].[hash:8][ext]' } }
Naming Placeholders
[name]: Asset name[ext]: Asset extension[hash]: Asset hash[contenthash]: Content hash[hash:n]: Hash of specified length
Asset Caching Strategy
Long-term Caching
javascriptmodule.exports = { output: { filename: '[name].[contenthash:8].js', assetModuleFilename: 'assets/[name].[contenthash:8][ext]' }, optimization: { runtimeChunk: 'single', moduleIds: 'deterministic' } }
CDN Configuration
javascriptmodule.exports = { output: { publicPath: 'https://cdn.example.com/assets/' } }
Asset Compression
Gzip Compression
javascriptconst CompressionPlugin = require('compression-webpack-plugin'); module.exports = { plugins: [ new CompressionPlugin({ algorithm: 'gzip', test: /\.(js|css|html|svg)$/, threshold: 10240, minRatio: 0.8 }) ] }
Brotli Compression
javascriptconst CompressionPlugin = require('compression-webpack-plugin'); module.exports = { plugins: [ new CompressionPlugin({ algorithm: 'brotliCompress', test: /\.(js|css|html|svg)$/, threshold: 10240, minRatio: 0.8 }) ] }
Best Practices
-
Asset Optimization:
- Compress images and fonts
- Use appropriate formats
- Load assets on demand
-
Caching Strategy:
- Use contenthash
- Configure long-term caching
- Utilize CDN acceleration
-
Performance Optimization:
- Inline small assets
- Lazy load images
- Use WebP format
-
Development Experience:
- Reasonably configure asset paths
- Provide clear naming
- Optimize build speed
Rspack's asset processing features provide developers with powerful and flexible asset management capabilities. Through reasonable configuration and optimization, application performance and user experience can be significantly improved.