From 38302fc650b4326bc516348c45f98f16f52ea003 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Thu, 15 Aug 2024 23:06:04 +0100 Subject: [PATCH 1/2] 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. --- lib/internal/modules/helpers.js | 5 ++- test/parallel/test-inspector-strip-types.js | 41 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-inspector-strip-types.js diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index 890d851f5bd88f..729a33e04f34cd 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -371,7 +371,10 @@ function stripTypeScriptTypes(source, filename) { const base64SourceMap = Buffer.from(map).toString('base64'); return `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`; } - return code; + // 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. + return `${code}\n\n//# sourceURL=${filename}`; } function isUnderNodeModules(filename) { diff --git a/test/parallel/test-inspector-strip-types.js b/test/parallel/test-inspector-strip-types.js new file mode 100644 index 00000000000000..e8af6791b23ecb --- /dev/null +++ b/test/parallel/test-inspector-strip-types.js @@ -0,0 +1,41 @@ +'use strict'; + +const common = require('../common'); +common.skipIfInspectorDisabled(); +if (!process.config.variables.node_use_amaro) common.skip('Requires Amaro'); + +const { NodeInstance } = require('../common/inspector-helper.js'); +const fixtures = require('../common/fixtures'); +const assert = require('assert'); + +const scriptPath = fixtures.path('typescript/ts/test-typescript.ts'); + +async function runTest() { + const child = new NodeInstance( + ['--inspect-brk=0', '--experimental-strip-types'], + undefined, + scriptPath); + + const session = await child.connectInspectorSession(); + + const commands = [ + { 'method': 'Debugger.enable' }, + { 'method': 'Runtime.enable' }, + { 'method': 'Runtime.runIfWaitingForDebugger' }, + ]; + + await session.send(commands); + + const scriptParsed = await session.waitForNotification((notification) => { + if (notification.method !== 'Debugger.scriptParsed') return false; + + return notification.params.url === scriptPath; + }); + // Verify that the script has a sourceURL, hinting that it is a generated source. + assert(scriptParsed.params.hasSourceURL); + + session.disconnect(); + assert.strictEqual((await child.expectShutdown()).exitCode, 0); +} + +runTest().then(common.mustCall()); From 95bbe2ed0321392cc37a1ef31764f23cd96c84e5 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Tue, 20 Aug 2024 00:59:33 +0100 Subject: [PATCH 2/2] fixup! module: add sourceURL magic comment hinting generated source --- test/common/inspector-helper.js | 8 ++++++++ test/parallel/test-inspector-strip-types.js | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/test/common/inspector-helper.js b/test/common/inspector-helper.js index 4890fa68c46110..9d5e3b15aece4b 100644 --- a/test/common/inspector-helper.js +++ b/test/common/inspector-helper.js @@ -271,6 +271,14 @@ class InspectorSession { `break on ${url}:${line}`); } + waitForPauseOnStart() { + return this + .waitForNotification( + (notification) => + notification.method === 'Debugger.paused' && notification.params.reason === 'Break on start', + 'break on start'); + } + pausedDetails() { return this._pausedDetails; } diff --git a/test/parallel/test-inspector-strip-types.js b/test/parallel/test-inspector-strip-types.js index e8af6791b23ecb..68e2463d530e8f 100644 --- a/test/parallel/test-inspector-strip-types.js +++ b/test/parallel/test-inspector-strip-types.js @@ -34,7 +34,9 @@ async function runTest() { // Verify that the script has a sourceURL, hinting that it is a generated source. assert(scriptParsed.params.hasSourceURL); - session.disconnect(); + await session.waitForPauseOnStart(); + await session.runToCompletion(); + assert.strictEqual((await child.expectShutdown()).exitCode, 0); }