flecks/packages/server/build/start.js

98 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-12-03 10:47:16 -06:00
const cluster = require('cluster');
2023-11-30 21:41:42 -06:00
const {join} = require('path');
2022-12-03 10:47:16 -06:00
class StartServerPlugin {
pluginName = 'StartServerPlugin';
worker = null;
constructor(options = {}) {
2023-12-01 05:34:33 -06:00
this.options = {
args: [],
2024-01-20 03:40:20 -06:00
env: {},
2023-12-01 05:34:33 -06:00
killOnExit: true,
nodeArgs: [],
signal: false,
...('string' === typeof options ? {name: options} : options),
};
2022-12-03 10:47:16 -06:00
}
apply(compiler) {
const {options: {exec, signal}, pluginName} = this;
const logger = compiler.getInfrastructureLogger(pluginName);
compiler.hooks.afterEmit.tapAsync(pluginName, (compilation, callback) => {
if (this.worker && this.worker.isConnected()) {
if (signal) {
process.kill(this.worker.process.pid, true === signal ? 'SIGUSR2' : signal);
}
callback();
return;
}
let entryPoint;
if (!exec) {
2023-11-30 21:41:42 -06:00
entryPoint = compilation.getPath(Object.keys(compilation.assets)[0]);
2022-12-03 10:47:16 -06:00
}
else if (compilation.assets[exec]) {
2023-11-30 21:41:42 -06:00
entryPoint = compilation.getPath(exec);
2022-12-03 10:47:16 -06:00
}
else if ('string' === typeof exec) {
entryPoint = exec;
}
else {
entryPoint = exec(compilation);
}
2023-11-30 21:41:42 -06:00
this.startServer(join(compiler.options.output.path, entryPoint), callback);
2022-12-03 10:47:16 -06:00
});
compiler.hooks.shouldEmit.tap(pluginName, (compilation) => {
const entryPoints = Object.keys(compilation.assets);
if ('string' === typeof exec) {
return true;
}
if (!exec && entryPoints.length > 1) {
logger.warn(`No entrypoint selected, defaulting to '${entryPoints[0]}'. Other entrypoints: '${entryPoints.slice(1).join("', '")}'`);
}
return true;
});
}
static inspectPortFromExecArgv(execArgv) {
const inspectArg = execArgv.find((arg) => arg.includes('--inspect'));
if (!inspectArg || !inspectArg.includes('=')) {
return undefined;
}
const [, hostPort] = inspectArg.split('=');
const port = hostPort.includes(':') ? hostPort.split(':')[1] : hostPort;
return parseInt(port, 10);
}
startServer(exec, callback) {
2024-01-20 03:40:20 -06:00
const {
args,
env,
killOnExit,
nodeArgs,
} = this.options;
2022-12-03 10:47:16 -06:00
const execArgv = nodeArgs.concat(process.execArgv);
const inspectPort = this.constructor.inspectPortFromExecArgv(execArgv);
cluster.setupPrimary({
exec,
execArgv,
args,
...(inspectPort && {inspectPort}),
});
cluster.on('online', () => callback());
2024-01-20 03:40:20 -06:00
this.worker = cluster.fork(env);
2023-12-01 05:34:33 -06:00
if (killOnExit) {
this.worker.on('exit', () => {
process.exit();
});
}
2022-12-03 10:47:16 -06:00
}
}
2023-11-30 21:41:42 -06:00
module.exports = (pluginOptions = {}) => (
new StartServerPlugin(pluginOptions)
2022-12-03 10:47:16 -06:00
);