Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sourcemap sources removes webpack path #1122

Merged
merged 4 commits into from
Oct 18, 2023
Merged
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
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",
}
}

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.

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.