diff --git a/src/cli.js b/src/cli.js index 974c12c6..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,6 +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, 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..0373a1e8 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.replace(/\/$/, '') + '[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');