From ce5984e4b07103b4e1123539ebb9fb82daf1344d Mon Sep 17 00:00:00 2001 From: axuj <68978293+axuj@users.noreply.github.com> Date: Thu, 19 Oct 2023 05:04:37 +0800 Subject: [PATCH] fix: sourcemap sources removes webpack path (#1122) - Fixes #1011 - Fixes #1121 The source is now in the format `webpack://[namespace]/[resourcePath]` --- src/index.js | 30 +++---------------- 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 ++++ .../output-coverage.js.map | 2 +- .../minify-sourcemap-register/output.js.map | 2 +- .../output-coverage.js.map | 2 +- .../output.js.map | 2 +- test/unit/minify/output-coverage.js.map | 2 +- test/unit/minify/output.js.map | 2 +- 11 files changed, 47 insertions(+), 33 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 6d6774ce..8ffa3022 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"); @@ -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, @@ -469,30 +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; - - if (map) { - 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; - }); - } + 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-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 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