1 | const path = require('path');
|
2 |
|
3 | const camelcase = require('camelcase');
|
4 | const capitalize = require('titleize');
|
5 | const chalk = require('chalk');
|
6 | const merge = require('merge-options');
|
7 | const webpack = require('webpack');
|
8 | const weblog = require('webpack-log');
|
9 |
|
10 | const progress = require('./progress');
|
11 | const StylishReporter = require('./reporters/StylishReporter');
|
12 |
|
13 | function makeCallback(options) {
|
14 | const { compiler, config, reporter, resolve, reject } = options;
|
15 | const { watch } = config;
|
16 |
|
17 | return (error, stats) => {
|
18 | const log = weblog({ name: 'webpack', id: 'webpack-command' });
|
19 |
|
20 | if (error || !watch) {
|
21 |
|
22 |
|
23 | compiler.purgeInputFileSystem();
|
24 | log.debug('Input File System Purged');
|
25 | }
|
26 |
|
27 | const result = reporter.render(error, stats);
|
28 |
|
29 | if (error) {
|
30 | reject(error);
|
31 | return;
|
32 | } else if (stats && stats.hasErrors() && !watch) {
|
33 | process.exitCode = 1;
|
34 |
|
35 | process.stdout.write('\n');
|
36 | }
|
37 |
|
38 | resolve(result);
|
39 | };
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function requireReporter(name) {
|
49 | const prefix = capitalize(camelcase(name));
|
50 | const locations = [`./reporters/${prefix}Reporter`, name, path.resolve(name)];
|
51 | let result = null;
|
52 |
|
53 | for (const location of locations) {
|
54 | try {
|
55 |
|
56 | result = require(location);
|
57 | } catch (e) {
|
58 |
|
59 | }
|
60 |
|
61 | if (result) {
|
62 | break;
|
63 | }
|
64 | }
|
65 |
|
66 | return result;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function sanitize(config) {
|
73 | const configs = [].concat(config).map((conf) => {
|
74 | const result = merge({}, conf);
|
75 | delete result.progress;
|
76 | delete result.reporter;
|
77 | delete result.watchStdin;
|
78 |
|
79 |
|
80 | for (const property of ['entry', 'output']) {
|
81 | const target = result[property];
|
82 | if (target && Object.keys(target).length === 0) {
|
83 | delete result[property];
|
84 | }
|
85 | }
|
86 |
|
87 | return result;
|
88 | });
|
89 |
|
90 |
|
91 | return configs.length > 1 ? configs : configs[0];
|
92 | }
|
93 |
|
94 | module.exports = (config) => {
|
95 | const log = weblog({ name: 'webpack', id: 'webpack-command' });
|
96 | const target = sanitize(config);
|
97 | const compiler = webpack(target);
|
98 | const { done, run } = compiler.hooks;
|
99 | const configs = [].concat(config);
|
100 | const [first] = configs;
|
101 | const { reporter: reporterName, watchOptions } = first;
|
102 | let ReporterClass = requireReporter(reporterName || 'stylish');
|
103 |
|
104 | if (!ReporterClass) {
|
105 | log.error(`The reporter specified (${reporterName}) could not be located`);
|
106 | ReporterClass = StylishReporter;
|
107 | }
|
108 |
|
109 | const reporter = new ReporterClass({ compiler, config });
|
110 |
|
111 | if (first.progress) {
|
112 | progress.apply(first, compiler, reporter);
|
113 | }
|
114 |
|
115 | run.tap('WebpackCommand', () => {
|
116 | log.info('Starting Build');
|
117 | });
|
118 |
|
119 | done.tap('WebpackCommand', () => {
|
120 | log.info('Build Finished');
|
121 | });
|
122 |
|
123 | return {
|
124 | run() {
|
125 | return new Promise((resolve, reject) => {
|
126 | const { stdin } = watchOptions || {};
|
127 | const callback = makeCallback({
|
128 | compiler,
|
129 | config: first,
|
130 | reporter,
|
131 | resolve,
|
132 | reject
|
133 | });
|
134 |
|
135 | if (first.watch) {
|
136 | log.info('Watching enabled');
|
137 | for (const conf of configs) {
|
138 | if (conf.bail) {
|
139 | const configName =
|
140 | configs.length > 1 ? `config[${conf.name || configs.indexOf(conf)}]` : 'the config';
|
141 | log.warn(
|
142 | chalk`The {bold \`bail\`} option in ${configName} will force webpack to exit on the first error`
|
143 | );
|
144 | break;
|
145 | }
|
146 | }
|
147 | compiler.watch(watchOptions, callback);
|
148 | } else {
|
149 | compiler.run(callback);
|
150 | }
|
151 |
|
152 |
|
153 | if (stdin) {
|
154 | process.stdin.on('end', () => {
|
155 | process.exit();
|
156 | });
|
157 |
|
158 | process.stdin.resume();
|
159 | }
|
160 | });
|
161 | }
|
162 | };
|
163 | };
|