From 5278a324ff540b152d4eba3d533ec9fa190db521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Fri, 30 Mar 2018 13:59:08 +0200 Subject: [PATCH] Don't muck with source maps if file doesn't match Source maps were stripped and generated for files that did not contain any modules. But those files were never parsed so their source maps were not initialised correctly. Then the source map merge step would merge the input source map with the uninitialised source map, essentially throwing away all mappings. With this patch the input is passed through entirely if it does not contain any modules, before any source map related checks. --- index.js | 20 +++++++++++++++----- test/sourcemap.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index aa200d7..7bcbef0 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,20 @@ 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(); @@ -40,11 +54,7 @@ module.exports = function parse (modules, opts) { 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); diff --git a/test/sourcemap.js b/test/sourcemap.js index bb00d45..56be1ce 100644 --- a/test/sourcemap.js +++ b/test/sourcemap.js @@ -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); });