examples/native-magic-string/README.md
This example demonstrates the use of experimental.nativeMagicString in Rolldown.
The nativeMagicString option enables a native Rust implementation of MagicString for source map generation and code transformation. This provides significant performance improvements over the JavaScript implementation, especially for large codebases.
When experimental.nativeMagicString is enabled:
magicString object in the meta parameter of transform hooksnpm run build
This will:
dist/ directoryThe example includes two plugins demonstrating different use cases:
example-transform (MagicString only)Demonstrates basic MagicString operations:
ast-magicstring-example (AST + MagicString)Demonstrates combining meta.ast and meta.magicString for code transformation:
oxc parser.fn(() => import(url)) into fn(() => import(url), url) with native magicStringmeta.ast to find specific code patternsmeta.magicString to modify code at precise locationsExample transformation in lazy-loader.js:
// Before:
registerLazyModule(() => import('./greet.js'));
// After:
registerLazyModule(() => import('./greet.js'), './greet.js');
All transformations maintain accurate source maps thanks to the native MagicString implementation.
export default defineConfig({
experimental: {
nativeMagicString: true, // Enable native Rust implementation
},
output: {
sourcemap: true, // Enable source map generation
},
});