-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpackx.ts
77 lines (63 loc) · 1.66 KB
/
webpackx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import path from 'path';
import { ChildProcess, spawn } from 'child_process';
import webpack from 'webpack';
import config, { outputPath, outputFilename } from './webpack/webpack.config';
const compilerRunPromise = (compiler) =>
new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
return reject(err);
}
if (stats && stats.hasErrors()) {
reject(err || stats.toString());
}
resolve(stats);
});
});
export function onExit(childProcess: ChildProcess): Promise<void> {
return new Promise((resolve, reject) => {
childProcess.once('exit', (code: number) => {
if (code === 0) {
resolve(undefined);
} else {
reject(new Error(`Exit with error code: ${code}`));
}
});
childProcess.once('error', (err: Error) => {
reject(err);
});
});
}
const runProgram = async () => {
const outputFile = path.join(outputPath, outputFilename);
console.log({
execPath: process.execPath,
outputFile,
});
const childProcess = spawn(
process.execPath,
['--conditions=react-server', outputFile],
{
stdio: [process.stdin, process.stdout, process.stderr],
},
);
await onExit(childProcess);
};
(async () => {
try {
const wpConfig = {
...config,
entry: path.join(__dirname, process.argv[2]),
};
const compiler = webpack(wpConfig);
const stats = await compilerRunPromise(compiler);
// eslint-disable-next-line
console.log(stats.toString());
await runProgram();
} catch (err) {
// eslint-disable-next-line
console.log('err: ', err);
process.exit(1);
}
process.exit(0);
})();