Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(esbuild): Add debug ID injection for esbuild #202

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/bundler-plugin-core/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"rollup.config.js",
"test/fixtures/**/*",
"sentry-release-injection-file.js",
"sentry-esbuild-debugid-injection-file.js",
],
parserOptions: {
tsconfigRootDir: __dirname,
Expand Down
9 changes: 7 additions & 2 deletions packages/bundler-plugin-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"files": [
"dist",
"sentry-release-injection-file.js"
"sentry-release-injection-file.js",
"sentry-esbuild-debugid-injection-file.js"
],
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
Expand Down Expand Up @@ -71,5 +72,9 @@
},
"engines": {
"node": ">= 10"
}
},
"sideEffects": [
"./sentry-release-injection-file.js",
"./sentry-esbuild-debugid-injection-file.js"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
try {
var globalObject =
"undefined" != typeof window
? window
: "undefined" != typeof global
? global
: "undefined" != typeof self
? self
: {};

var stack = new Error().stack;

if (stack) {
globalObject._sentryDebugIds = globalObject._sentryDebugIds || {};
globalObject._sentryDebugIds[stack] = "__SENTRY_DEBUG_ID__";
globalObject._sentryDebugIdIdentifier = "sentry-dbid-__SENTRY_DEBUG_ID__";
}
} catch (e) {}
60 changes: 53 additions & 7 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createUnplugin } from "unplugin";
import { createUnplugin, UnpluginOptions } from "unplugin";
import MagicString from "magic-string";
import { Options, BuildContext } from "./types";
import {
Expand Down Expand Up @@ -26,8 +26,8 @@ import { makeMain } from "@sentry/node";
import os from "os";
import path from "path";
import fs from "fs";
import util from "util";
import { getDependencies, getPackageJson, parseMajorVersion } from "./utils";
import { promisify } from "util";
import { getDependencies, getPackageJson, parseMajorVersion, stringToUUID } from "./utils";
import { glob } from "glob";
import { injectDebugIdSnippetIntoChunk, prepareBundleForDebugIdUpload } from "./debug-id";
import { SourceMapSource } from "webpack-sources";
Expand All @@ -39,6 +39,10 @@ const releaseInjectionFilePath = require.resolve(
"@sentry/bundler-plugin-core/sentry-release-injection-file"
);

const esbuildDebugIdInjectionFilePath = require.resolve(
"@sentry/bundler-plugin-core/sentry-esbuild-debugid-injection-file"
);

/**
* The sentry bundler plugin concerns itself with two things:
* - Release injection
Expand Down Expand Up @@ -66,7 +70,7 @@ const releaseInjectionFilePath = require.resolve(
*
* This release creation pipeline relies on Sentry CLI to execute the different steps.
*/
const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
const unplugin = createUnplugin<Options, true>((options, unpluginMetaContext) => {
const internalOptions = normalizeUserOptions(options);

const allowedToSendTelemetryPromise = shouldSendTelemetry(internalOptions);
Expand Down Expand Up @@ -113,7 +117,9 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
let transaction: Transaction | undefined;
let releaseInjectionSpan: Span | undefined;

return {
const plugins: UnpluginOptions[] = [];

plugins.push({
name: "sentry-plugin",
enforce: "pre", // needed for Vite to call resolveId hook

Expand Down Expand Up @@ -317,7 +323,31 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
})
).filter((p) => p.endsWith(".js") || p.endsWith(".mjs"));

const sourceFileUploadFolderPromise = util.promisify(fs.mkdtemp)(
if (unpluginMetaContext.framework === "esbuild") {
await Promise.all(
debugIdChunkFilePaths.map(async (debugIdChunkFilePath) => {
const chunkFileContents = await promisify(fs.readFile)(
debugIdChunkFilePath,
"utf-8"
);

const debugId = stringToUUID(chunkFileContents);

const newChunkFileContents = chunkFileContents.replace(
/__SENTRY_DEBUG_ID__/g,
debugId
);

await promisify(fs.writeFile)(
debugIdChunkFilePath,
newChunkFileContents,
"utf-8"
);
})
);
}

const sourceFileUploadFolderPromise = promisify(fs.mkdtemp)(
path.join(os.tmpdir(), "sentry-bundler-plugin-upload-")
);

Expand Down Expand Up @@ -455,7 +485,23 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
});
}
},
};
});

if (unpluginMetaContext.framework === "esbuild") {
if (internalOptions._experiments.debugIdUpload) {
plugins.push({
name: "sentry-esbuild-debug-id-plugin",
esbuild: {
setup({ initialOptions }) {
initialOptions.inject = initialOptions.inject || [];
initialOptions.inject.push(esbuildDebugIdInjectionFilePath);
},
},
});
}
}

return plugins;
});

function handleError(
Expand Down