Skip to content

Commit 62e4483

Browse files
committed
fix: record name on .bundler before calling bridge
1 parent a95cf64 commit 62e4483

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

packages/server-utils/src/orchestrion/bundler/webpack-loader.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface LoaderContext {
1515

1616
type LoaderFn = (this: LoaderContext, code: string, inputSourceMap?: unknown) => void;
1717

18-
const upstreamLoader = codeTransformerLoaderImpl as unknown as LoaderFn;
18+
const upstreamLoader: LoaderFn = codeTransformerLoaderImpl;
1919

2020
/**
2121
* The npm package name for a module path.
@@ -40,15 +40,18 @@ function packageNameFromPath(resourcePath: string): string | undefined {
4040
}
4141

4242
/**
43-
* Announce a runtime-injected module the way the runtime `--import` hook does,
44-
* so the lazily-registered channel integrations subscribe. Appended to each
45-
* transformed module's code, it runs when that module loads.
43+
* Announce a runtime-injected module the way the banner and runtime `--import`
44+
* hook do, so the lazily-registered channel integrations subscribe. Appended to
45+
* each transformed module's code, it runs when that module loads.
4646
*/
4747
function onInjectSnippet(moduleName: string): string {
48+
const name = JSON.stringify(moduleName);
4849
return (
4950
';(function(){' +
50-
'var g=globalThis.__SENTRY_ORCHESTRION__;' +
51-
`if(g&&typeof g.onInject==='function')g.onInject(${JSON.stringify(moduleName)});` +
51+
'var g=globalThis.__SENTRY_ORCHESTRION__=globalThis.__SENTRY_ORCHESTRION__||{};' +
52+
'if(!Array.isArray(g.bundler))g.bundler=[];' +
53+
`if(g.bundler.indexOf(${name})<0)g.bundler.push(${name});` +
54+
`if(typeof g.onInject==='function')g.onInject(${name});` +
5255
'})();\n'
5356
);
5457
}

packages/server-utils/test/orchestrion/webpack-loader.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ describe('orchestrion webpack/Turbopack loader', () => {
3737
expect(output).toContain('g.onInject("ioredis")');
3838
});
3939

40+
it('records the module on `.bundler` even when the bridge is not installed yet', () => {
41+
const output = runLoader({ resourcePath: IOREDIS }, 'code') as string;
42+
const snippet = output.slice('code;//transformed'.length);
43+
44+
// Bridge absent (early load): records `.bundler`, no throw.
45+
const early: { __SENTRY_ORCHESTRION__?: { bundler?: string[]; onInject?: (name: string) => void } } = {};
46+
new Function('globalThis', snippet)(early);
47+
expect(early.__SENTRY_ORCHESTRION__?.bundler).toEqual(['ioredis']);
48+
49+
// Bridge present (loaded after init): records AND fires the bridge.
50+
const injected: string[] = [];
51+
const late = { __SENTRY_ORCHESTRION__: { onInject: (name: string) => injected.push(name) } };
52+
new Function('globalThis', snippet)(late);
53+
expect(injected).toEqual(['ioredis']);
54+
});
55+
4056
it('does not append when webpack runs the plugin banner (compilation present)', () => {
4157
const output = runLoader({ resourcePath: IOREDIS, _compilation: {} }, 'code');
4258

0 commit comments

Comments
 (0)