Skip to content

Commit f49cc63

Browse files
Alexandre SoroLuca Forstner
andauthored
feat(core): Add injectRelease and uploadSourceMaps options (#190)
Co-authored-by: Luca Forstner <[email protected]>
1 parent d3261df commit f49cc63

File tree

13 files changed

+117
-5
lines changed

13 files changed

+117
-5
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
225225
async transform(code, id) {
226226
logger.debug('Called "transform":', { id });
227227

228+
if (!internalOptions.injectRelease) {
229+
return;
230+
}
231+
228232
// The MagicString library allows us to generate sourcemaps for the changes we make to the user code.
229233
const ms = new MagicString(code);
230234

packages/bundler-plugin-core/src/options-mapping.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type RequiredInternalOptions = Required<
1313
| "telemetry"
1414
| "injectReleasesMap"
1515
| "_experiments"
16+
| "injectRelease"
17+
| "uploadSourceMaps"
1618
>
1719
>;
1820

@@ -90,6 +92,8 @@ export function normalizeUserOptions(userOptions: UserOptions): InternalOptions
9092
silent: userOptions.silent ?? false,
9193
telemetry: userOptions.telemetry ?? true,
9294
injectReleasesMap: userOptions.injectReleasesMap ?? false,
95+
injectRelease: userOptions.injectRelease ?? true,
96+
uploadSourceMaps: userOptions.uploadSourceMaps ?? true,
9397
_experiments: userOptions._experiments ?? {},
9498

9599
// These options and can also be set via env variables or the config file.

packages/bundler-plugin-core/src/sentry/releasePipeline.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export async function uploadSourceMaps(
6161
ctx: BuildContext,
6262
releaseName: string
6363
): Promise<void> {
64+
if (!options.uploadSourceMaps) {
65+
logger.debug("Skipping source maps upload.");
66+
return;
67+
}
68+
6469
const span = addSpanToTransaction(ctx, "function.plugin.upload_sourcemaps");
6570
ctx.logger.info("Uploading Sourcemaps.");
6671

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ export type Options = Omit<IncludeEntry, "paths"> & {
190190
*/
191191
configFile?: string;
192192

193+
/**
194+
* Whether the plugin should inject release information into the build.
195+
*
196+
* Defaults to `true`.
197+
*/
198+
injectRelease?: boolean;
199+
193200
/**
194201
* If set to true, the plugin will inject an additional `SENTRY_RELEASES` variable that
195202
* maps from `{org}@{project}` to the `release` value. This might be helpful for webpack
@@ -199,6 +206,13 @@ export type Options = Omit<IncludeEntry, "paths"> & {
199206
*/
200207
injectReleasesMap?: boolean;
201208

209+
/**
210+
* Whether the plugin should upload source maps to Sentry.
211+
*
212+
* Defaults to `true`.
213+
*/
214+
uploadSourceMaps?: boolean;
215+
202216
/**
203217
* These options are considered experimental and subject to change.
204218
*

packages/bundler-plugin-core/test/option-mappings.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ describe("normalizeUserOptions()", () => {
3333
release: "my-release",
3434
silent: false,
3535
telemetry: true,
36+
injectRelease: true,
3637
injectReleasesMap: false,
38+
uploadSourceMaps: true,
3739
_experiments: {},
3840
url: "https://sentry.io",
3941
});
@@ -77,7 +79,9 @@ describe("normalizeUserOptions()", () => {
7779
release: "my-release",
7880
silent: false,
7981
telemetry: true,
82+
injectRelease: true,
8083
injectReleasesMap: false,
84+
uploadSourceMaps: true,
8185
_experiments: {},
8286
url: "https://sentry.io",
8387
});

packages/bundler-plugin-core/test/sentry/releasePipeline.test.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InternalOptions } from "../../src/options-mapping";
1+
import { InternalOptions, normalizeUserOptions } from "../../src/options-mapping";
22
import {
33
addDeploy,
44
cleanArtifacts,
@@ -93,16 +93,32 @@ describe("Release Pipeline", () => {
9393
});
9494

9595
describe("uploadSourceMaps", () => {
96-
it("makes a call to Sentry CLI's sourcemaps upload command", async () => {
97-
const options = {
96+
it("doesn't do anything if uploadSourceMaps option is `false`", async () => {
97+
const options = normalizeUserOptions({
98+
uploadSourceMaps: false,
9899
include: [{ paths: ["dist"] }],
99-
} as InternalOptions;
100+
});
100101

101102
await uploadSourceMaps(options, ctx as unknown as BuildContext, "1.0.0");
102103

103-
expect(mockedCLI.releases.uploadSourceMaps).toHaveBeenCalledWith("1.0.0", {
104+
expect(mockedCLI.releases.uploadSourceMaps).not.toHaveBeenCalled();
105+
expect(mockedAddSpanToTxn).not.toHaveBeenCalled();
106+
expect(mockedChildSpan.finish).not.toHaveBeenCalled();
107+
});
108+
109+
it("makes a call to Sentry CLI's sourcemaps upload command", async () => {
110+
const options = normalizeUserOptions({
104111
include: [{ paths: ["dist"] }],
105112
});
113+
114+
await uploadSourceMaps(options, ctx as unknown as BuildContext, "1.0.0");
115+
116+
expect(mockedCLI.releases.uploadSourceMaps).toHaveBeenCalledWith(
117+
"1.0.0",
118+
expect.objectContaining({
119+
include: [expect.objectContaining({ paths: ["dist"] })],
120+
})
121+
);
106122
expect(mockedAddSpanToTxn).toHaveBeenCalledWith(ctx, "function.plugin.upload_sourcemaps");
107123
expect(mockedChildSpan.finish).toHaveBeenCalled();
108124
});

packages/esbuild-plugin/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ The Sentry Esbuild Plugin takes an options argument with the following propertie
8383
| errorHandler | `function(err: Error): void` | optional | When an error occurs during rlease creation or sourcemaps upload, the plugin will call this function. By default, the plugin will simply throw an error, thereby stopping the bundling process. If an `errorHandler` callback is provided, compilation will continue, unless an error is thrown in the provided callback. |
8484
| setCommits | `Object` | optional | Associates the release with its commits in Sentry. See [table below](#setCommits) for details. |
8585
| deploy | `Object` | optional | Adds deployment information to the release in Sentry. See [table below](#deploy) for details. |
86+
| injectRelease | `boolean` | optional | Whether the plugin should inject release information into the build. Defaults to `true`. |
8687
| injectReleasesMap | `boolean` | optional | If set to true, the plugin will inject an additional `SENTRY_RELEASES` variable that maps from `{org}@{project}` to the `release` value. This might be helpful for webpack module federation or micro frontend setups. Defaults to `false`. |
88+
| uploadSourceMaps | `boolean` | optional | Whether the plugin should upload source maps to Sentry. Defaults to `true`. |
8789
| telemetry | `boolean` | optional | If set to true, internal plugin errors and performance data will be sent to Sentry. At Sentry we like to use Sentry ourselves to deliver faster and more stable products. We're very careful of what we're sending. We won't collect anything other than error and high-level performance data. We will never collect your code or any details of the projects in which you're using this plugin. Defaults to `true`. |
8890
| \_experiments.injectBuildInformation | `boolean` | optional | If set to true, the plugin will inject an additional `SENTRY_BUILD_INFO` variable that contains information about the build that can be used to improve SDK functionality. Defaults to `false`. |
8991

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import childProcess from "child_process";
2+
import path from "path";
3+
import { testIfNodeMajorVersionIsLessThan18 } from "../../utils/testIf";
4+
5+
/**
6+
* Runs a node file in a seprate process.
7+
*
8+
* @param bundlePath Path of node file to run
9+
* @returns Stdout of the process
10+
*/
11+
function checkBundle(bundlePath: string): void {
12+
const processOutput = childProcess.execSync(`node ${bundlePath}`, { encoding: "utf-8" });
13+
expect(processOutput).toBe("");
14+
}
15+
16+
test("esbuild bundle", () => {
17+
expect.assertions(1);
18+
checkBundle(path.join(__dirname, "out", "esbuild", "index.js"));
19+
});
20+
21+
test("rollup bundle", () => {
22+
expect.assertions(1);
23+
checkBundle(path.join(__dirname, "out", "rollup", "index.js"));
24+
});
25+
26+
test("vite bundle", () => {
27+
expect.assertions(1);
28+
checkBundle(path.join(__dirname, "out", "vite", "index.js"));
29+
});
30+
31+
testIfNodeMajorVersionIsLessThan18("webpack 4 bundle", () => {
32+
expect.assertions(1);
33+
checkBundle(path.join(__dirname, "out", "webpack4", "index.js"));
34+
});
35+
36+
test("webpack 5 bundle", () => {
37+
expect.assertions(1);
38+
checkBundle(path.join(__dirname, "out", "webpack5", "index.js"));
39+
});
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_RELEASE || {}).id || "");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
injectRelease: false,
15+
injectReleasesMap: true,
16+
} as Options);

0 commit comments

Comments
 (0)