Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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