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,70 +229,80 @@ exports.commands = (program, flecks) => {
'--config', webpackConfig, '--config', webpackConfig,
'--mode', (production && !hot) ? 'production' : 'development', '--mode', (production && !hot) ? 'production' : 'development',
]; ];
const options = {
env: {
FLECKS_BUILD_IS_PRODUCTION: production,
...(target ? {FLECKS_CORE_BUILD_LIST: target} : {}),
...(hot ? {FLECKS_ENV__flecks_server__hot: 'true'} : {}),
},
useFork: true,
};
if (!watch) {
return spawnWith(cmd, options);
}
await rootsDependencies(flecks.roots, flecks.resolver);
const watched = Object.keys(dependencies);
watched.push(
...await Promise.all(
flecks.roots.map(([, request]) => flecks.resolver.resolve(join(request, 'package.json'))),
),
);
watched.push(join(FLECKS_CORE_ROOT, 'build/flecks.yml'));
const watcher = chokidar.watch(watched, {
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 5,
},
});
let webpack; let webpack;
const spawnWebpack = () => { const spawnWebpack = () => {
webpack = spawnWith( webpack = spawnWith(cmd, options);
cmd,
{
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) => { webpack.on('message', (message) => {
if ('restart' === message) { switch (message) {
webpack.kill(); case 'kill':
spawnWebpack(); debug('killing...');
webpack.kill();
watcher.close();
break;
case 'restart':
debug('restarting webpack...');
webpack.kill();
spawnWebpack();
break;
default:
} }
}); });
}; };
spawnWebpack(); spawnWebpack();
if (watch) { await new Promise((resolve, reject) => {
await rootsDependencies(flecks.roots, flecks.resolver); watcher.on('error', reject);
const watched = Object.keys(dependencies); watcher.on('ready', resolve);
watched.push( });
...await Promise.all( const configPath = join(FLECKS_CORE_ROOT, 'build', 'flecks.yml');
flecks.roots.map(([, request]) => flecks.resolver.resolve(join(request, 'package.json'))), const initialConfig = loadYml(await readFile(configPath));
), watcher.on('all', async (event, path) => {
); let respawn = false;
watched.push(join(FLECKS_CORE_ROOT, 'build/flecks.yml')); if (configPath === path) {
const watcher = chokidar.watch(watched, { const config = loadYml(await readFile(configPath));
awaitWriteFinish: { if (
stabilityThreshold: 50, JSON.stringify(Object.keys(initialConfig).sort())
pollInterval: 5, !== JSON.stringify(Object.keys(config).sort())
}, ) {
}); debug('Config keys changed');
await new Promise((resolve, reject) => {
watcher.on('error', reject);
watcher.on('ready', resolve);
});
const configPath = join(FLECKS_CORE_ROOT, 'build', 'flecks.yml');
const initialConfig = loadYml(await readFile(configPath));
watcher.on('all', async (event, path) => {
let respawn = false;
if (configPath === path) {
const config = loadYml(await readFile(configPath));
if (
JSON.stringify(Object.keys(initialConfig).sort())
!== JSON.stringify(Object.keys(config).sort())
) {
debug('Config keys changed');
respawn = true;
}
}
else {
respawn = true; respawn = true;
} }
if (respawn) { }
debug('Respawning...'); else {
webpack.kill(); respawn = true;
spawnWebpack(); }
} if (respawn) {
}); 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, NODE_PRESERVE_SYMLINKS: flecks.roots.some(([path, request]) => path !== request) ? 1 : 0,
}, },
exec: 'index.js', exec: 'index.js',
killOnExit: !hot,
// Bail hard on unhandled rejections and report. // Bail hard on unhandled rejections and report.
nodeArgs: [...nodeArgs, '--unhandled-rejections=strict', '--trace-uncaught'], nodeArgs: [...nodeArgs, '--unhandled-rejections=strict', '--trace-uncaught'],
// HMR. // HMR.

View File

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