Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand All @@ -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"],
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 25 additions & 3 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
Expand All @@ -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));
Expand All @@ -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;
}
}
]
1 change: 1 addition & 0 deletions test/fixtures/sourcemap-base-prefix/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('asdf');
Loading