/
Compiler.js
148 lines (132 loc) · 5.36 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
const webpack = require('webpack');
const logger = require('./logger');
const { CompilerOutput } = require('./CompilerOutput');
class Compiler {
constructor() {
this.output = new CompilerOutput();
this.compilerOptions = {};
}
setUpHookForCompilation(compilation, outputOptions, options) {
const { ProgressPlugin } = webpack;
let progressPluginExists;
if (options.plugins) {
progressPluginExists = options.plugins.find((e) => e instanceof ProgressPlugin);
}
compilation.hooks.beforeRun.tap('webpackProgress', () => {
if (outputOptions.progress) {
if (!progressPluginExists) {
new ProgressPlugin().apply(compilation);
} else {
if (!progressPluginExists.handler) {
options.plugins = options.plugins.filter((e) => e !== progressPluginExists);
Object.keys(progressPluginExists).map((opt) => {
ProgressPlugin.defaultOptions[opt] = progressPluginExists[opt];
});
new ProgressPlugin().apply(compilation);
} else {
progressPluginExists.apply(compilation);
}
}
}
});
}
generateOutput(outputOptions, stats) {
this.output.generateRawOutput(stats, this.compilerOptions);
process.stdout.write('\n');
if (outputOptions.watch) {
logger.info('watching files for updates...');
}
}
compilerCallback(err, stats, lastHash, options, outputOptions) {
const statsErrors = [];
if (!outputOptions.watch || err) {
// Do not keep cache anymore
this.compiler.purgeInputFileSystem();
}
if (err) {
lastHash = null;
logger.error(err.stack || err);
process.exit(1); // eslint-disable-line
}
if (!outputOptions.watch && (stats.hasErrors() || stats.hasWarnings())) {
process.exitCode = 1;
}
if (outputOptions.json) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + '\n');
} else if (stats.hash !== lastHash) {
lastHash = stats.hash;
if (stats.compilation && stats.compilation.errors.length !== 0) {
const errors = stats.compilation.errors;
errors.forEach((statErr) => {
const errLoc = statErr.module ? statErr.module.resource : null;
statsErrors.push({ name: statErr.message, loc: errLoc });
});
}
return this.generateOutput(outputOptions, stats, statsErrors);
}
}
async invokeCompilerInstance(lastHash, options, outputOptions) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
await this.compiler.run((err, stats) => {
const content = this.compilerCallback(err, stats, lastHash, options, outputOptions);
resolve(content);
});
});
}
async invokeWatchInstance(lastHash, options, outputOptions, watchOptions) {
return this.compiler.watch(watchOptions, (err, stats) => {
return this.compilerCallback(err, stats, lastHash, options, outputOptions);
});
}
async createCompiler(options) {
try {
this.compiler = await webpack(options);
this.compilerOptions = options;
} catch (err) {
process.stdout.write('\n');
logger.error(`${err.name}: ${err.message}`);
process.stdout.write('\n');
return;
}
}
get getCompiler() {
return this.compiler;
}
async webpackInstance(opts) {
const { outputOptions, options } = opts;
const lastHash = null;
const { ProgressPlugin } = webpack;
if (options.plugins) {
options.plugins = options.plugins.filter((e) => e instanceof ProgressPlugin);
}
if (outputOptions.interactive) {
const interactive = require('./interactive');
return interactive(options, outputOptions);
}
//warn the user if bail and watch both are used together
if (!this.compiler.compilers && this.compiler.options.bail && this.compiler.watch) {
logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
}
if (this.compiler.compilers) {
this.compiler.compilers.forEach((comp, idx) => {
this.setUpHookForCompilation(comp, outputOptions, options[idx]);
});
} else {
this.setUpHookForCompilation(this.compiler, outputOptions, options);
}
if (outputOptions.watch) {
const watchOptions = outputOptions.watchOptions || {};
if (watchOptions.stdin) {
process.stdin.on('end', function () {
process.exit();
});
process.stdin.resume();
}
await this.invokeWatchInstance(lastHash, options, outputOptions, watchOptions);
} else {
return await this.invokeCompilerInstance(lastHash, options, outputOptions);
}
}
}
module.exports = { Compiler };