From 7e2b6d3016c570da19429eff8964bf9be80cedcb Mon Sep 17 00:00:00 2001 From: Vladimir Starkov Date: Sun, 16 Feb 2025 15:41:46 +0100 Subject: [PATCH 1/3] fix: make sourcemap base prefix relative to the build file instead of default "../" (fixes #1253) --- src/cli.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cli.js b/src/cli.js index 974c12c6..2705ab61 100755 --- a/src/cli.js +++ b/src/cli.js @@ -264,6 +264,7 @@ async function runCmd (argv, stdout, stderr) { externals: args["--external"], sourceMap: args["--source-map"] || run, sourceMapRegister: args["--no-source-map-register"] ? false : undefined, + sourceMapBasePrefix: path.relative(outDir, buildFile), assetBuilds: args["--asset-builds"] ? true : false, cache: args["--no-cache"] ? false : undefined, watch: args["--watch"], From 35d2efa5d51f70289fec5a4d29c4f3faac2a6add Mon Sep 17 00:00:00 2001 From: Vladimir Starkov Date: Tue, 29 Apr 2025 14:42:28 +0200 Subject: [PATCH 2/3] fix: adjust sourceMapBasePrefix handling and add tests for sourcemap paths --- src/cli.js | 3 ++- src/index.js | 6 ++++- test/cli.js | 28 +++++++++++++++++--- test/fixtures/sourcemap-base-prefix/index.js | 1 + 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/sourcemap-base-prefix/index.js diff --git a/src/cli.js b/src/cli.js index 2705ab61..70a99e49 100755 --- a/src/cli.js +++ b/src/cli.js @@ -256,6 +256,7 @@ async function runCmd (argv, stdout, stderr) { const buildFile = eval("require.resolve")(resolve(args._[1] || ".")); const esm = buildFile.endsWith('.mjs') || !buildFile.endsWith('.cjs') && hasTypeModule(buildFile); const ext = buildFile.endsWith('.cjs') ? '.cjs' : esm && (buildFile.endsWith('.mjs') || !hasTypeModule(buildFile)) ? '.mjs' : '.js'; + const sourceMapBasePrefix = relative(outDir, process.cwd()); const ncc = require("./index.js")( buildFile, { @@ -264,7 +265,7 @@ async function runCmd (argv, stdout, stderr) { externals: args["--external"], sourceMap: args["--source-map"] || run, sourceMapRegister: args["--no-source-map-register"] ? false : undefined, - sourceMapBasePrefix: path.relative(outDir, buildFile), + sourceMapBasePrefix, assetBuilds: args["--asset-builds"] ? true : false, cache: args["--no-cache"] ? false : undefined, watch: args["--watch"], diff --git a/src/index.js b/src/index.js index 8ffa3022..e7505839 100644 --- a/src/index.js +++ b/src/index.js @@ -295,7 +295,11 @@ function ncc ( libraryTarget: esm ? 'module' : 'commonjs2', strictModuleExceptionHandling: true, module: esm, - devtoolModuleFilenameTemplate: sourceMapBasePrefix + '[resource-path]' + // if prefix is `../`, then final path results in `.././` + // if prefix is `../../`, then final path results results in `../.././` + // which ideally would have been normalized by webpack, but alas it isnt, + // so we need to adjust sourceMapBasePrefix + devtoolModuleFilenameTemplate: sourceMapBasePrefix.slice(0, -1) + '[resource-path]' }, resolve: { extensions: SUPPORTED_EXTENSIONS, diff --git a/test/cli.js b/test/cli.js index 1ac38536..500b07ce 100644 --- a/test/cli.js +++ b/test/cli.js @@ -106,7 +106,7 @@ module.exports = [ env: { TYPESCRIPT_LOOKUP_PATH: '/tmp/nowhere' }, - expect (code, stdout) { + expect (code, stdout) { return code === 0 && stdout.indexOf('ncc built-in') !== -1; }, }, @@ -121,7 +121,7 @@ module.exports = [ 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 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)); @@ -133,12 +133,34 @@ module.exports = [ 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 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-base-prefix/index.js", "--source-map"], + expect(code) { + const fs = require('fs'); + const map = JSON.parse(fs.readFileSync(join('tmp', 'index.js.map'), 'utf8')); + const actual = map.sources[1]; + const expected = '../test/fixtures/sourcemap-base-prefix/index.js' + if (actual !== expected) console.log({ actual, expected }) + return code === 0 && actual === expected; + } + }, + { + args: ["build", "-o", "tmp/sourcemap-base-prefix", "test/fixtures/sourcemap-base-prefix/index.js", "--source-map"], + expect(code) { + const fs = require('fs'); + const map = JSON.parse(fs.readFileSync(join('tmp', 'sourcemap-base-prefix', 'index.js.map'), 'utf8')); + const actual = map.sources[1]; + const expected = '../../test/fixtures/sourcemap-base-prefix/index.js' + if (actual !== expected) console.log({ actual, expected }) + return code === 0 && actual === expected; + } } ] diff --git a/test/fixtures/sourcemap-base-prefix/index.js b/test/fixtures/sourcemap-base-prefix/index.js new file mode 100644 index 00000000..5f47e93c --- /dev/null +++ b/test/fixtures/sourcemap-base-prefix/index.js @@ -0,0 +1 @@ +console.log('asdf'); From 10073b00da15f3df5e0c1233a02389c97f741f8d Mon Sep 17 00:00:00 2001 From: Vladimir Starkov <559321+iamstarkov@users.noreply.github.com> Date: Thu, 22 May 2025 20:53:58 +0200 Subject: [PATCH 3/3] Update src/index.js --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index e7505839..0373a1e8 100644 --- a/src/index.js +++ b/src/index.js @@ -299,7 +299,7 @@ function ncc ( // if prefix is `../../`, then final path results results in `../.././` // which ideally would have been normalized by webpack, but alas it isnt, // so we need to adjust sourceMapBasePrefix - devtoolModuleFilenameTemplate: sourceMapBasePrefix.slice(0, -1) + '[resource-path]' + devtoolModuleFilenameTemplate: sourceMapBasePrefix.replace(/\/$/, '') + '[resource-path]' }, resolve: { extensions: SUPPORTED_EXTENSIONS,