From 5e5506b533f509786680d6e2d6005fe55a903f15 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 20 Jan 2026 13:29:55 +0100 Subject: [PATCH 1/2] feat: Use Rolldown native `MagicString` --- packages/bundler-plugin-core/src/index.ts | 28 +++++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index 555de622..ef1c848b 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -232,10 +232,12 @@ type RenderChunkHook = ( chunk: { fileName: string; facadeModuleId?: string | null; - } + }, + outputOptions?: unknown, + meta?: { magicString?: MagicString } ) => { code: string; - map: SourceMap; + readonly map?: SourceMap; } | null; /** @@ -292,7 +294,12 @@ export function createRollupInjectionHooks( renderChunk: RenderChunkHook; } { return { - renderChunk(code: string, chunk: { fileName: string; facadeModuleId?: string | null }) { + renderChunk( + code: string, + chunk: { fileName: string; facadeModuleId?: string | null }, + _?: unknown, + meta?: { magicString?: MagicString } + ) { if (!isJsFile(chunk.fileName)) { return null; // returning null means not modifying the chunk at all } @@ -320,7 +327,7 @@ export function createRollupInjectionHooks( } } - const ms = new MagicString(code, { filename: chunk.fileName }); + const ms = meta?.magicString || new MagicString(code, { filename: chunk.fileName }); const match = code.match(COMMENT_USE_STRICT_REGEX)?.[0]; if (match) { @@ -333,9 +340,20 @@ export function createRollupInjectionHooks( ms.prepend(codeToInject); } + // Rolldown can pass a native MagicString instance in meta.magicString + if (ms?.constructor?.name === "BindingMagicString") { + // Rolldown docs say to return the magic string instance directly in this case + return { code: ms as unknown as string }; + } + return { code: ms.toString(), - map: ms.generateMap({ file: chunk.fileName, hires: "boundary" }), + get map() { + return ms.generateMap({ + file: chunk.fileName, + hires: "boundary", + }); + }, }; }, }; From 3190a92d391dcb2cff639ad373d8d0e693b7687e Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 23 Jan 2026 18:15:08 +0100 Subject: [PATCH 2/2] Add docs link --- packages/bundler-plugin-core/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index ef1c848b..a9b78c45 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -341,6 +341,7 @@ export function createRollupInjectionHooks( } // Rolldown can pass a native MagicString instance in meta.magicString + // https://rolldown.rs/in-depth/native-magic-string#usage-examples if (ms?.constructor?.name === "BindingMagicString") { // Rolldown docs say to return the magic string instance directly in this case return { code: ms as unknown as string };