diff --git a/index.js b/index.js index d59a7f2..e2e23be 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,8 @@ module.exports = UglifyWriter; UglifyWriter.prototype = Object.create(Plugin.prototype); UglifyWriter.prototype.constructor = UglifyWriter; +const silent = process.argv.indexOf('--silent') !== -1; + function UglifyWriter (inputNodes, options) { if (!(this instanceof UglifyWriter)) { return new UglifyWriter(inputNodes, options); @@ -98,7 +100,12 @@ UglifyWriter.prototype.processFile = function(inFile, outFile, relativePath, out if (srcURL.existsIn(src)) { let url = srcURL.getFrom(src); - sourceMap.content = JSON.parse(fs.readFileSync(path.join(path.dirname(inFile), url))); + 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`); + } } options = defaults(options, { sourceMap }); @@ -111,7 +118,7 @@ UglifyWriter.prototype.processFile = function(inFile, outFile, relativePath, out var total = end - start; debug('[finished]: %s %dKB in %dms', relativePath, (result.code.length / 1000), total); - if (total > 20000 && process.argv.indexOf('--silent') === -1) { + if (total > 20000 && !silent) { console.warn('[WARN] (broccoli-uglify-sourcemap) Minifying: `' + relativePath + '` took: ' + total + 'ms (more than 20,000ms)'); } diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap index ce7ce45..68a1451 100644 --- a/test/__snapshots__/test.js.snap +++ b/test/__snapshots__/test.js.snap @@ -9,6 +9,7 @@ Object { "something.css": "#id { background: white; }", + "with-broken-sourcemap.js": "function meaningOfLife(){throw new Error(42)}", } `; @@ -45,6 +46,9 @@ function third(){ "something.css": "#id { background: white; }", + "with-broken-sourcemap.js": "function meaningOfLife(){throw new Error(42)} +//# sourceMappingURL=with-broken-sourcemap.map", + "with-broken-sourcemap.map": "{\\"version\\":3,\\"sources\\":[\\"0\\"],\\"names\\":[\\"meaningOfLife\\",\\"Error\\"],\\"mappings\\":\\"AAAA,SAASA,gBAEP,MAAM,IAAIC,MADa\\",\\"file\\":\\"with-broken-sourcemap.js\\"}", } `; @@ -69,6 +73,9 @@ Object { "something.css": "#id { background: white; }", + "with-broken-sourcemap.js": "function meaningOfLife(){throw new Error(42)} +//# sourceMappingURL=with-broken-sourcemap.map", + "with-broken-sourcemap.map": "{\\"version\\":3,\\"sources\\":[\\"0\\"],\\"names\\":[\\"meaningOfLife\\",\\"Error\\"],\\"mappings\\":\\"AAAA,SAASA,gBAEP,MAAM,IAAIC,MADa\\",\\"file\\":\\"with-broken-sourcemap.js\\"}", } `; @@ -80,6 +87,7 @@ Object { }, "maps": Object { "no-upstream-sourcemap.map": "{\\"version\\":3,\\"sources\\":[\\"0\\"],\\"names\\":[\\"meaningOfLife\\",\\"Error\\",\\"boom\\",\\"somethingElse\\",\\"fourth\\",\\"third\\"],\\"mappings\\":\\"AACA,SAASA,gBAEP,MAAM,IAAIC,MADa,IAIzB,SAASC,OACP,MAAM,IAAID,MAAM,QAGlB,SAASE,gBACP,MAAM,IAAIF,MAAM,kBAMlB,SAASG,SACP,MAAM,IAAIH,MAAM,UAGlB,SAASI,QACP,MAAM,IAAIJ,MAAM\\",\\"file\\":\\"no-upstream-sourcemap.js\\"}", + "with-broken-sourcemap.map": "{\\"version\\":3,\\"sources\\":[\\"0\\"],\\"names\\":[\\"meaningOfLife\\",\\"Error\\"],\\"mappings\\":\\"AAAA,SAASA,gBAEP,MAAM,IAAIC,MADa\\",\\"file\\":\\"with-broken-sourcemap.js\\"}", "with-upstream-sourcemap.map": "{\\"version\\":3,\\"sources\\":[\\"/inner/first.js\\",\\"/inner/second.js\\",\\"/other/fourth.js\\",\\"/other/third.js\\"],\\"names\\":[\\"meaningOfLife\\",\\"Error\\",\\"boom\\",\\"somethingElse\\",\\"fourth\\",\\"third\\"],\\"mappings\\":\\"AAAA,SAAAA,gBAEA,MAAA,IAAAC,MADA,IAIA,SAAAC,OACA,MAAA,IAAAD,MAAA,QCNA,SAAAE,gBACA,MAAA,IAAAF,MAAA,kBCEA,SAAAG,SACA,MAAA,IAAAH,MAAA,UCJA,SAAAI,QACA,MAAA,IAAAJ,MAAA\\",\\"file\\":\\"with-upstream-sourcemap.js\\",\\"sourcesContent\\":[\\"function meaningOfLife() {\\\\n var thisIsALongLocal = 42;\\\\n throw new Error(thisIsALongLocal);\\\\n}\\\\n\\\\nfunction boom() {\\\\n throw new Error('boom');\\\\n}\\\\n\\",\\"function somethingElse() {\\\\n throw new Error(\\\\\\"somethign else\\\\\\");\\\\n}\\\\n\\",\\"\\\\n// Hello world\\\\n\\\\nfunction fourth(){\\\\n throw new Error('fourth');\\\\n}\\\\n\\",\\"function third(){\\\\n throw new Error(\\\\\\"oh no\\\\\\");\\\\n}\\\\n\\"]}", }, "no-upstream-sourcemap.js": "function meaningOfLife(){throw new Error(42)}function boom(){throw new Error(\\"boom\\")}function somethingElse(){throw new Error(\\"somethign else\\")}function fourth(){throw new Error(\\"fourth\\")}function third(){throw new Error(\\"oh no\\")} @@ -87,5 +95,7 @@ Object { "something.css": "#id { background: white; }", + "with-broken-sourcemap.js": "function meaningOfLife(){throw new Error(42)} +//# sourceMappingURL=/maps/with-broken-sourcemap.map", } `; diff --git a/test/fixtures/with-broken-sourcemap.js b/test/fixtures/with-broken-sourcemap.js new file mode 100644 index 0000000..03af9ad --- /dev/null +++ b/test/fixtures/with-broken-sourcemap.js @@ -0,0 +1,6 @@ +function meaningOfLife() { + var thisIsALongLocal = 42; + throw new Error(thisIsALongLocal); +} + +//# sourceMappingURL=missing-sourcemap.map \ No newline at end of file