monosize Rspack Bundler Plugin
raw JSON →monosize-bundler-rspack is a specialized plugin for the `monosize` tool, designed to integrate Rspack as the underlying bundler for comprehensive JavaScript bundle size analysis. It allows developers to harness the high performance of Rspack for evaluating bundle sizes within large-scale monorepos or projects optimized for `monosize`. Currently at version 0.2.1, it is an actively developed component within the broader Microsoft `monosize` ecosystem. While exact release cadences are not strictly defined for this plugin, its early version number indicates ongoing development and potential for rapid evolution. A key differentiator is its default 'batch build mode,' which significantly enhances build speed (8-14x faster in typical scenarios) compared to sequential bundling, making it exceptionally well-suited for efficient monorepo size optimization. The plugin also provides an API to customize the underlying rsbuild configuration, offering flexibility for intricate build requirements.
Common errors
error Error: Cannot find module 'monosize-bundler-rspack' from 'monosize.config.mjs' ↓
npm install --save-dev monosize-bundler-rspack or yarn add --dev monosize-bundler-rspack. error TypeError: rspackBundler is not a function ↓
monosize.config.mjs uses import rspackBundler from 'monosize-bundler-rspack'; and not const rspackBundler = require(...) or import { rspackBundler } from '...'. Also verify the file extension is .mjs. error rsbuild build failed: [detailed Rspack error messages] ↓
monosize.config.mjs customization by commenting out changes, and ensure all source files and their dependencies are correctly resolved and valid. Warnings
breaking As a pre-1.0 package (current version 0.2.1), `monosize-bundler-rspack` may introduce breaking changes in minor or even patch versions. Developers should review release notes carefully when upgrading. ↓
gotcha By default, `monosize` runs in 'batch build mode' when using this bundler, which is significantly faster. However, this can sometimes obscure issues related to individual builds. If you suspect an issue with a specific fixture's build, you might need to switch to sequential mode. ↓
gotcha The `rspackBundler` function accepts a callback to customize the rsbuild configuration. Incorrect modifications to this configuration, especially if they conflict with `monosize`'s internal bundling assumptions, can lead to incorrect size measurements or build failures. ↓
Install
npm install monosize-bundler-rspack yarn add monosize-bundler-rspack pnpm add monosize-bundler-rspack Imports
- rspackBundler wrong
const rspackBundler = require('monosize-bundler-rspack');correctimport rspackBundler from 'monosize-bundler-rspack'; - RspackBundlerConfig
import type { RspackBundlerConfig } from 'monosize-bundler-rspack'; - CustomizeConfigCallback
import type { CustomizeConfigCallback } from 'monosize-bundler-rspack';
Quickstart
/* monosize.config.mjs */
import rspackBundler from 'monosize-bundler-rspack';
/**
* @type {import('monosize').MonosizeConfig}
*/
export default {
// Define your workspace roots for monosize to scan
workspaces: [
'packages/*',
'apps/*'
],
// Specify fixtures (entry points) for size measurement
fixtures: {
'my-component': 'packages/my-component/src/index.ts',
'my-util': 'packages/my-util/src/index.ts'
},
// Integrate the Rspack bundler plugin
bundler: rspackBundler(config => {
// Example: Customize Rspack's resolve alias for specific packages
config.resolve ??= {};
config.resolve.alias = {
...config.resolve.alias,
'some-internal-lib': 'libs/some-internal-lib/dist/esm/index.js'
};
// Example: Add a new rule for handling SVG files
config.module?.rules?.push({
test: /\.svg$/,
type: 'asset/resource'
});
return config;
}),
// Other monosize configurations...
output: {
dir: './monosize-results',
filename: 'report.json'
}
};
/* Example of how to run this config in your package.json */
// "scripts": {
// "measure-size": "monosize measure"
// }