Skip to content

Commit

Permalink
fix: correctly terminate dev server processes
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Aug 6, 2017
1 parent d9eb53a commit 390de93
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 1,935 deletions.
2 changes: 1 addition & 1 deletion electron-webpack.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/test/src/__snapshots__" />
<excludePattern pattern="*packages/*.js" />
<excludePattern pattern="*.js" />
<excludePattern pattern="*.js.map" />
<excludePattern pattern="yarn.lock" />
</content>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"update-deps": "lerna exec -- npm-check-updates -a",
"compile": "ts-babel packages/electron-webpack",
"lint": "tslint -c ./node_modules/electron-builder-tslint-config/tslint.json -p packages/electron-webpack --exclude '**/*.js'",
"release": "yarn compile && npm publish packages/electron-webpack",
"release": "yarn compile && npm publish packages/electron-webpack && conventional-changelog -p angular -i CHANGELOG.md -s",
"test": "jest",
"update-wiki": "(git branch -D wiki || true) && git subtree split -b wiki --prefix docs/ && git push -f wiki wiki:master"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/electron-webpack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-webpack",
"version": "0.10.1",
"version": "0.10.2",
"license": "MIT",
"author": "Vladimir Krivosheev <[email protected]>",
"files": [
Expand All @@ -16,6 +16,7 @@
"//": "@types/webpack-env in the dependencies because used by clients",
"dependencies": {
"@types/webpack-env": "^1.13.0",
"async-exit-hook": "^2.0.1",
"babili-webpack-plugin": "^0.1.2",
"bluebird-lst": "^1.0.2",
"chalk": "^2.0.1",
Expand All @@ -29,6 +30,7 @@
"fs-extra-p": "^4.4.0",
"html-loader": "^0.5.0",
"html-webpack-plugin": "^2.30.1",
"lazy-val": "^1.0.1",
"read-config-file": "^0.1.3",
"semver": "^5.4.1",
"source-map-support": "^0.4.15",
Expand Down
36 changes: 22 additions & 14 deletions packages/electron-webpack/src/dev/WebpackDevServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BluebirdPromise from "bluebird-lst"
import { blue, red } from "chalk"
import { ChildProcess, spawn } from "child_process"
import * as path from "path"
import { getCommonEnv, LineFilter, logError, logProcess, logProcessErrorOutput, onDeath } from "./DevRunnerUtil"
import { getCommonEnv, LineFilter, logError, logProcess, logProcessErrorOutput } from "./devUtil"

const debug = require("debug")("electron-webpack:dev-runner")

Expand All @@ -22,28 +22,32 @@ export function startRenderer(projectDir: string) {
new OneTimeLineFilter("webpack output is served from "),
])
return new BluebirdPromise((resolve: (() => void) | null, reject: ((error: Error) => void) | null) => {
let webpackDevServer: ChildProcess | null
let devServerProcess: ChildProcess | null
try {
webpackDevServer = spawnWds(projectDir)
devServerProcess = spawnWds(projectDir)
}
catch (e) {
reject!(e)
return
}

onDeath(eventName => {
if (webpackDevServer == null) {
require("async-exit-hook")(() => {
const server = devServerProcess
if (server == null) {
return
}

if (debug.enabled) {
debug(`Kill webpackDevServer on ${eventName}`)
devServerProcess = null

const r = resolve
if (r != null) {
resolve = null
r()
}
webpackDevServer.kill("SIGINT")
webpackDevServer = null
server.kill("SIGINT")
})

webpackDevServer.on("error", error => {
devServerProcess.on("error", error => {
if (reject == null) {
logError("Renderer", error)
}
Expand All @@ -53,7 +57,7 @@ export function startRenderer(projectDir: string) {
}
})

webpackDevServer.stdout.on("data", (data: string) => {
devServerProcess.stdout.on("data", (data: string) => {
logProcess("Renderer", data, blue, lineFilter)

const r = resolve
Expand All @@ -64,10 +68,14 @@ export function startRenderer(projectDir: string) {
}
})

logProcessErrorOutput("Renderer", webpackDevServer)
logProcessErrorOutput("Renderer", devServerProcess)

devServerProcess.on("close", code => {
if (devServerProcess == null) {
return
}

webpackDevServer.on("close", code => {
webpackDevServer = null
devServerProcess = null

const message = `webpackDevServer process exited with code ${code}`

Expand Down
9 changes: 4 additions & 5 deletions packages/electron-webpack/src/dev/dev-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Compiler, Stats } from "webpack"
import { HmrServer } from "../../electron-main-hmr/HmrServer"
import { orNullIfFileNotExist } from "../util"
import { configure } from "../webpackConfigurator"
import { DelayedFunction, getCommonEnv, logError, logProcess, logProcessErrorOutput, onDeath } from "./DevRunnerUtil"
import { DelayedFunction, getCommonEnv, logError, logProcess, logProcessErrorOutput } from "./devUtil"
import { startRenderer } from "./WebpackDevServerManager"

const webpack = require("webpack")
Expand Down Expand Up @@ -83,7 +83,6 @@ class DevRunner {
printCompilingMessage.cancel()

if (watcher == null) {
process.exit(0)
return
}

Expand Down Expand Up @@ -111,14 +110,14 @@ class DevRunner {
hmrServer.built(stats)
})

onDeath(() => {
require("async-exit-hook")((callback: () => void) => {
const w = watcher
if (w == null) {
return
}

watcher = null;
(w as any).close()
watcher = null
w.close(() => callback())
})
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,4 @@ export function getCommonEnv() {
DEBUG_COLORS: true,
DEBUG_FD: "1",
}
}

export function onDeath(handler: (eventName: string) => void) {
function registerListener(eventName: string) {
process.on(eventName as any, () => handler(eventName))
}

registerListener("beforeExit")
registerListener("exit")
registerListener("SIGINT")
registerListener("SIGQUIT")

process.on("uncaughtException", error => {
process.stderr.write(error.stack || error.toString())
handler("uncaughtException")
})
}
23 changes: 0 additions & 23 deletions packages/electron-webpack/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,6 @@ export function orIfFileNotExist<T>(promise: Promise<T>, fallbackValue: T): Prom
})
}

export class Lazy<T> {
private _value: Promise<T>
private creator: (() => Promise<T>) | null

get value(): Promise<T> {
if (this.creator == null) {
return this._value
}

this.value = this.creator()
return this._value
}

set value(value: Promise<T>) {
this._value = value
this.creator = null
}

constructor(creator: () => Promise<T>) {
this.creator = creator
}
}

export function getFirstExistingFile(names: Array<string>, rootDir: string | null): Promise<string | null> {
return BluebirdPromise.filter(names.map(it => rootDir == null ? it : path.join(rootDir, it)), it => statOrNull(it).then(it => it != null))
.then(it => it.length > 0 ? it[0] : null)
Expand Down
3 changes: 2 additions & 1 deletion packages/electron-webpack/src/webpackConfigurator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BluebirdPromise from "bluebird-lst"
import { readJson } from "fs-extra-p"
import { Lazy } from "lazy-val"
import * as path from "path"
import "source-map-support/register"
import { Configuration, Plugin, Rule } from "webpack"
Expand All @@ -9,7 +10,7 @@ import { ConfigurationEnv, ConfigurationType, ElectronWebpackConfig, PackageMeta
import { BaseTarget } from "./targets/BaseTarget"
import { MainTarget } from "./targets/MainTarget"
import { BaseRendererTarget, RendererTarget } from "./targets/RendererTarget"
import { getFirstExistingFile, Lazy, orNullIfFileNotExist } from "./util"
import { getFirstExistingFile, orNullIfFileNotExist } from "./util"

const _debug = require("debug")

Expand Down
Loading

0 comments on commit 390de93

Please sign in to comment.