fix: spawning

This commit is contained in:
cha0s 2024-02-07 19:33:57 -06:00
parent 38ea408ba3
commit 3f7f2573d8
3 changed files with 76 additions and 70 deletions

View File

@ -229,28 +229,17 @@ exports.commands = (program, flecks) => {
'--config', webpackConfig,
'--mode', (production && !hot) ? 'production' : 'development',
];
let webpack;
const spawnWebpack = () => {
webpack = spawnWith(
cmd,
{
const options = {
env: {
FLECKS_BUILD_IS_PRODUCTION: production,
...(target ? {FLECKS_CORE_BUILD_LIST: target} : {}),
...(hot ? {FLECKS_ENV__flecks_server__hot: 'true'} : {}),
},
useFork: true,
},
);
webpack.on('message', (message) => {
if ('restart' === message) {
webpack.kill();
spawnWebpack();
}
});
};
spawnWebpack();
if (watch) {
if (!watch) {
return spawnWith(cmd, options);
}
await rootsDependencies(flecks.roots, flecks.resolver);
const watched = Object.keys(dependencies);
watched.push(
@ -265,6 +254,26 @@ exports.commands = (program, flecks) => {
pollInterval: 5,
},
});
let webpack;
const spawnWebpack = () => {
webpack = spawnWith(cmd, options);
webpack.on('message', (message) => {
switch (message) {
case 'kill':
debug('killing...');
webpack.kill();
watcher.close();
break;
case 'restart':
debug('restarting webpack...');
webpack.kill();
spawnWebpack();
break;
default:
}
});
};
spawnWebpack();
await new Promise((resolve, reject) => {
watcher.on('error', reject);
watcher.on('ready', resolve);
@ -287,12 +296,13 @@ exports.commands = (program, flecks) => {
respawn = true;
}
if (respawn) {
debug('Respawning...');
debug('restarting webpack...');
webpack.kill();
spawnWebpack();
}
});
}
// Persist...
return new Promise(() => {});
},
};
}

View File

@ -62,7 +62,6 @@ module.exports = async (env, argv, flecks) => {
NODE_PRESERVE_SYMLINKS: flecks.roots.some(([path, request]) => path !== request) ? 1 : 0,
},
exec: 'index.js',
killOnExit: !hot,
// Bail hard on unhandled rejections and report.
nodeArgs: [...nodeArgs, '--unhandled-rejections=strict', '--trace-uncaught'],
// HMR.

View File

@ -92,20 +92,17 @@ class StartServerPlugin {
...(inspectPort && {inspectPort}),
});
this.worker = cluster.fork(env);
if (killOnExit) {
this.worker.on('exit', (code) => {
process.exit(code);
});
}
else {
this.worker.on('disconnect', () => {
if (this.worker.exitedAfterDisconnect) {
// eslint-disable-next-line no-console
console.error('[HMR] Restarting application...');
process.send('restart');
}
});
else if (killOnExit) {
process.send('kill');
process.exit(0);
}
});
return new Promise((resolve, reject) => {
this.worker.on('error', reject);
this.worker.on('online', resolve);