Skip to content

Commit ce5984e

Browse files
authoredOct 18, 2023
fix: sourcemap sources removes webpack path (#1122)
- Fixes #1011 - Fixes #1121 The source is now in the format `webpack://[namespace]/[resourcePath]`
1 parent f9c1153 commit ce5984e

File tree

11 files changed

+47
-33
lines changed

11 files changed

+47
-33
lines changed
 

‎src/index.js

+4-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const resolve = require("resolve");
22
const fs = require("graceful-fs");
33
const crypto = require("crypto");
4-
const { join, dirname, extname, relative, resolve: pathResolve } = require("path");
4+
const { join, dirname, extname, resolve: pathResolve } = require("path");
55
const webpack = require("webpack");
66
const MemoryFS = require("memory-fs");
77
const terser = require("terser");
@@ -294,7 +294,8 @@ function ncc (
294294
filename: ext === '.cjs' ? filename + '.js' : filename,
295295
libraryTarget: esm ? 'module' : 'commonjs2',
296296
strictModuleExceptionHandling: true,
297-
module: esm
297+
module: esm,
298+
devtoolModuleFilenameTemplate: sourceMapBasePrefix + '[resource-path]'
298299
},
299300
resolve: {
300301
extensions: SUPPORTED_EXTENSIONS,
@@ -469,30 +470,7 @@ function ncc (
469470
delete assets[filename + (ext === '.cjs' ? '.js' : '')];
470471
delete assets[`${filename}${ext === '.cjs' ? '.js' : ''}.map`];
471472
let code = mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}`, "utf8");
472-
let map = sourceMap ? mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}.map`, "utf8") : null;
473-
474-
if (map) {
475-
map = JSON.parse(map);
476-
// make source map sources relative to output
477-
map.sources = map.sources.map(source => {
478-
// webpack:///webpack:/// happens too for some reason
479-
while (source.startsWith('webpack:///'))
480-
source = source.slice(11);
481-
if (source.startsWith('//'))
482-
source = source.slice(1);
483-
if (source.startsWith('/'))
484-
source = relative(process.cwd(), source).replace(/\\/g, '/');
485-
if (source.startsWith('external '))
486-
source = 'node:' + source.slice(9);
487-
if (source.startsWith('./'))
488-
source = source.slice(2);
489-
if (source.startsWith('(webpack)'))
490-
source = 'webpack' + source.slice(9);
491-
if (source.startsWith('webpack/'))
492-
return '/webpack/' + source.slice(8);
493-
return sourceMapBasePrefix + source;
494-
});
495-
}
473+
let map = sourceMap ? JSON.parse(mfs.readFileSync(`/${filename}${ext === '.cjs' ? '.js' : ''}.map`, "utf8")) : null;
496474

497475
if (minify) {
498476
let result;

‎test/cli.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { join } = require('path')
1+
const { join, resolve: pathResolve } = require('path');
22

33
module.exports = [
44
{
@@ -116,5 +116,29 @@ module.exports = [
116116
const fs = require('fs');
117117
return code === 0 && fs.readFileSync(join('tmp', 'index.js'), 'utf8').toString().indexOf('export {') === -1;
118118
}
119+
},
120+
{
121+
args: ["build", "-o", "tmp", "test/fixtures/sourcemap-resource-path/index.ts", "--source-map", "--no-source-map-register"],
122+
expect (code, stdout, stderr) {
123+
const fs = require('fs');
124+
const map = JSON.parse(fs.readFileSync(join('tmp', 'index.js.map'), 'utf8'));
125+
const paths = map.sources.map(source=>pathResolve(join('tmp', source)));
126+
function hasPath(path) {
127+
return paths.includes(pathResolve(path));
128+
}
129+
return code === 0 && hasPath('test/fixtures/sourcemap-resource-path/index.ts') && hasPath('test/fixtures/sourcemap-resource-path/sum.ts');
130+
}
131+
},
132+
{
133+
args: ["build", "-o", "tmp", "test/fixtures/sourcemap-resource-path/index.ts", "-m", "--source-map", "--no-source-map-register"],
134+
expect (code, stdout, stderr) {
135+
const fs = require('fs');
136+
const map = JSON.parse(fs.readFileSync(join('tmp', 'index.js.map'), 'utf8'));
137+
const paths = map.sources.map(source=>pathResolve(join('tmp', source)));
138+
function hasPath(path) {
139+
return paths.includes(pathResolve(path));
140+
}
141+
return code === 0 && hasPath('test/fixtures/sourcemap-resource-path/index.ts') && hasPath('test/fixtures/sourcemap-resource-path/sum.ts');
142+
}
119143
}
120144
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { sum } from './sum'
2+
3+
const s = sum(1, 2)
4+
console.log(s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function sum(a: number, b: number) {
2+
return a + b
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2015",
4+
}
5+
}

‎test/unit/minify-sourcemap-register/output-coverage.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/unit/minify-sourcemap-register/output.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/unit/minify-v8cache-sourcemap-register/output-coverage.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/unit/minify-v8cache-sourcemap-register/output.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/unit/minify/output-coverage.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/unit/minify/output.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.