From 520949c5e140da423cba30363834caba81d42a14 Mon Sep 17 00:00:00 2001 From: Nikolai Sivertsen Date: Wed, 12 Sep 2018 16:55:45 +0200 Subject: [PATCH] Enable debugging in Chrome and VS Code - fixes 435 --- src/api/dev.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/api/dev.ts b/src/api/dev.ts index c996b06..b3f9e45 100644 --- a/src/api/dev.ts +++ b/src/api/dev.ts @@ -36,6 +36,8 @@ class Watcher extends EventEmitter { live: boolean; hot: boolean; + devtools_port: number; + dev_server: DevServer; proc: child_process.ChildProcess; filewatchers: Array<{ close: () => void }>; @@ -57,6 +59,7 @@ class Watcher extends EventEmitter { 'dev-port': dev_port, live, hot, + 'devtools-port': devtools_port, bundler, webpack = 'webpack', rollup = 'rollup', @@ -68,6 +71,7 @@ class Watcher extends EventEmitter { 'dev-port': number, live: boolean, hot: boolean, + 'devtools-port': number, bundler?: string, webpack: string, rollup: string, @@ -84,6 +88,8 @@ class Watcher extends EventEmitter { this.live = live; this.hot = hot; + this.devtools_port = devtools_port; + this.filewatchers = []; this.current_build = { @@ -129,6 +135,9 @@ class Watcher extends EventEmitter { if (!this.dev_port) this.dev_port = await ports.find(10000); + // Chrome looks for debugging targets on ports 9222 and 9229 by default + if (!this.devtools_port) this.devtools_port = await ports.find(9222); + let manifest_data: ManifestData; try { @@ -238,12 +247,21 @@ class Watcher extends EventEmitter { restart(); } + // we need to give the child process its own DevTools port, + // otherwise Node will try to use the parent's (and fail) + const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/; + const execArgv = process.execArgv.slice(); + if (execArgv.some((arg: string) => !!arg.match(debugArgRegex))) { + execArgv.push(`--inspect-port=${this.devtools_port}`); + } + this.proc = child_process.fork(`${dest}/server.js`, [], { cwd: process.cwd(), env: Object.assign({ PORT: this.port }, process.env), - stdio: ['ipc'] + stdio: ['ipc'], + execArgv }); this.proc.stdout.on('data', chunk => {