Merge pull request #1469 from h3poteto/fix/hot-reload

Fix hotcode reload for new html-webpack-plugin
This commit is contained in:
AkiraFukushima 2020-05-23 22:00:16 +09:00 committed by GitHub
commit 350c82af65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 37 deletions

View File

@ -16,19 +16,22 @@ let electronProcess = null
let manualRestart = false
let hotMiddleware
function logStats (proc, data) {
function logStats(proc, data) {
let log = ''
log += chalk.yellow.bold(`${proc} Process ${new Array((19 - proc.length) + 1).join('-')}`)
log += chalk.yellow.bold(`${proc} Process ${new Array(19 - proc.length + 1).join('-')}`)
log += '\n\n'
if (typeof data === 'object') {
data.toString({
colors: true,
chunks: false
}).split(/\r?\n/).forEach(line => {
log += ' ' + line + '\n'
})
data
.toString({
colors: true,
chunks: false
})
.split(/\r?\n/)
.forEach(line => {
log += ' ' + line + '\n'
})
} else {
log += ` ${data}\n`
}
@ -38,7 +41,7 @@ function logStats (proc, data) {
console.log(log)
}
function startRenderer () {
function startRenderer() {
return new Promise((resolve, reject) => {
rendererConfig.entry.renderer = [path.join(__dirname, 'dev-client')].concat(rendererConfig.entry.renderer)
rendererConfig.mode = 'development'
@ -49,7 +52,8 @@ function startRenderer () {
})
compiler.hooks.compilation.tap('compilation', compilation => {
compilation.hooks.htmlWebpackPluginAfterEmit.tapAsync('html-webpack-plugin-after-emit', (data, cb) => {
const HtmlWebpackPlugin = require('html-webpack-plugin')
HtmlWebpackPlugin.getHooks(compilation).afterEmit.tapAsync('html-webpack-plugin-after-emit', (data, cb) => {
hotMiddleware.publish({ action: 'reload' })
cb()
})
@ -59,25 +63,22 @@ function startRenderer () {
logStats('Renderer', stats)
})
const server = new WebpackDevServer(
compiler,
{
contentBase: path.join(__dirname, '../'),
quiet: true,
before (app, ctx) {
app.use(hotMiddleware)
ctx.middleware.waitUntilValid(() => {
resolve()
})
}
const server = new WebpackDevServer(compiler, {
contentBase: path.join(__dirname, '../'),
quiet: true,
before(app, ctx) {
app.use(hotMiddleware)
ctx.middleware.waitUntilValid(() => {
resolve()
})
}
)
})
server.listen(9080)
})
}
function startMain () {
function startMain() {
return new Promise((resolve, reject) => {
mainConfig.entry.main = [path.join(__dirname, '../src/main/index.dev.ts')].concat(mainConfig.entry.main)
mainConfig.mode = 'development'
@ -113,7 +114,7 @@ function startMain () {
})
}
function startElectron () {
function startElectron() {
electronProcess = spawn(electron, ['--inspect=5858', path.join(__dirname, '../dist/electron/main.js')])
electronProcess.stdout.on('data', data => {
@ -127,11 +128,8 @@ function startElectron () {
if (!manualRestart) process.exit()
})
}
function startElectron () {
var args = [
'--inspect=5858',
path.join(__dirname, '../dist/electron/main.js')
]
function startElectron() {
var args = ['--inspect=5858', path.join(__dirname, '../dist/electron/main.js')]
// detect yarn or npm and process commandline args accordingly
if (process.env.npm_execpath.endsWith('yarn.js')) {
@ -154,7 +152,7 @@ function startElectron () {
})
}
function electronLog (data, color) {
function electronLog(data, color) {
let log = ''
data = data.toString().split(/\r?\n/)
data.forEach(line => {
@ -162,16 +160,12 @@ function electronLog (data, color) {
})
if (/[0-9A-z]+/.test(log)) {
console.log(
chalk[color].bold('┏ Electron -------------------') +
'\n\n' +
log +
chalk[color].bold('┗ ----------------------------') +
'\n'
chalk[color].bold('┏ Electron -------------------') + '\n\n' + log + chalk[color].bold('┗ ----------------------------') + '\n'
)
}
}
function greeting () {
function greeting() {
const cols = process.stdout.columns
let text = ''
@ -189,7 +183,7 @@ function greeting () {
console.log(chalk.blue(' getting ready...') + '\n')
}
function init () {
function init() {
greeting()
Promise.all([startRenderer(), startMain()])