From 14dcb03c95c5af356b6058aa344633d6695f2ddd Mon Sep 17 00:00:00 2001 From: Yurii Palii Date: Thu, 2 Dec 2021 18:55:02 +0200 Subject: [PATCH] fix: Fix source maps (#818) * Fix missing source map register if the v8 cache enabled * Fix missing source maps in assets if the v8 cache disabled Co-authored-by: yuriipalii Co-authored-by: Steven --- src/index.js | 12 ++++++------ test/unit.test.js | 7 +++++++ test/unit/minify-sourcemap-register/input.js | 3 +++ test/unit/minify-sourcemap-register/opt.json | 5 +++++ test/unit/minify-sourcemap-register/output.js | 2 ++ test/unit/minify-sourcemap-register/output.js.map | 1 + test/unit/minify-v8cache-sourcemap-register/input.js | 3 +++ test/unit/minify-v8cache-sourcemap-register/opt.json | 6 ++++++ .../unit/minify-v8cache-sourcemap-register/output.js | 8 ++++++++ .../minify-v8cache-sourcemap-register/output.js.map | 1 + 10 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 test/unit/minify-sourcemap-register/input.js create mode 100644 test/unit/minify-sourcemap-register/opt.json create mode 100644 test/unit/minify-sourcemap-register/output.js create mode 100644 test/unit/minify-sourcemap-register/output.js.map create mode 100644 test/unit/minify-v8cache-sourcemap-register/input.js create mode 100644 test/unit/minify-v8cache-sourcemap-register/opt.json create mode 100644 test/unit/minify-v8cache-sourcemap-register/output.js create mode 100644 test/unit/minify-v8cache-sourcemap-register/output.js.map diff --git a/src/index.js b/src/index.js index f7dff022..242f413d 100644 --- a/src/index.js +++ b/src/index.js @@ -490,25 +490,25 @@ function ncc ( // custom terser phase used over Webpack integration for this reason if (!result || result.code === undefined) throw null; - + ({ code, map } = { code: result.code, map: map ? JSON.parse(result.map) : undefined }); } catch (e) { - console.log('An error occurred while minifying. The result will not be minified.'); + console.log('An error occurred while minifying. The result will not be minified.'); } } + if (map) { + assets[`${filename}.map`] = { source: JSON.stringify(map), permissions: defaultPermissions }; + } + if (v8cache) { const { Script } = require('vm'); assets[`${filename}.cache`] = { source: new Script(code).createCachedData(), permissions: defaultPermissions }; assets[`${filename}.cache${ext}`] = { source: code, permissions: defaultPermissions }; - if (map) { - assets[filename + '.map'] = { source: JSON.stringify(map), permissions: defaultPermissions }; - map = undefined; - } const columnOffset = -'(function (exports, require, module, __filename, __dirname) { '.length; code = `const { readFileSync, writeFileSync } = require('fs'), { Script } = require('vm'), { wrap } = require('module');\n` + diff --git a/test/unit.test.js b/test/unit.test.js index ffc6ec46..b2b4ca83 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -51,6 +51,13 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) { expect(assets['pi-bridge.js']).toBeDefined(); expect(assets['pi-bridge.js'].source.toString()).toContain('Math.PI'); } + if (unitTest.includes('sourcemap-register')) { + expect(assets['sourcemap-register.js']).toBeDefined() + expect(assets['sourcemap-register.js'].source.toString()).toEqual(fs.readFileSync(__dirname + '/../src/sourcemap-register.js.cache.js').toString()) + } + if (unitTest.includes('minify') && !unitTest.includes('minify-err')) { + expect(assets['index.js.map']).toBeDefined() + } const actual = code .trim() // Windows support diff --git a/test/unit/minify-sourcemap-register/input.js b/test/unit/minify-sourcemap-register/input.js new file mode 100644 index 00000000..e166ba5d --- /dev/null +++ b/test/unit/minify-sourcemap-register/input.js @@ -0,0 +1,3 @@ +const foobar = 'qux' +console.log?.("hello"); +exports.foobar = foobar diff --git a/test/unit/minify-sourcemap-register/opt.json b/test/unit/minify-sourcemap-register/opt.json new file mode 100644 index 00000000..31f46e59 --- /dev/null +++ b/test/unit/minify-sourcemap-register/opt.json @@ -0,0 +1,5 @@ +{ + "minify": true, + "sourceMap": true, + "sourceMapRegister": true +} diff --git a/test/unit/minify-sourcemap-register/output.js b/test/unit/minify-sourcemap-register/output.js new file mode 100644 index 00000000..ec551d8b --- /dev/null +++ b/test/unit/minify-sourcemap-register/output.js @@ -0,0 +1,2 @@ +require('./sourcemap-register.js');(()=>{if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var e={};(()=>{var _=e;const o="qux";console.log?.("hello");_.foobar=o})();module.exports=e})(); +//# sourceMappingURL=index.js.map diff --git a/test/unit/minify-sourcemap-register/output.js.map b/test/unit/minify-sourcemap-register/output.js.map new file mode 100644 index 00000000..609c7fa3 --- /dev/null +++ b/test/unit/minify-sourcemap-register/output.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../webpack:/@vercel/ncc/webpack/runtime/compat","../webpack:/@vercel/ncc/test/unit/minify-sourcemap-register/input.js"],"names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,2BCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,OAAAA","file":"index.js","sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"]} \ No newline at end of file diff --git a/test/unit/minify-v8cache-sourcemap-register/input.js b/test/unit/minify-v8cache-sourcemap-register/input.js new file mode 100644 index 00000000..e0038a8c --- /dev/null +++ b/test/unit/minify-v8cache-sourcemap-register/input.js @@ -0,0 +1,3 @@ +const foobar = 'qux' +console.log("hello"); +exports.foobar = foobar diff --git a/test/unit/minify-v8cache-sourcemap-register/opt.json b/test/unit/minify-v8cache-sourcemap-register/opt.json new file mode 100644 index 00000000..d4a6d15d --- /dev/null +++ b/test/unit/minify-v8cache-sourcemap-register/opt.json @@ -0,0 +1,6 @@ +{ + "minify": true, + "sourceMap": true, + "sourceMapRegister": true, + "v8cache": true +} diff --git a/test/unit/minify-v8cache-sourcemap-register/output.js b/test/unit/minify-v8cache-sourcemap-register/output.js new file mode 100644 index 00000000..85ca74ce --- /dev/null +++ b/test/unit/minify-v8cache-sourcemap-register/output.js @@ -0,0 +1,8 @@ +require('./sourcemap-register.js');const { readFileSync, writeFileSync } = require('fs'), { Script } = require('vm'), { wrap } = require('module'); +const basename = __dirname + '/index.js'; +const source = readFileSync(basename + '.cache.js', 'utf-8'); +const cachedData = !process.pkg && require('process').platform !== 'win32' && readFileSync(basename + '.cache'); +const scriptOpts = { filename: basename + '.cache.js', columnOffset: -62 } +const script = new Script(wrap(source), cachedData ? Object.assign({ cachedData }, scriptOpts) : scriptOpts); +(script.runInThisContext())(exports, require, module, __filename, __dirname); +if (cachedData) process.on('exit', () => { try { writeFileSync(basename + '.cache', script.createCachedData()); } catch(e) {} }); diff --git a/test/unit/minify-v8cache-sourcemap-register/output.js.map b/test/unit/minify-v8cache-sourcemap-register/output.js.map new file mode 100644 index 00000000..442ea081 --- /dev/null +++ b/test/unit/minify-v8cache-sourcemap-register/output.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../webpack:/@vercel/ncc/webpack/runtime/compat","../webpack:/@vercel/ncc/test/unit/minify-v8cache-sourcemap-register/input.js"],"names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,2BCDA,MAAAC,EAAA,MACAC,QAAAC,IAAA,SACAC,EAAAH,OAAAA","file":"index.js","sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log(\"hello\");\nexports.foobar = foobar\n"]}