Skip to content

Commit

Permalink
Fix sources: ["0"], output the correct source
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xg-wang committed Apr 13, 2019
1 parent 3f10b7d commit fb0de6d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 28 deletions.
26 changes: 26 additions & 0 deletions lib/get-sourcemap-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const fs = require('fs');
const path = require('path');
const srcURL = require('source-map-url');
const srcRegExpg = new RegExp(srcURL.regex, 'g');

module.exports = function getSourceMapContent(src, inFile, relativePath, silent) {
let urls = [];
let match;
// eslint-disable-next-line no-cond-assign
while (match = srcRegExpg.exec(src)) {
urls.push(match[1] || match[2] || '');
}
if (urls.length) {
for (let i = urls.length - 1; i >= 0; --i) {
let sourceMapPath = path.join(path.dirname(inFile), urls[i]);
if (fs.existsSync(sourceMapPath)) {
return JSON.parse(fs.readFileSync(sourceMapPath));
}
}
if (!silent) {
console.warn(`[WARN] (broccoli-uglify-sourcemap) ${urls.map(u => `"${u}"`).join(', ')} referenced in "${relativePath}" could not be found`);
}
}
};
33 changes: 20 additions & 13 deletions lib/process-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const defaults = require('lodash.defaultsdeep');
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const srcURL = require('source-map-url');
const getSourceMapContent = require('./get-sourcemap-content');

const terser = require('terser');

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

let sourceMap = { filename, url };

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

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

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

newSourceMap.sources = newSourceMap.sources.map(function(path) {
// If out output file has the same name as one of our original
// sources, they will shadow eachother in Dev Tools. So instead we
// alter the reference to the upstream file.
if (path === relativePath) {
path = path.replace(/\.js$/, '-orig.js');
// If out output file has the same name as one of our original
// sources, they will shadow eachother in Dev Tools. So instead we
// alter the reference to the upstream file.
return path.replace(/\.js$/, '-orig.js');
} else if (path === '0') {
// 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.
return relativePath;
}
return path;
});
Expand Down
Loading

0 comments on commit fb0de6d

Please sign in to comment.