From 289f49c6cfd1c43b766f7ebddd02ce53ddfd0557 Mon Sep 17 00:00:00 2001 From: axuj Date: Thu, 12 Oct 2023 20:41:24 +0800 Subject: [PATCH 1/4] fix: sourcemap sources removes webpack path --- src/index.js | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 6d6774ce..aa27b536 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ const resolve = require("resolve"); const fs = require("graceful-fs"); const crypto = require("crypto"); -const { join, dirname, extname, relative, resolve: pathResolve } = require("path"); +const { join, dirname, extname, resolve: pathResolve } = require("path"); const webpack = require("webpack"); const MemoryFS = require("memory-fs"); const terser = require("terser"); @@ -475,22 +475,11 @@ function ncc ( map = JSON.parse(map); // make source map sources relative to output map.sources = map.sources.map(source => { - // webpack:///webpack:/// happens too for some reason - while (source.startsWith('webpack:///')) - source = source.slice(11); - if (source.startsWith('//')) - source = source.slice(1); - if (source.startsWith('/')) - source = relative(process.cwd(), source).replace(/\\/g, '/'); - if (source.startsWith('external ')) - source = 'node:' + source.slice(9); - if (source.startsWith('./')) - source = source.slice(2); - if (source.startsWith('(webpack)')) - source = 'webpack' + source.slice(9); - if (source.startsWith('webpack/')) - return '/webpack/' + source.slice(8); - return sourceMapBasePrefix + source; + // webpack://[namespace]/[resourcePath] + return join( + sourceMapBasePrefix, + source.replace(/^webpack:[\/]*([^\/]+\/)/, '') + ); }); } From 651a21b0ce0fb18a2d663cdbb301de2d1313ff86 Mon Sep 17 00:00:00 2001 From: axuj Date: Fri, 13 Oct 2023 15:29:26 +0800 Subject: [PATCH 2/4] fix: use webpack config to generate resource-path --- src/index.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index aa27b536..af6c2646 100644 --- a/src/index.js +++ b/src/index.js @@ -294,7 +294,8 @@ function ncc ( filename: ext === '.cjs' ? filename + '.js' : filename, libraryTarget: esm ? 'module' : 'commonjs2', strictModuleExceptionHandling: true, - module: esm + module: esm, + devtoolModuleFilenameTemplate: sourceMapBasePrefix + '[resource-path]' }, resolve: { extensions: SUPPORTED_EXTENSIONS, @@ -471,18 +472,6 @@ function ncc ( let code = mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}`, "utf8"); let map = sourceMap ? mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}.map`, "utf8") : null; - if (map) { - map = JSON.parse(map); - // make source map sources relative to output - map.sources = map.sources.map(source => { - // webpack://[namespace]/[resourcePath] - return join( - sourceMapBasePrefix, - source.replace(/^webpack:[\/]*([^\/]+\/)/, '') - ); - }); - } - if (minify) { let result; try { From a612dd9a870949a5ea2e41785dba78ff5e01c875 Mon Sep 17 00:00:00 2001 From: axuj Date: Sat, 14 Oct 2023 18:31:08 +0800 Subject: [PATCH 3/4] Add sourcemap-resource-path test. test/unit/minify update output --- src/index.js | 2 +- test/cli.js | 26 ++++++++++++++++++- .../fixtures/sourcemap-resource-path/index.ts | 4 +++ test/fixtures/sourcemap-resource-path/sum.ts | 3 +++ .../sourcemap-resource-path/tsconfig.json | 5 ++++ test/unit/minify/output-coverage.js.map | 2 +- test/unit/minify/output.js.map | 2 +- 7 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/sourcemap-resource-path/index.ts create mode 100644 test/fixtures/sourcemap-resource-path/sum.ts create mode 100644 test/fixtures/sourcemap-resource-path/tsconfig.json diff --git a/src/index.js b/src/index.js index af6c2646..8ffa3022 100644 --- a/src/index.js +++ b/src/index.js @@ -470,7 +470,7 @@ function ncc ( delete assets[filename + (ext === '.cjs' ? '.js' : '')]; delete assets[`${filename}${ext === '.cjs' ? '.js' : ''}.map`]; let code = mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}`, "utf8"); - let map = sourceMap ? mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}.map`, "utf8") : null; + let map = sourceMap ? JSON.parse(mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}.map`, "utf8")) : null; if (minify) { let result; diff --git a/test/cli.js b/test/cli.js index 8df03fda..1ac38536 100644 --- a/test/cli.js +++ b/test/cli.js @@ -1,4 +1,4 @@ -const { join } = require('path') +const { join, resolve: pathResolve } = require('path'); module.exports = [ { @@ -116,5 +116,29 @@ module.exports = [ const fs = require('fs'); return code === 0 && fs.readFileSync(join('tmp', 'index.js'), 'utf8').toString().indexOf('export {') === -1; } + }, + { + args: ["build", "-o", "tmp", "test/fixtures/sourcemap-resource-path/index.ts", "--source-map", "--no-source-map-register"], + expect (code, stdout, stderr) { + const fs = require('fs'); + const map = JSON.parse(fs.readFileSync(join('tmp', 'index.js.map'), 'utf8')); + const paths = map.sources.map(source=>pathResolve(join('tmp', source))); + function hasPath(path) { + return paths.includes(pathResolve(path)); + } + return code === 0 && hasPath('test/fixtures/sourcemap-resource-path/index.ts') && hasPath('test/fixtures/sourcemap-resource-path/sum.ts'); + } + }, + { + args: ["build", "-o", "tmp", "test/fixtures/sourcemap-resource-path/index.ts", "-m", "--source-map", "--no-source-map-register"], + expect (code, stdout, stderr) { + const fs = require('fs'); + const map = JSON.parse(fs.readFileSync(join('tmp', 'index.js.map'), 'utf8')); + const paths = map.sources.map(source=>pathResolve(join('tmp', source))); + function hasPath(path) { + return paths.includes(pathResolve(path)); + } + return code === 0 && hasPath('test/fixtures/sourcemap-resource-path/index.ts') && hasPath('test/fixtures/sourcemap-resource-path/sum.ts'); + } } ] diff --git a/test/fixtures/sourcemap-resource-path/index.ts b/test/fixtures/sourcemap-resource-path/index.ts new file mode 100644 index 00000000..2eee6743 --- /dev/null +++ b/test/fixtures/sourcemap-resource-path/index.ts @@ -0,0 +1,4 @@ +import { sum } from './sum' + +const s = sum(1, 2) +console.log(s) diff --git a/test/fixtures/sourcemap-resource-path/sum.ts b/test/fixtures/sourcemap-resource-path/sum.ts new file mode 100644 index 00000000..5d8550bb --- /dev/null +++ b/test/fixtures/sourcemap-resource-path/sum.ts @@ -0,0 +1,3 @@ +export function sum(a: number, b: number) { + return a + b +} diff --git a/test/fixtures/sourcemap-resource-path/tsconfig.json b/test/fixtures/sourcemap-resource-path/tsconfig.json new file mode 100644 index 00000000..e208006e --- /dev/null +++ b/test/fixtures/sourcemap-resource-path/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "target": "es2015", + } +} diff --git a/test/unit/minify/output-coverage.js.map b/test/unit/minify/output-coverage.js.map index 2a1bef32..bd4df7b0 100644 --- a/test/unit/minify/output-coverage.js.map +++ b/test/unit/minify/output-coverage.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack://@vercel/ncc/webpack/runtime/compat","../webpack://@vercel/ncc/./test/unit/minify/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file +{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack/runtime/compat",".././test/unit/minify/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file diff --git a/test/unit/minify/output.js.map b/test/unit/minify/output.js.map index 2a1bef32..bd4df7b0 100644 --- a/test/unit/minify/output.js.map +++ b/test/unit/minify/output.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack://@vercel/ncc/webpack/runtime/compat","../webpack://@vercel/ncc/./test/unit/minify/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file +{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack/runtime/compat",".././test/unit/minify/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file From 4a7b2a02c2e76d850336c5014c3a253a025691c8 Mon Sep 17 00:00:00 2001 From: axuj Date: Tue, 17 Oct 2023 15:37:57 +0800 Subject: [PATCH 4/4] Update test output test/unit/minify-sourcemap-register test/unit/minify-v8cache-sourcemap-register --- test/unit/minify-sourcemap-register/output-coverage.js.map | 2 +- test/unit/minify-sourcemap-register/output.js.map | 2 +- .../minify-v8cache-sourcemap-register/output-coverage.js.map | 2 +- test/unit/minify-v8cache-sourcemap-register/output.js.map | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/minify-sourcemap-register/output-coverage.js.map b/test/unit/minify-sourcemap-register/output-coverage.js.map index 3880bd5b..c6d6e61b 100644 --- a/test/unit/minify-sourcemap-register/output-coverage.js.map +++ b/test/unit/minify-sourcemap-register/output-coverage.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack://@vercel/ncc/webpack/runtime/compat","../webpack://@vercel/ncc/./test/unit/minify-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file +{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack/runtime/compat",".././test/unit/minify-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file diff --git a/test/unit/minify-sourcemap-register/output.js.map b/test/unit/minify-sourcemap-register/output.js.map index 3880bd5b..c6d6e61b 100644 --- a/test/unit/minify-sourcemap-register/output.js.map +++ b/test/unit/minify-sourcemap-register/output.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack://@vercel/ncc/webpack/runtime/compat","../webpack://@vercel/ncc/./test/unit/minify-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file +{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack/runtime/compat",".././test/unit/minify-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log?.(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,MAAA,SACAC,EAAAH,Q"} \ No newline at end of file diff --git a/test/unit/minify-v8cache-sourcemap-register/output-coverage.js.map b/test/unit/minify-v8cache-sourcemap-register/output-coverage.js.map index f571fe71..7d9bd01b 100644 --- a/test/unit/minify-v8cache-sourcemap-register/output-coverage.js.map +++ b/test/unit/minify-v8cache-sourcemap-register/output-coverage.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack://@vercel/ncc/webpack/runtime/compat","../webpack://@vercel/ncc/./test/unit/minify-v8cache-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,IAAA,SACAC,EAAAH,Q"} \ No newline at end of file +{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack/runtime/compat",".././test/unit/minify-v8cache-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,IAAA,SACAC,EAAAH,Q"} \ No newline at end of file diff --git a/test/unit/minify-v8cache-sourcemap-register/output.js.map b/test/unit/minify-v8cache-sourcemap-register/output.js.map index f571fe71..7d9bd01b 100644 --- a/test/unit/minify-v8cache-sourcemap-register/output.js.map +++ b/test/unit/minify-v8cache-sourcemap-register/output.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack://@vercel/ncc/webpack/runtime/compat","../webpack://@vercel/ncc/./test/unit/minify-v8cache-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,IAAA,SACAC,EAAAH,Q"} \ No newline at end of file +{"version":3,"file":"index.js","names":["__webpack_require__","ab","__dirname","foobar","console","log","exports"],"sources":["../webpack/runtime/compat",".././test/unit/minify-v8cache-sourcemap-register/input.js"],"sourcesContent":["\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + \"/\";","const foobar = 'qux'\nconsole.log(\"hello\");\nexports.foobar = foobar\n"],"mappings":"MACA,UAAAA,sBAAA,YAAAA,oBAAAC,GAAAC,UAAA,I,uBCDA,MAAAC,EAAA,MACAC,QAAAC,IAAA,SACAC,EAAAH,Q"} \ No newline at end of file