-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrunWebPackSync.js
41 lines (35 loc) · 1.27 KB
/
runWebPackSync.js
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
import { join } from 'path';
import { readFileSync } from 'fs';
import rimraf from 'rimraf';
import colors from 'colors/safe';
import { tmpdir } from 'os';
import { quote } from 'shell-quote';
import { execSync } from 'child_process';
export default ({ path, configPath, config, verbose }) => {
const DEFAULT_OUTPUT_PATH = tmpdir();
const webPackPath = require.resolve('webpack/bin/webpack');
const rnd = `${(new Date()).getTime()}_${Math.round(1000000 * Math.random())}`;
const outPath = join(config.output.path || DEFAULT_OUTPUT_PATH, `.webpack.res.${rnd}.js`);
// I need to run webpack via execSync because I have not found the way how to run
// babel visitors asynchronously or run webpack compile synchronously
const webPackStdOut = execSync(
quote([
'node', // for windows support
webPackPath,
path, outPath,
'--config', configPath,
'--colors', '--bail',
])
);
if (verbose) {
console.error( // eslint-disable-line
colors.blue(`Webpack stdout for ${path}\n`) + // eslint-disable-line prefer-template
colors.blue('---------\n') +
`${webPackStdOut}\n` +
colors.blue('---------')
);
}
const webPackResult = readFileSync(outPath, { encoding: 'utf8' });
rimraf.sync(outPath);
return webPackResult;
};