Skip to content

Commit f4446a5

Browse files
author
Luca Forstner
authored
feat(core): Implements rewrite sources for debug ID upload (#243)
1 parent fa023dc commit f4446a5

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export function sentryUnpluginFactory({
206206
releaseName: options.release.name,
207207
logger: logger,
208208
handleRecoverableError: handleRecoverableError,
209+
rewriteSourcesHook: options.sourcemaps.rewriteSources,
209210
sentryHub,
210211
sentryClient,
211212
sentryCliOptions: {

packages/bundler-plugin-core/src/plugins/debug-id-upload.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ import { promisify } from "util";
99
import { Hub, NodeClient } from "@sentry/node";
1010
import SentryCli from "@sentry/cli";
1111

12+
interface RewriteSourcesHook {
13+
(source: string, map: any): string;
14+
}
15+
1216
interface DebugIdUploadPluginOptions {
1317
logger: Logger;
1418
assets: string | string[];
1519
ignore?: string | string[];
1620
releaseName?: string;
1721
dist?: string;
22+
rewriteSourcesHook?: RewriteSourcesHook;
1823
handleRecoverableError: (error: unknown) => void;
1924
sentryHub: Hub;
2025
sentryClient: NodeClient;
@@ -40,6 +45,7 @@ export function debugIdUploadPlugin({
4045
sentryHub,
4146
sentryClient,
4247
sentryCliOptions,
48+
rewriteSourcesHook,
4349
deleteFilesAfterUpload,
4450
}: DebugIdUploadPluginOptions): UnpluginOptions {
4551
return {
@@ -75,7 +81,8 @@ export function debugIdUploadPlugin({
7581
chunkFilePath,
7682
tmpUploadFolder,
7783
String(chunkIndex),
78-
logger
84+
logger,
85+
rewriteSourcesHook ?? defaultRewriteSourcesHook
7986
);
8087
})
8188
);
@@ -124,7 +131,8 @@ export async function prepareBundleForDebugIdUpload(
124131
bundleFilePath: string,
125132
uploadFolder: string,
126133
uniqueUploadName: string,
127-
logger: Logger
134+
logger: Logger,
135+
rewriteSourcesHook: RewriteSourcesHook
128136
) {
129137
let bundleContent;
130138
try {
@@ -162,6 +170,7 @@ export async function prepareBundleForDebugIdUpload(
162170
sourceMapPath,
163171
path.join(uploadFolder, `${uniqueUploadName}.js.map`),
164172
debugId,
173+
rewriteSourcesHook,
165174
logger
166175
);
167176
}
@@ -229,6 +238,7 @@ async function prepareSourceMapForDebugIdUpload(
229238
sourceMapPath: string,
230239
targetPath: string,
231240
debugId: string,
241+
rewriteSourcesHook: RewriteSourcesHook,
232242
logger: Logger
233243
): Promise<void> {
234244
let sourceMapFileContent: string;
@@ -241,9 +251,9 @@ async function prepareSourceMapForDebugIdUpload(
241251
return;
242252
}
243253

244-
let map: Record<string, string>;
254+
let map: Record<string, unknown>;
245255
try {
246-
map = JSON.parse(sourceMapFileContent) as Record<string, string>;
256+
map = JSON.parse(sourceMapFileContent) as { sources: unknown; [key: string]: unknown };
247257
// For now we write both fields until we know what will become the standard - if ever.
248258
map["debug_id"] = debugId;
249259
map["debugId"] = debugId;
@@ -252,6 +262,10 @@ async function prepareSourceMapForDebugIdUpload(
252262
return;
253263
}
254264

265+
if (map["sources"] && Array.isArray(map["sources"])) {
266+
map["sources"].map((source: string) => rewriteSourcesHook(source, map));
267+
}
268+
255269
try {
256270
await util.promisify(fs.writeFile)(targetPath, JSON.stringify(map), {
257271
encoding: "utf8",
@@ -261,3 +275,12 @@ async function prepareSourceMapForDebugIdUpload(
261275
return;
262276
}
263277
}
278+
279+
const PROTOCOL_REGEX = /^[a-zA-Z][a-zA-Z0-9+\-.]*:\/\//;
280+
function defaultRewriteSourcesHook(source: string): string {
281+
if (source.match(PROTOCOL_REGEX)) {
282+
return source.replace(PROTOCOL_REGEX, "");
283+
} else {
284+
return path.relative(process.cwd(), path.normalize(source));
285+
}
286+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ export interface Options {
114114
* Hook to rewrite the `sources` field inside the source map before being uploaded to Sentry. Does not modify the actual source map.
115115
*
116116
* Defaults to making all sources relative to `process.cwd()` while building.
117-
*
118-
* @hidden Not yet implemented.
119117
*/
120118
rewriteSources?: (source: string, map: any) => string;
121119

0 commit comments

Comments
 (0)