/
Compiler.js
167 lines (135 loc) · 5.8 KB
/
Compiler.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const { options: coloretteOptions } = require('colorette');
const { packageExists } = require('./package-exists');
const webpack = packageExists('webpack') ? require('webpack') : undefined;
const logger = require('./logger');
const { writeFileSync } = require('fs');
class Compiler {
constructor() {
this.compilerOptions = {};
}
async createCompiler(options) {
try {
this.compiler = await webpack(options);
this.compilerOptions = options;
} catch (error) {
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
const ValidationError = webpack.ValidationError ? webpack.ValidationError : webpack.WebpackOptionsValidationError;
// In case of schema errors print and exit process
// For webpack@4 and webpack@5
if (error instanceof ValidationError) {
logger.error(error.message);
} else {
logger.error(error);
}
process.exit(2);
}
}
get getCompiler() {
return this.compiler;
}
async webpackInstance(opts) {
const { outputOptions, options } = opts;
if (outputOptions.interactive) {
const interactive = require('./interactive');
return interactive(options, outputOptions);
}
const compilers = this.compiler.compilers ? this.compiler.compilers : [this.compiler];
const isWatchMode = Boolean(compilers.find((compiler) => compiler.options.watch));
const isRawOutput = typeof outputOptions.json === 'undefined';
if (isRawOutput) {
for (const compiler of compilers) {
if (outputOptions.progress) {
const { ProgressPlugin } = webpack;
let progressPluginExists;
if (compiler.options.plugins) {
progressPluginExists = Boolean(compiler.options.plugins.find((e) => e instanceof ProgressPlugin));
}
if (!progressPluginExists) {
new ProgressPlugin().apply(compiler);
}
}
}
this.compiler.hooks.watchRun.tap('watchInfo', (compilation) => {
if (compilation.options.bail && isWatchMode) {
logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
}
logger.success(`Compilation${compilation.name ? `${compilation.name}` : ''} starting...`);
});
this.compiler.hooks.done.tap('watchInfo', (compilation) => {
logger.success(`Compilation${compilation.name ? `${compilation.name}` : ''} finished`);
});
}
const callback = (error, stats) => {
if (error) {
logger.error(error);
process.exit(1);
}
if (stats.hasErrors()) {
process.exitCode = 1;
}
const getStatsOptions = (stats) => {
// TODO remove after drop webpack@4
if (webpack.Stats && webpack.Stats.presetToOptions) {
if (!stats) {
stats = {};
} else if (typeof stats === 'boolean' || typeof stats === 'string') {
stats = webpack.Stats.presetToOptions(stats);
}
}
stats.colors = typeof stats.colors !== 'undefined' ? stats.colors : coloretteOptions.enabled;
return stats;
};
const foundStats = this.compiler.compilers
? { children: this.compiler.compilers.map((compiler) => getStatsOptions(compiler.options.stats)) }
: getStatsOptions(this.compiler.options.stats);
if (outputOptions.json === true) {
process.stdout.write(JSON.stringify(stats.toJson(foundStats), null, 2) + '\n');
} else if (typeof outputOptions.json === 'string') {
const JSONStats = JSON.stringify(stats.toJson(foundStats), null, 2);
try {
writeFileSync(outputOptions.json, JSONStats);
logger.success(`stats are successfully stored as json to ${outputOptions.json}`);
} catch (error) {
logger.error(error);
process.exit(2);
}
} else {
logger.raw(`${stats.toString(foundStats)}`);
}
if (isWatchMode) {
logger.success('watching files for updates...');
}
};
if (isWatchMode) {
const watchOptions = (this.compiler.options && this.compiler.options.watchOptions) || {};
if (watchOptions.stdin) {
process.stdin.on('end', function () {
process.exit();
});
process.stdin.resume();
}
return new Promise((resolve) => {
this.compiler.watch(watchOptions, (error, stats) => {
callback(error, stats);
resolve();
});
});
} else {
return new Promise((resolve) => {
this.compiler.run((error, stats) => {
if (this.compiler.close) {
this.compiler.close(() => {
callback(error, stats);
resolve();
});
} else {
callback(error, stats);
resolve();
}
});
});
}
}
}
module.exports = { Compiler };