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); });