Skip to content

Commit 286e5d3

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): rewire sourcemap back to original source root
Prior to this change, when a error occurs when using the SSR dev-server the stacktraces pointed back to the virtual root path.
1 parent 2b5409b commit 286e5d3

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,25 +534,21 @@ export async function setupServer(
534534
const originalssrTransform = server.ssrTransform;
535535
server.ssrTransform = async (code, map, url, originalCode) => {
536536
const result = await originalssrTransform(code, null, url, originalCode);
537-
if (!result) {
538-
return null;
537+
if (!result || !result.map || !map) {
538+
return result;
539539
}
540540

541-
let transformedCode = result.code;
542-
if (result.map && map) {
543-
transformedCode +=
544-
`\n//# sourceMappingURL=` +
545-
`data:application/json;base64,${Buffer.from(
546-
JSON.stringify(
547-
remapping([result.map as SourceMapInput, map as SourceMapInput], () => null),
548-
),
549-
).toString('base64')}`;
550-
}
541+
const remappedMap = remapping(
542+
[result.map as SourceMapInput, map as SourceMapInput],
543+
() => null,
544+
);
545+
546+
// Set the sourcemap root to the workspace root. This is needed since we set a virtual path as root.
547+
remappedMap.sourceRoot = serverOptions.workspaceRoot + '/';
551548

552549
return {
553550
...result,
554-
map: null,
555-
code: transformedCode,
551+
map: remappedMap as (typeof result)['map'],
556552
};
557553
};
558554

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { doesNotMatch, match } from 'node:assert';
2+
import { killAllProcesses, ng } from '../../utils/process';
3+
import { appendToFile, rimraf } from '../../utils/fs';
4+
import { ngServe, useSha } from '../../utils/project';
5+
import { installWorkspacePackages } from '../../utils/packages';
6+
7+
export default async function () {
8+
// Forcibly remove in case another test doesn't clean itself up.
9+
await rimraf('node_modules/@angular/ssr');
10+
await ng('add', '@angular/ssr', '--skip-confirmation');
11+
await useSha();
12+
await installWorkspacePackages();
13+
14+
try {
15+
// Create Error.
16+
await appendToFile(
17+
'src/app/app.component.ts',
18+
`
19+
(() => {
20+
throw new Error('something happened!');
21+
})();
22+
`,
23+
);
24+
25+
const port = await ngServe();
26+
const response = await fetch(`http://localhost:${port}/`);
27+
const text = await response.text();
28+
29+
// The error is also sent in the browser, so we don't need to scrap the stderr.
30+
match(
31+
text,
32+
/something happened.+at eval \(.+\/e2e-test[\\\/]test-project[\\\/]src[\\\/]app[\\\/]app\.component\.ts:\d+:\d+\)/,
33+
);
34+
doesNotMatch(text, /vite-root/);
35+
} finally {
36+
await killAllProcesses();
37+
}
38+
}

0 commit comments

Comments
 (0)