Fix hotcode reload for new html-webpack-plugin

This commit is contained in:
AkiraFukushima 2020-05-23 21:57:08 +09:00
parent 7cbe1b98da
commit 33439c61cb
1 changed files with 31 additions and 37 deletions

View File

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