Skip to content
Merged
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
41 changes: 36 additions & 5 deletions packages/esbuild-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,57 @@ function esbuildReleaseInjectionPlugin(injectionCode: string): UnpluginOptions {

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

return {
name: pluginName,

esbuild: {
setup({ initialOptions, onLoad, onResolve }) {
initialOptions.inject = initialOptions.inject || [];
initialOptions.inject.push(virtualReleaseInjectionFilePath);
setup({ onLoad, onResolve }) {
onResolve({ filter: /.*/ }, (args) => {
if (args.kind !== "entry-point") {
return;
}
return {
pluginName,
path: args.path,
namespace: proxyNamespace,
pluginData: {
originalPath: args.path,
originalResolveDir: args.resolveDir,
},
};
});

onLoad({ filter: /.*/, namespace: proxyNamespace }, (args) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const originalPath = args.pluginData.originalPath as string;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const originalResolveDir = args.pluginData.originalResolveDir as string;
return {
loader: "js",
pluginName,
contents: `
import "_sentry-debug-id-injection-stub";
import * as OriginalModule from "${originalPath}";
export default OriginalModule.default;
Copy link
Member

Choose a reason for hiding this comment

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

just sanity checking, does this also work if the original module has no default export?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I made sure to check this. The output code will look as follows in these situations:

var d = typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {};
d.SENTRY_RELEASE = { id: "0cd3502" };
(function () {
  try {
    var e =
        typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {},
      n = new Error().stack;
    n &&
      ((e._sentryDebugIds = e._sentryDebugIds || {}),
      (e._sentryDebugIds[n] = "e426cce9-c1fa-4eea-aa91-50e49ab0f23d"),
      (e._sentryDebugIdIdentifier = "sentry-dbid-e426cce9-c1fa-4eea-aa91-50e49ab0f23d"));
  } catch {}
})();
function o() {
  return global.SENTRY_RELEASE;
}
function t() {
  console.log("Hello world!");
}
console.log("entrypoint1.js loaded");
function p() {
  console.log("called main (entrypoint1.js)"), t();
}
console.log("global:", o());
var w = void 0;
export { w as default, o as getGlobal, p as main };
//# sourceMappingURL=entrypoint1.js.map

export * from "${originalPath}";`,
resolveDir: originalResolveDir,
};
});

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

onLoad({ filter: /_sentry-debug-id-injection-stub/ }, () => {
onLoad({ filter: /_sentry-debug-id-injection-stub/, namespace: stubNamespace }, () => {
return {
loader: "js",
pluginName,
Expand Down