Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -333,9 +340,21 @@ export function createRollupInjectionHooks(
ms.prepend(codeToInject);
}

// 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") {
Copy link
Collaborator

@logaretm logaretm Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: could this check fail in the future since this is experimental? we could check meta.magicString again here instead. WDYT?

Copy link
Collaborator Author

@timfish timfish Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to return { code: magicStringInstance as string } if this is a Rolldown native instance. The value of meta.magicString doesn't matter at this point.

// 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",
});
},
};
},
};
Expand Down
Loading