@@ -9,12 +9,17 @@ import { promisify } from "util";
99import { Hub , NodeClient } from "@sentry/node" ;
1010import SentryCli from "@sentry/cli" ;
1111
12+ interface RewriteSourcesHook {
13+ ( source : string , map : any ) : string ;
14+ }
15+
1216interface 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 - z A - Z ] [ a - z A - Z 0 - 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+ }
0 commit comments