Skip to content

Commit fb0de6d

Browse files
committed
Fix sources: ["0"], output the correct source
Case 1, sourcemap missing In [terser-js](https://github.com/terser-js/terser#source-map-options), sources are always 0 if old sourcemaps are not provided. The value passed for sourceMap.url is only used to set //# sourceMappingURL=out.js.map in result.code. The value of filename is only used to set file attribute in source map file. In broccoli-uglify-sourcemap we know in this case we are generating sourcemap for the file we are processing, changing 0 to the actual file gives us the correct source. Case2, multiple sourceMap comments source-map-url only matches the very first magic comment so we mistakenly thinks the js file doesn't have a valid sourcemap.
1 parent 3f10b7d commit fb0de6d

File tree

5 files changed

+121
-28
lines changed

5 files changed

+121
-28
lines changed

lib/get-sourcemap-content.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const srcURL = require('source-map-url');
6+
const srcRegExpg = new RegExp(srcURL.regex, 'g');
7+
8+
module.exports = function getSourceMapContent(src, inFile, relativePath, silent) {
9+
let urls = [];
10+
let match;
11+
// eslint-disable-next-line no-cond-assign
12+
while (match = srcRegExpg.exec(src)) {
13+
urls.push(match[1] || match[2] || '');
14+
}
15+
if (urls.length) {
16+
for (let i = urls.length - 1; i >= 0; --i) {
17+
let sourceMapPath = path.join(path.dirname(inFile), urls[i]);
18+
if (fs.existsSync(sourceMapPath)) {
19+
return JSON.parse(fs.readFileSync(sourceMapPath));
20+
}
21+
}
22+
if (!silent) {
23+
console.warn(`[WARN] (broccoli-uglify-sourcemap) ${urls.map(u => `"${u}"`).join(', ')} referenced in "${relativePath}" could not be found`);
24+
}
25+
}
26+
};

lib/process-file.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const defaults = require('lodash.defaultsdeep');
55
const fs = require('fs');
66
const mkdirp = require('mkdirp');
77
const path = require('path');
8-
const srcURL = require('source-map-url');
8+
const getSourceMapContent = require('./get-sourcemap-content');
99

1010
const terser = require('terser');
1111

@@ -38,14 +38,9 @@ module.exports = function processFile(inFile, outFile, relativePath, outDir, sil
3838

3939
let sourceMap = { filename, url };
4040

41-
if (srcURL.existsIn(src)) {
42-
let url = srcURL.getFrom(src);
43-
let sourceMapPath = path.join(path.dirname(inFile), url);
44-
if (fs.existsSync(sourceMapPath)) {
45-
sourceMap.content = JSON.parse(fs.readFileSync(sourceMapPath));
46-
} else if (!silent) {
47-
console.warn(`[WARN] (broccoli-uglify-sourcemap) "${url}" referenced in "${relativePath}" could not be found`);
48-
}
41+
let content = getSourceMapContent(src, inFile, relativePath, silent);
42+
if (content) {
43+
sourceMap.content = content;
4944
}
5045

5146
options = defaults(options, { sourceMap });
@@ -69,13 +64,25 @@ module.exports = function processFile(inFile, outFile, relativePath, outDir, sil
6964

7065
if (options.sourceMap) {
7166
let newSourceMap = JSON.parse(result.map);
67+
debug('[newSourceMap] %O', newSourceMap);
7268

7369
newSourceMap.sources = newSourceMap.sources.map(function(path) {
74-
// If out output file has the same name as one of our original
75-
// sources, they will shadow eachother in Dev Tools. So instead we
76-
// alter the reference to the upstream file.
7770
if (path === relativePath) {
78-
path = path.replace(/\.js$/, '-orig.js');
71+
// If out output file has the same name as one of our original
72+
// sources, they will shadow eachother in Dev Tools. So instead we
73+
// alter the reference to the upstream file.
74+
return path.replace(/\.js$/, '-orig.js');
75+
} else if (path === '0') {
76+
// In [terser-js](https://github.com/terser-js/terser#source-map-options),
77+
// sources are always 0 if old sourcemaps are not provided.
78+
// The value passed for `sourceMap.url` is only used to set
79+
// `//# sourceMappingURL=out.js.map` in `result.code`.
80+
// The value of `filename` is only used to set `file` attribute
81+
// in source map file.
82+
// In broccoli-uglify-sourcemap we know in this case we are generating
83+
// sourcemap for the file we are processing, changing 0 to the actual
84+
// file gives us the correct source.
85+
return relativePath;
7986
}
8087
return path;
8188
});

0 commit comments

Comments
 (0)