Native MagicString
Overview
experimental.nativeMagicString is an optimization feature that replaces the JavaScript-based MagicString implementation with a native Rust version, enabling source map generation in background threads for improving performance.
What is MagicString?
MagicString is a JavaScript library developed by Rich Harris (the creator of Rollup and Svelte) that provides efficient string manipulation with automatic source map generation. It's commonly used by bundlers and build tools for:
- Code transformation in plugins
- Source map generation
- Precise line/column tracking
- Efficient string operations (replace, prepend, append, etc.)
The JavaScript Implementation vs Native Rust
Traditional JavaScript MagicString
The original MagicString implementation is written in JavaScript and runs in the Node.js environment. When bundlers perform code transformations, they typically:
- Load source code as JavaScript strings
- Apply transformations using MagicString API
- Generate source maps for the transformed code
- Process everything in the main JavaScript thread
Native Rust Implementation
Rolldown's native MagicString implementation rewrites the core functionality in Rust, providing several advantages:
- Performance: Rust's memory safety and zero-cost abstractions make string operations faster
- Parallel Processing: Source map generation can happen in background threads
- Memory Efficiency: Better memory management for large codebases
- Integration: Seamless integration with Rolldown's Rust-based architecture
How It Works
When experimental.nativeMagicString is enabled, Rolldown modifies the transformation pipeline. The diagrams below show the architectural differences:
INFO
Some technical details are simplified for better illustration. The native MagicString implementation provides a magicString object in the meta parameter of transform hooks, which plugins can use just like the JavaScript version.
Without Native MagicString
(Correction in the image: "rolldown without js magic-string" should be "rolldown without native magic-string")
With Native MagicString
Key Difference: The native implementation is written in Rust, providing both Rust's performance advantages and background thread source map generation. Offloading to background threads improves overall CPU usage and enables significant performance improvements.
API Compatibility
The native implementation maintains API compatibility with the JavaScript version. The most commonly used APIs are already implemented, with the remaining APIs planned for completion in future releases.
Implemented Methods
The following MagicString methods are currently available in the native implementation:
String Manipulation:
append(content)- Appends content to the end of the stringprepend(content)- Prepends content to the beginning of the stringappendLeft(index, content)- Appends content to the left of a specific indexappendRight(index, content)- Appends content to the right of a specific indexprependLeft(index, content)- Prepends content to the left of a specific indexprependRight(index, content)- Prepends content to the right of a specific indexoverwrite(start, end, content)- Replaces content in a rangeupdate(start, end, content)- Updates content in a rangeremove(start, end)- Removes content in a rangereplace(from, to)- Replaces the first occurrence of a patternreplaceAll(from, to)- Replaces all occurrences of a pattern
Transformations:
indent(indentor?)- Indents the content with optional custom indentation stringrelocate(start, end, to)- Moves content from one position to another
Utilities:
toString()- Returns the transformed stringhasChanged()- Checks if the string has been modifiedlength()- Returns the length of the transformed stringisEmpty()- Checks if the string is emptyclone()- Returns a clone of the MagicString instancetrim(charType?)- Trims whitespace or specified characters from both endstrimStart(charType?)- Trims whitespace or specified characters from the starttrimEnd(charType?)- Trims whitespace or specified characters from the endtrimLines()- Trims newlines from both endssnip(start, end)- Returns a clone with content outside the range removedslice(start?, end?)- Returns content between positionsreset(start, end)- Resets a range to its original contentlastChar()- Returns the last characterlastLine()- Returns the content after the last newline
Source Map Generation:
addSourcemapLocation(index)registers a UTF-16 index in the original source for maps withhires: false. Theoffsetoption does not change this index. ThesourcemapLocationsproperty returns each registered index once, in ascending order.generateMap(options?)- Generates a source map object. ItstoString()returns the map as a JSON string, and itstoUrl()returns the map as a data URLoptions.source- Source file nameoptions.includeContent- Include original source in the mapoptions.hires- High-resolution mode:true,false, or"boundary". The default is thehiresoption of the instance, orfalsewhen that option is not set
generateDecodedMap(options?)- Generates a source map object with decodedmappings. Itsmappingsis an array with one array of segments for each line. It takes the same options asgenerateMap
The maps from generateMap() and generateDecodedMap() include trailing output lines with no mapping segments.
The constructor also accepts a hires option, which magic-string does not have: new RolldownMagicString(code, { hires: false }). It sets the resolution of the source maps that are generated from the instance.
When a transform or renderChunk hook returns the native MagicString without a map, Rolldown generates the map for that step. It uses the hires option of the MagicString, or "boundary" when the option is not set. With "boundary", the output source map keeps column positions and names after that map is composed with the others in the chain. With hires: false, the map for that step is smaller and faster to generate, because it has fewer segments. With hires: true, the map for that step is larger and slower to generate. The output source map usually has the same positions as with "boundary", because the maps that Rolldown generates when it prints modules and chunks have segments at the start of tokens, and "boundary" has a segment at the start of each word and at each character that is not a letter, a digit or _, for example ", [ or !. In renderChunk, hires: true can also make the output source map larger.
MagicString starts with one piece of text. An edit can split a piece. If you insert an empty string inside a piece, MagicString splits that piece.
For hires: false, the source map includes segments according to these rules. A newline is \n.
- The map includes a segment at the first non-newline character on each line of an unedited piece. This rule also applies if the piece starts partway through a line.
- The map includes a segment for each index from
addSourcemapLocation()that falls within a non-newline character in an unedited piece. - The map includes a segment at the start of each line of replacement text. Each segment points to the start of the replaced range in the original source.
The map includes no segments for inserted text.
When Rolldown composes the maps, a position resolves to the last segment at or before it on its line. If the position is before the first segment of its line, it resolves to that first segment. If its line has no segment, the position has no mapping.
To use a different resolution, create the MagicString with the hires option. meta.magicString has no options, because Rolldown creates it, so create your own instance from code. With this option, Rolldown still generates the transform map on a background thread when experimental.nativeMagicString and source maps are on.
You can also return your own map with the MagicString, for example { code: magicString, map: magicString.generateMap({ hires: 'boundary' }) }. Rolldown then uses your map and does not generate one, but your hook generates that map on the JS thread. map: null tells Rolldown that the step did not move code, so Rolldown does not generate a map for it. If the step moved code, the output source map points to wrong positions.
Real-World Performance
use rolldown/benchmarks as benchmark cases
Build time
| Runs | oxc raw transfer + js magicString | oxc raw transfer + native magicString | Time Saved | Speedup |
|---|---|---|---|---|
| apps/1000 | 497.6 ms | 431.1 ms | 66.5 ms | 1.15x |
| apps/5000 | 1.100 s | 894.5 ms | 205.5 ms | 1.23x |
| apps/10000 | 1.814 s | 1.368 s | 446.0 ms | 1.33x |
Plugin transform time (build time - noop plugin build time)
| Runs | Transform Time (oxc raw transfer + js magicString) | Transform Time (oxc raw transfer + native magicString) | Time Saved | Speedup |
|---|---|---|---|---|
| 1000 | 172.0 ms | 105.5 ms | 66.5 ms | 1.63x |
| 5000 | 455.4 ms | 249.9 ms | 205.5 ms | 1.82x |
| 10000 | 799.0 ms | 353.0 ms | 446.0 ms | 2.26x |
For detailed benchmark results, see the benchmark pull request.
Usage Examples
Basic Plugin with Native MagicString
import { defineConfig } from 'rolldown';
export default defineConfig({
experimental: {
nativeMagicString: true,
},
output: {
sourcemap: true,
},
plugins: [
{
name: 'transform-example',
transform(code, id, meta) {
if (!meta?.magicString) {
// Fallback when nativeMagicString is not available
return null;
}
const { magicString } = meta;
// Example transformation: Add debug comments
if (code.includes('console.log')) {
magicString.replace(/console\.log\(/g, 'console.log("[DEBUG]", ');
}
// Example: Add file header
magicString.prepend(`// Transformed from: ${id}\n`);
return {
code: magicString,
};
},
},
],
});Compatibility and Fallbacks
Checking for Native MagicString Availability
transform(code, id, meta) {
if (meta?.magicString) {
// Native MagicString is available
const { magicString } = meta;
// Use the native implementation
// Note: Return the magicString object directly, not a string
return {
code: magicString
};
} else {
// Fallback to regular string manipulation
// or use the JavaScript MagicString library
const MagicString = require('magic-string');
const ms = new MagicString(code);
// Your transformations here...
return {
code: ms.toString(),
map: ms.generateMap()
};
}
}Rollup Compatibility
This feature is Rolldown-specific and not available in Rollup. For plugins that need to work with both bundlers:
function createTransform() {
return function (code, id, meta) {
if (meta?.magicString) {
// Rolldown with native MagicString
return transformWithNativeMagicString(code, id, meta);
} else {
// Rollup or Rolldown without native MagicString
return transformWithJsMagicString(code, id);
}
};
}TIP
You can use rolldown-string, which provides a unified interface that works with both bundlers.
When to Use Native MagicString
Recommended Scenarios
- Large Codebases: Projects with hundreds or thousands of files
- Complex Transformations: Plugins that perform extensive code manipulation
- Source Map Intensive: Projects requiring detailed source maps
- Performance-Critical: Build processes where speed is crucial
- Development Mode: Faster rebuild times during development
When to Be Cautious
- Experimental Feature: As an experimental feature, API may change
- Plugin Compatibility: Some plugins may expect specific JavaScript MagicString behavior
- Debugging: Native implementation may have different error messages
Migration Guide
Enabling Native MagicString
- Update Configuration:
export default {
experimental: {
nativeMagicString: true,
},
output: {
sourcemap: true, // Required for source map generation
},
};- Update Plugins:
// Before
transform(code, id) {
const ms = new MagicString(code);
// ... transformations
return { code: ms.toString(), map: ms.generateMap() };
}
// After
transform(code, id, meta) {
if (meta?.magicString) {
const { magicString } = meta;
// ... transformations (same API)
return { code: magicString };
}
// Fallback logic
}Limitations and Considerations
Current Limitations
- Experimental Status: API may change in future versions
- Edge Cases: Some edge cases may behave differently from JavaScript version
- Debugging: Error messages may be less familiar
Best Practices
- Always Check Availability: Verify
meta?.magicStringexists before using - Provide Fallbacks: Include fallback logic for compatibility
- Test Thoroughly: Test transformations with both implementations
- Report Issues: Report any behavior differences to the Rolldown team
Conclusion
experimental.nativeMagicString represents a significant performance optimization for Rolldown by leveraging Rust's efficiency for code transformation tasks. While it requires some considerations for compatibility, the performance benefits make it an attractive option for large-scale projects and performance-critical build processes.
As an experimental feature, it's recommended to test thoroughly in development environments before adopting in production workflows. The Rolldown team is actively working on this feature, and feedback from the community is valuable for its continued development.