From f0c86267887a70a64fd7c1e9892c7da2ae4f2bf8 Mon Sep 17 00:00:00 2001 From: legendecas Date: Wed, 12 Feb 2020 23:13:48 +0800 Subject: [PATCH] Fix minified source-map outputs format (#504) * Fix minified source-map outputs format --- src/index.js | 5 +++- test/index.test.js | 35 ++++++++++++++++++++++--- test/unit/minify/input.js | 2 ++ test/unit/minify/opt.json | 5 ++++ test/unit/minify/output-coverage.js | 2 ++ test/unit/minify/output-coverage.js.map | 1 + test/unit/minify/output.js | 2 ++ test/unit/minify/output.js.map | 1 + 8 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 test/unit/minify/input.js create mode 100644 test/unit/minify/opt.json create mode 100644 test/unit/minify/output-coverage.js create mode 100644 test/unit/minify/output-coverage.js.map create mode 100644 test/unit/minify/output.js create mode 100644 test/unit/minify/output.js.map diff --git a/src/index.js b/src/index.js index bc310d67..8951c14c 100644 --- a/src/index.js +++ b/src/index.js @@ -368,7 +368,10 @@ module.exports = ( // For some reason, auth0 returns "undefined"! // custom terser phase used over Webpack integration for this reason if (result.code !== undefined) - ({ code, map } = { code: result.code, map: result.map }); + ({ code, map } = { + code: result.code, + map: sourceMap ? JSON.parse(result.map) : undefined + }); } if (v8cache) { diff --git a/test/index.test.js b/test/index.test.js index 2dba9bda..f3015b24 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -11,17 +11,32 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) { .trim() // Windows support .replace(/\r/g, ""); + let expectedSoureMap; + try { + expectedSoureMap = fs + .readFileSync(`${testDir}/output${global.coverage ? '-coverage' : ''}.js.map`) + .toString() + .trim() + // Windows support + .replace(/\r/g, ""); + } catch (_) {} + + let opts; + try { + opts = fs.readFileSync(`${testDir}/opt.json`, 'utf8'); + opts = JSON.parse(opts); + } catch (_) {} // set env variable so tsconfig-paths can find the config process.env.TS_NODE_PROJECT = `${testDir}/tsconfig.json`; // find the name of the input file (e.g input.ts) const inputFile = fs.readdirSync(testDir).find(file => file.includes("input")); - await ncc(`${testDir}/${inputFile}`, { + await ncc(`${testDir}/${inputFile}`, Object.assign({ externals: { 'externaltest': 'externalmapped' } - }).then( - async ({ code, assets }) => { + }, opts)).then( + async ({ code, assets, map }) => { const actual = code .trim() // Windows support @@ -33,6 +48,20 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) { fs.writeFileSync(`${testDir}/actual.js`, actual); throw e; } + + if (map) { + const actualSourceMap = map + .trim() + // Windows support + .replace(/\r/g, ""); + try { + expect(actualSourceMap).toBe(expectedSoureMap); + } catch (e) { + // useful for updating fixtures + fs.writeFileSync(`${testDir}/actual.js.map`, actualSourceMap); + throw e; + } + } } ); }); diff --git a/test/unit/minify/input.js b/test/unit/minify/input.js new file mode 100644 index 00000000..74b15e55 --- /dev/null +++ b/test/unit/minify/input.js @@ -0,0 +1,2 @@ +const foobar = 'qux' +exports.foobar = foobar diff --git a/test/unit/minify/opt.json b/test/unit/minify/opt.json new file mode 100644 index 00000000..c66df06a --- /dev/null +++ b/test/unit/minify/opt.json @@ -0,0 +1,5 @@ +{ + "minify": true, + "sourceMap": true, + "sourceMapRegister": false +} diff --git a/test/unit/minify/output-coverage.js b/test/unit/minify/output-coverage.js new file mode 100644 index 00000000..0ee64ce2 --- /dev/null +++ b/test/unit/minify/output-coverage.js @@ -0,0 +1,2 @@ +module.exports=function(r,e){"use strict";var t={};function __webpack_require__(e){if(t[e]){return t[e].exports}var _=t[e]={i:e,l:false,exports:{}};r[e].call(_.exports,_,_.exports,__webpack_require__);_.l=true;return _.exports}__webpack_require__.ab=__dirname+"/";function startup(){return __webpack_require__(894)}return startup()}({894:function(r,e){const t="qux";e.foobar=t}}); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/test/unit/minify/output-coverage.js.map b/test/unit/minify/output-coverage.js.map new file mode 100644 index 00000000..36d3f443 --- /dev/null +++ b/test/unit/minify/output-coverage.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["/webpack/bootstrap","../test/unit/minify/input.js"],"names":["installedModules","__webpack_require__","moduleId","exports","module","i","l","modules","call","ab","__dirname","startup","foobar"],"mappings":"0CACA,IAAAA,EAAA,GAGA,SAAAC,oBAAAC,GAGA,GAAAF,EAAAE,GAAA,CACA,OAAAF,EAAAE,GAAAC,QAGA,IAAAC,EAAAJ,EAAAE,GAAA,CACAG,EAAAH,EACAI,EAAA,MACAH,QAAA,IAIAI,EAAAL,GAAAM,KAAAJ,EAAAD,QAAAC,EAAAA,EAAAD,QAAAF,qBAGAG,EAAAE,EAAA,KAGA,OAAAF,EAAAD,QAIAF,oBAAAQ,GAAAC,UAAA,IAGA,SAAAC,UAEA,OAAAV,oBAAA,KAIA,OAAAU,8BCrCA,MAAAC,EAAA,MACAT,EAAOS,OAAAA","file":"index.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t__webpack_require__.ab = __dirname + \"/\";\n\n \t// the startup function\n \tfunction startup() {\n \t\t// Load entry module and return exports\n \t\treturn __webpack_require__(894);\n \t};\n\n \t// run startup\n \treturn startup();\n","const foobar = 'qux'\nexports.foobar = foobar\n"]} \ No newline at end of file diff --git a/test/unit/minify/output.js b/test/unit/minify/output.js new file mode 100644 index 00000000..08b31d1e --- /dev/null +++ b/test/unit/minify/output.js @@ -0,0 +1,2 @@ +module.exports=function(r,e){"use strict";var t={};function __webpack_require__(e){if(t[e]){return t[e].exports}var _=t[e]={i:e,l:false,exports:{}};r[e].call(_.exports,_,_.exports,__webpack_require__);_.l=true;return _.exports}__webpack_require__.ab=__dirname+"/";function startup(){return __webpack_require__(657)}return startup()}({657:function(r,e){const t="qux";e.foobar=t}}); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/test/unit/minify/output.js.map b/test/unit/minify/output.js.map new file mode 100644 index 00000000..96aeb2e4 --- /dev/null +++ b/test/unit/minify/output.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["/webpack/bootstrap","../test/unit/minify/input.js"],"names":["installedModules","__webpack_require__","moduleId","exports","module","i","l","modules","call","ab","__dirname","startup","foobar"],"mappings":"0CACA,IAAAA,EAAA,GAGA,SAAAC,oBAAAC,GAGA,GAAAF,EAAAE,GAAA,CACA,OAAAF,EAAAE,GAAAC,QAGA,IAAAC,EAAAJ,EAAAE,GAAA,CACAG,EAAAH,EACAI,EAAA,MACAH,QAAA,IAIAI,EAAAL,GAAAM,KAAAJ,EAAAD,QAAAC,EAAAA,EAAAD,QAAAF,qBAGAG,EAAAE,EAAA,KAGA,OAAAF,EAAAD,QAIAF,oBAAAQ,GAAAC,UAAA,IAGA,SAAAC,UAEA,OAAAV,oBAAA,KAIA,OAAAU,8BCrCA,MAAAC,EAAA,MACAT,EAAOS,OAAAA","file":"index.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t__webpack_require__.ab = __dirname + \"/\";\n\n \t// the startup function\n \tfunction startup() {\n \t\t// Load entry module and return exports\n \t\treturn __webpack_require__(657);\n \t};\n\n \t// run startup\n \treturn startup();\n","const foobar = 'qux'\nexports.foobar = foobar\n"]} \ No newline at end of file