From d590c8639b51c9fd82d469802c5ad2d76fbd7835 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 16 Nov 2017 13:22:16 +0100 Subject: [PATCH] Ignore missing sourcemap if referenced file does not exist --- index.js | 5 ++++- test/__snapshots__/test.js.snap | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d59a7f2..aba4fb2 100644 --- a/index.js +++ b/index.js @@ -98,7 +98,10 @@ 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)); + } } options = defaults(options, { sourceMap }); 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", } `;