Skip to content

Commit 2a22ed8

Browse files
author
Luca Forstner
authored
fix(esbuild): Inject different debug IDs into different output bundles (#301)
1 parent 015d8fb commit 2a22ed8

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

packages/esbuild-plugin/src/index.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,57 @@ function esbuildReleaseInjectionPlugin(injectionCode: string): UnpluginOptions {
3838

3939
function esbuildDebugIdInjectionPlugin(): UnpluginOptions {
4040
const pluginName = "sentry-esbuild-debug-id-injection-plugin";
41-
const virtualReleaseInjectionFilePath = path.resolve("_sentry-debug-id-injection-stub"); // needs to be an absolute path for older eslint versions
41+
const proxyNamespace = "sentry-debug-id-proxy";
42+
const stubNamespace = "sentry-debug-id-stub";
4243

4344
return {
4445
name: pluginName,
4546

4647
esbuild: {
47-
setup({ initialOptions, onLoad, onResolve }) {
48-
initialOptions.inject = initialOptions.inject || [];
49-
initialOptions.inject.push(virtualReleaseInjectionFilePath);
48+
setup({ onLoad, onResolve }) {
49+
onResolve({ filter: /.*/ }, (args) => {
50+
if (args.kind !== "entry-point") {
51+
return;
52+
}
53+
return {
54+
pluginName,
55+
path: args.path,
56+
namespace: proxyNamespace,
57+
pluginData: {
58+
originalPath: args.path,
59+
originalResolveDir: args.resolveDir,
60+
},
61+
};
62+
});
63+
64+
onLoad({ filter: /.*/, namespace: proxyNamespace }, (args) => {
65+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
66+
const originalPath = args.pluginData.originalPath as string;
67+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
68+
const originalResolveDir = args.pluginData.originalResolveDir as string;
69+
return {
70+
loader: "js",
71+
pluginName,
72+
contents: `
73+
import "_sentry-debug-id-injection-stub";
74+
import * as OriginalModule from "${originalPath}";
75+
export default OriginalModule.default;
76+
export * from "${originalPath}";`,
77+
resolveDir: originalResolveDir,
78+
};
79+
});
5080

5181
onResolve({ filter: /_sentry-debug-id-injection-stub/ }, (args) => {
5282
return {
5383
path: args.path,
5484
sideEffects: true,
5585
pluginName,
86+
namespace: stubNamespace,
5687
suffix: "?sentry-module-id=" + uuidv4(), // create different module, each time this is resolved
5788
};
5889
});
5990

60-
onLoad({ filter: /_sentry-debug-id-injection-stub/ }, () => {
91+
onLoad({ filter: /_sentry-debug-id-injection-stub/, namespace: stubNamespace }, () => {
6192
return {
6293
loader: "js",
6394
pluginName,

0 commit comments

Comments
 (0)