Skip to content

Commit a83a80e

Browse files
committed
Re-add the Rolldown optimisations and fix types
1 parent 2534352 commit a83a80e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/rollup-plugin/src/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ export function _rollupPluginInternal(userOptions: Options = {}, buildTool: stri
127127

128128
function renderChunk(
129129
code: string,
130-
chunk: { fileName: string; facadeModuleId?: string | null }
130+
chunk: { fileName: string; facadeModuleId?: string | null },
131+
_?: unknown,
132+
meta?: { magicString?: MagicString }
131133
): {
132134
code: string;
133-
map: SourceMap;
135+
map?: SourceMap;
134136
} | null {
135137
if (!isJsFile(chunk.fileName)) {
136138
return null; // returning null means not modifying the chunk at all
@@ -152,8 +154,7 @@ export function _rollupPluginInternal(userOptions: Options = {}, buildTool: stri
152154
return null;
153155
}
154156

155-
const ms = new MagicString(code, { filename: chunk.fileName });
156-
157+
const ms = meta?.magicString || new MagicString(code, { filename: chunk.fileName });
157158
const match = code.match(COMMENT_USE_STRICT_REGEX)?.[0];
158159

159160
if (match) {
@@ -166,6 +167,13 @@ export function _rollupPluginInternal(userOptions: Options = {}, buildTool: stri
166167
ms.prepend(injectCode.code());
167168
}
168169

170+
// Rolldown can pass a native MagicString instance in meta.magicString
171+
// https://rolldown.rs/in-depth/native-magic-string#usage-examples
172+
if (ms?.constructor?.name === "BindingMagicString") {
173+
// Rolldown docs say to return the magic string instance directly in this case
174+
return { code: ms as unknown as string };
175+
}
176+
169177
return {
170178
code: ms.toString(),
171179
map: ms.generateMap({ file: chunk.fileName, hires: "boundary" as unknown as undefined }),
@@ -219,8 +227,8 @@ export function _rollupPluginInternal(userOptions: Options = {}, buildTool: stri
219227
};
220228
}
221229

222-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
223-
export function sentryRollupPlugin(userOptions: Options = {}) {
230+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
231+
export function sentryRollupPlugin(userOptions: Options = {}): any {
224232
// We return an array here so we don't break backwards compatibility with what
225233
// unplugin used to return
226234
return [_rollupPluginInternal(userOptions, "rollup")];

0 commit comments

Comments
 (0)