Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 46 additions & 48 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,72 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
}
}

public restartServer(): void {
console.log('Restarting app...');

this.stopServer();

this.startServer();
}

public startServer(cb?: () => void): void {
const { args, nodeArgs, cwd, env } = this.options;
if (!this._entrypoint) throw new Error('run-script-webpack-plugin requires an entrypoint.');

const child = fork(this._entrypoint, args, {
execArgv: nodeArgs,
stdio: 'inherit',
cwd,
env,
});

setTimeout(() => {
this.worker = child;
cb?.();
}, 0);
}

public stopServer() {
const signal = getSignal(this.options.signal);
if (this.worker?.pid) {
process.kill(this.worker.pid, signal);
}
};

apply = (compiler: Compiler): void => {
compiler.hooks.afterEmit.tapAsync(
{ name: 'RunScriptPlugin' },
this.afterEmit,
);
};

private _enableRestarting(): void {
if (this.options.keyboard) {
process.stdin.setEncoding('utf8');
process.stdin.on('data', (data: string) => {
if (data.trim() === 'rs') {
this._restartServer();
this.restartServer();
}
});
}
}

private _restartServer(): void {
console.log('Restarting app...');

this._stopServer();

this._startServer();
}

private afterEmit = (compilation: Compilation, cb: () => void): void => {
if (this.worker && this.worker.connected && this.worker?.pid) {
if (this.options.autoRestart) {
this._restartServer();
this.restartServer();
cb();
return;
}
this._stopServer();
this.stopServer();
cb();
return;
}

this.startServer(compilation, cb);
this._startServer(compilation, cb);
};

apply = (compiler: Compiler): void => {
compiler.hooks.afterEmit.tapAsync(
{ name: 'RunScriptPlugin' },
this.afterEmit,
);
};

private startServer = (compilation: Compilation, cb: () => void): void => {
private _startServer = (compilation: Compilation, cb: () => void): void => {
const { assets, compiler } = compilation;
const { options } = this;
let name;
Expand All @@ -100,9 +124,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
name = names[0];
if (names.length > 1) {
console.log(
`More than one entry built, selected ${name}. All names: ${names.join(
' ',
)}`,
`More than one entry built, selected ${name}. All names: ${names.join(' ')}`,
);
}
}
Expand All @@ -111,30 +133,6 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
}

this._entrypoint = `${compiler.options.output.path}/${name}`;
this._startServer(cb);
};

private _startServer(cb?: () => void): void {
const { args, nodeArgs, cwd, env } = this.options;
if (!this._entrypoint) throw new Error('run-script-webpack-plugin requires an entrypoint.');

const child = fork(this._entrypoint, args, {
execArgv: nodeArgs,
stdio: 'inherit',
cwd,
env,
});

setTimeout(() => {
this.worker = child;
cb?.()
}, 0);
}

private _stopServer() {
const signal = getSignal(this.options.signal);
if (this.worker?.pid) {
process.kill(this.worker.pid, signal);
}
this.startServer(cb);
};
}