Skip to content

Commit

Permalink
Merge pull request #44 from browserify/fix/no-match
Browse files Browse the repository at this point in the history
Don't muck with source maps if file doesn't match
  • Loading branch information
goto-bus-stop authored Apr 4, 2018
2 parents b0af62e + 5278a32 commit 03941dc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,28 @@ module.exports = function parse (modules, opts) {
return duplexer(concat(function (buf) {
try {
body = buf.toString('utf8').replace(/^#!/, '//#!');
var matches = false;
for (var key in modules) {
if (body.indexOf(key) !== -1) {
matches = true;
break;
}
}

if (!matches) {
// just pass it through
output.end(buf);
return;
}

if (opts.sourceMap) {
inputMap = convertSourceMap.fromSource(body);
if (inputMap) inputMap = inputMap.toObject();
body = convertSourceMap.removeComments(body);
sourcemapper = new MagicString(body);
}

for (var key in modules) {
if (body.indexOf(key) === -1) continue;
falafel(body, parserOpts, walk);
break;
}
falafel(body, parserOpts, walk);
}
catch (err) { return error(err) }
finish(body);
Expand Down
38 changes: 38 additions & 0 deletions test/sourcemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,43 @@ test('input source map', function (t) {
t.equal(mapped.column, 0);
}));

transform.end(minified.code);
});

test('retain input source map when file has no static-module use', function (t) {
t.plan(4);

var content = fs.readFileSync(__dirname + '/sourcemap/main.js', 'utf8');
var minified = uglify.minify({ 'main.js': content }, {
output: { beautify: true },
sourceMap: {
url: 'inline',
includeSources: true
}
});

var transform = staticModule({
not_sheetify: function () { return 'whatever'; }
}, { sourceMap: true, inputFilename: 'main.js' });

transform.pipe(concat({ encoding: 'string' }, function (res) {
var consumer = new SourceMapConsumer(convertSourceMap.fromSource(res).toObject());

var mapped = consumer.originalPositionFor({
line: 7,
column: 0
});
t.equal(mapped.line, 8);
t.equal(mapped.column, 0);

mapped = consumer.originalPositionFor({
line: 7,
column: 21
});
t.equal(mapped.line, 10);
t.equal(mapped.column, 0);
}));


transform.end(minified.code);
});

0 comments on commit 03941dc

Please sign in to comment.