Skip to content

Commit 707d345

Browse files
committed
feat(core): Add SENTRY_RELEASES variable injection
1 parent 760cc59 commit 707d345

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

packages/bundler-plugin-core/src/index.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "@sentry/tracing";
1313
import { addSpanToTransaction, captureMinimalError, makeSentryClient } from "./sentry/telemetry";
1414
import { Span, Transaction } from "@sentry/types";
1515
import { createLogger } from "./sentry/logger";
16-
import { normalizeUserOptions } from "./options-mapping";
16+
import { InternalOptions, normalizeUserOptions } from "./options-mapping";
1717
import { getSentryCli } from "./sentry/cli";
1818

1919
// We prefix the polyfill id with \0 to tell other plugins not to try to load or transform it.
@@ -185,7 +185,11 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
185185
});
186186

187187
if (id === RELEASE_INJECTOR_ID) {
188-
return generateGlobalInjectorCode({ release: internalOptions.release });
188+
return generateGlobalInjectorCode({
189+
release: internalOptions.release,
190+
org: internalOptions.org,
191+
project: internalOptions.project,
192+
});
189193
} else {
190194
return undefined;
191195
}
@@ -320,10 +324,18 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
320324
* Generates code for the "sentry-release-injector" which is responsible for setting the global `SENTRY_RELEASE`
321325
* variable.
322326
*/
323-
function generateGlobalInjectorCode({ release }: { release: string }) {
327+
function generateGlobalInjectorCode({
328+
release,
329+
org,
330+
project,
331+
}: {
332+
release: string;
333+
org?: string;
334+
project?: string;
335+
}) {
324336
// The code below is mostly ternary operators because it saves bundle size.
325337
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
326-
return `
338+
let code = `
327339
var _global =
328340
typeof window !== 'undefined' ?
329341
window :
@@ -334,6 +346,16 @@ function generateGlobalInjectorCode({ release }: { release: string }) {
334346
{};
335347
336348
_global.SENTRY_RELEASE={id:"${release}"};`;
349+
350+
if (project) {
351+
const key = org ? `${project}@${org}` : project;
352+
code += `
353+
_global.SENTRY_RELEASES=_global.SENTRY_RELEASES || {};
354+
_global.SENTRY_RELEASES["${key}"]={id:"${release}"};
355+
`;
356+
}
357+
358+
return code;
337359
}
338360

339361
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
2+
process.stdout.write(global.SENTRY_RELEASES["releasesProject@releasesOrg"].id.toString());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import childProcess from "child_process";
2+
import path from "path";
3+
4+
/**
5+
* Runs a node file in a seprate process.
6+
*
7+
* @param bundlePath Path of node file to run
8+
* @returns Stdout of the process
9+
*/
10+
function checkBundle(bundlePath: string): void {
11+
const processOutput = childProcess.execSync(`node ${bundlePath}`, { encoding: "utf-8" });
12+
expect(processOutput).toBe("I AM A RELEASE!");
13+
}
14+
15+
test("esbuild bundle", () => {
16+
expect.assertions(1);
17+
checkBundle(path.join(__dirname, "./out/esbuild/index.js"));
18+
});
19+
20+
test("rollup bundle", () => {
21+
expect.assertions(1);
22+
checkBundle(path.join(__dirname, "./out/rollup/index.js"));
23+
});
24+
25+
test("vite bundle", () => {
26+
expect.assertions(1);
27+
checkBundle(path.join(__dirname, "./out/vite/index.js"));
28+
});
29+
30+
test("webpack 4 bundle", () => {
31+
expect.assertions(1);
32+
checkBundle(path.join(__dirname, "./out/webpack4/index.js"));
33+
});
34+
35+
test("webpack 5 bundle", () => {
36+
expect.assertions(1);
37+
checkBundle(path.join(__dirname, "./out/webpack5/index.js"));
38+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Options } from "@sentry/bundler-plugin-core";
2+
import * as path from "path";
3+
import { createCjsBundles } from "../../utils/create-cjs-bundles";
4+
5+
const entryPointPath = path.resolve(__dirname, "./input/entrypoint.js");
6+
const outputDir = path.resolve(__dirname, "./out");
7+
8+
createCjsBundles({ index: entryPointPath }, outputDir, {
9+
release: "I AM A RELEASE!",
10+
project: "releasesProject",
11+
org: "releasesOrg",
12+
include: outputDir,
13+
dryRun: true,
14+
} as Options);

0 commit comments

Comments
 (0)