Skip to content

Commit 7183703

Browse files
authored
Merge pull request #140 from xg-wang/sources
Fix sources: ["0"], output the correct source
2 parents 3f10b7d + b860f41 commit 7183703

5 files changed

Lines changed: 123 additions & 28 deletions

File tree

lib/get-sourcemap-content.js

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

lib/process-file.js

Lines changed: 19 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, path.dirname(inFile), relativePath, silent);
42+
if (content) {
43+
sourceMap.content = content;
4944
}
5045

5146
options = defaults(options, { sourceMap });
@@ -71,11 +66,22 @@ module.exports = function processFile(inFile, outFile, relativePath, outDir, sil
7166
let newSourceMap = JSON.parse(result.map);
7267

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

0 commit comments

Comments
 (0)