Skip to content

Commit

Permalink
fix: sourcemap sources removes webpack path (#1122)
Browse files Browse the repository at this point in the history
- Fixes #1011 
- Fixes #1121 

The source is now in the format `webpack://[namespace]/[resourcePath]`
  • Loading branch information
axuj authored Oct 18, 2023
1 parent f9c1153 commit ce5984e
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 33 deletions.
30 changes: 4 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 25 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { join } = require('path')
const { join, resolve: pathResolve } = require('path');

module.exports = [
{
Expand Down Expand Up @@ -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');
}
}
]
4 changes: 4 additions & 0 deletions test/fixtures/sourcemap-resource-path/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sum } from './sum'

const s = sum(1, 2)
console.log(s)
3 changes: 3 additions & 0 deletions test/fixtures/sourcemap-resource-path/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sum(a: number, b: number) {
return a + b
}
5 changes: 5 additions & 0 deletions test/fixtures/sourcemap-resource-path/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "es2015",
}
}
2 changes: 1 addition & 1 deletion test/unit/minify-sourcemap-register/output-coverage.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/unit/minify-sourcemap-register/output.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/unit/minify-v8cache-sourcemap-register/output.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/unit/minify/output-coverage.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/unit/minify/output.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ce5984e

Please sign in to comment.