Skip to content

Commit 222ea3d

Browse files
authored
Merge pull request #9 from dfreeman/match-maps-to-js-files
Match sourcemaps to specific JS files
2 parents c0bf3d3 + d3561a7 commit 222ea3d

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ module.exports = {
5151
var overwrite = this.readConfig('overwrite');
5252
var includeAppVersion = this.readConfig('includeAppVersion');
5353
var promises = [];
54-
var jsFilePaths = fetchFilePaths(distFiles, publicUrl, 'js');
55-
var mapFilePaths = fetchFilePaths(distFiles, distDir, 'map');
54+
var jsMapPairs = fetchJSMapPairs(distFiles, publicUrl, distDir);
5655
log('Uploading sourcemaps to bugsnag', { verbose: true });
5756

58-
for (var i = 0; i < mapFilePaths.length; i++) {
59-
var mapFilePath = mapFilePaths[i];
60-
var jsFilePath = jsFilePaths[i];
57+
for (var i = 0; i < jsMapPairs.length; i++) {
58+
var mapFilePath = jsMapPairs[i].mapFile;
59+
var jsFilePath = jsMapPairs[i].jsFile;
6160
var formData = {
6261
apiKey: apiKey,
6362
overwrite: overwrite,
@@ -124,6 +123,27 @@ module.exports = {
124123
}
125124
};
126125

126+
function fetchJSMapPairs(distFiles, publicUrl, distUrl) {
127+
var jsFiles = indexByBaseFilename(fetchFilePaths(distFiles, '', 'js'));
128+
return fetchFilePaths(distFiles, '', 'map').map(function(mapFile) {
129+
return {
130+
mapFile: distUrl + mapFile,
131+
jsFile: publicUrl + jsFiles[getBaseFilename(mapFile)]
132+
};
133+
});
134+
}
135+
136+
function indexByBaseFilename(files) {
137+
return files.reduce(function(result, file) {
138+
result[getBaseFilename(file)] = file;
139+
return result;
140+
}, {});
141+
}
142+
143+
function getBaseFilename(file) {
144+
return file.replace(/-[0-9a-f]+\.(js|map)$/, '');
145+
}
146+
127147
function fetchFilePaths(distFiles, basePath, type) {
128148
return distFiles.filter(function(filePath) {
129149
return new RegExp('assets\/.*\\.' + type + '$').test(filePath);

0 commit comments

Comments
 (0)