Skip to content

Commit 5376e69

Browse files
authored
module: add sourceURL magic comment hinting generated source
Source map is not necessary in strip-only mode. However, to map the source file in debuggers to the original TypeScript source, add a sourceURL magic comment to hint that it is a generated source. PR-URL: #54402 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 68e94c1 commit 5376e69

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

lib/internal/modules/helpers.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ function stripTypeScriptTypes(source, filename) {
371371
const base64SourceMap = Buffer.from(map).toString('base64');
372372
return `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`;
373373
}
374-
return code;
374+
// Source map is not necessary in strip-only mode. However, to map the source
375+
// file in debuggers to the original TypeScript source, add a sourceURL magic
376+
// comment to hint that it is a generated source.
377+
return `${code}\n\n//# sourceURL=${filename}`;
375378
}
376379

377380
function isUnderNodeModules(filename) {

test/common/inspector-helper.js

+8
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ class InspectorSession {
271271
`break on ${url}:${line}`);
272272
}
273273

274+
waitForPauseOnStart() {
275+
return this
276+
.waitForNotification(
277+
(notification) =>
278+
notification.method === 'Debugger.paused' && notification.params.reason === 'Break on start',
279+
'break on start');
280+
}
281+
274282
pausedDetails() {
275283
return this._pausedDetails;
276284
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
common.skipIfInspectorDisabled();
5+
if (!process.config.variables.node_use_amaro) common.skip('Requires Amaro');
6+
7+
const { NodeInstance } = require('../common/inspector-helper.js');
8+
const fixtures = require('../common/fixtures');
9+
const assert = require('assert');
10+
11+
const scriptPath = fixtures.path('typescript/ts/test-typescript.ts');
12+
13+
async function runTest() {
14+
const child = new NodeInstance(
15+
['--inspect-brk=0', '--experimental-strip-types'],
16+
undefined,
17+
scriptPath);
18+
19+
const session = await child.connectInspectorSession();
20+
21+
const commands = [
22+
{ 'method': 'Debugger.enable' },
23+
{ 'method': 'Runtime.enable' },
24+
{ 'method': 'Runtime.runIfWaitingForDebugger' },
25+
];
26+
27+
await session.send(commands);
28+
29+
const scriptParsed = await session.waitForNotification((notification) => {
30+
if (notification.method !== 'Debugger.scriptParsed') return false;
31+
32+
return notification.params.url === scriptPath;
33+
});
34+
// Verify that the script has a sourceURL, hinting that it is a generated source.
35+
assert(scriptParsed.params.hasSourceURL);
36+
37+
await session.waitForPauseOnStart();
38+
await session.runToCompletion();
39+
40+
assert.strictEqual((await child.expectShutdown()).exitCode, 0);
41+
}
42+
43+
runTest().then(common.mustCall());

0 commit comments

Comments
 (0)