Skip to content
Merged
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
52 changes: 27 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fork, ChildProcess } from 'child_process';
import { Compiler, WebpackPluginInstance, Compilation } from 'webpack';
import { ChildProcess, fork } from 'child_process';
import { Compilation, Compiler, WebpackPluginInstance } from 'webpack';

export type RunScriptWebpackPluginOptions = {
autoRestart?: boolean;
Expand Down Expand Up @@ -48,21 +48,18 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
process.stdin.setEncoding('utf8');
process.stdin.on('data', (data: string) => {
if (data.trim() === 'rs') {
this._restartServer()
this._restartServer();
}
});
}
}

private _restartServer():void {
private _restartServer(): void {
console.log('Restarting app...');
if (this.worker?.pid) {
const signal = getSignal(this.options.signal);
process.kill(this.worker.pid, signal);
}
this._startServer((worker) => {
this.worker = worker;
});

this._stopServer();

this._startServer();
}

private afterEmit = (compilation: Compilation, cb: () => void): void => {
Expand All @@ -72,10 +69,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
cb();
return;
}
const signal = getSignal(this.options.signal);
if (signal) {
process.kill(this.worker.pid, signal);
}
this._stopServer();
cb();
return;
}
Expand All @@ -86,7 +80,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
apply = (compiler: Compiler): void => {
compiler.hooks.afterEmit.tapAsync(
{ name: 'RunScriptPlugin' },
this.afterEmit
this.afterEmit,
);
};

Expand All @@ -99,16 +93,16 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
name = options.name;
if (!assets[name]) {
console.error(
`Entry ${name} not found. Try one of: ${names.join(' ')}`
`Entry ${name} not found. Try one of: ${names.join(' ')}`,
);
}
} else {
name = names[0];
if (names.length > 1) {
console.log(
`More than one entry built, selected ${name}. All names: ${names.join(
' '
)}`
' ',
)}`,
);
}
}
Expand All @@ -117,13 +111,10 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
}

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

private _startServer(cb: (arg0: ChildProcess) => void): void {
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.');

Expand All @@ -133,6 +124,17 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
cwd,
env,
});
setTimeout(() => cb(child), 0);

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

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