This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MultiCompiler.js
387 lines (306 loc) · 9.97 KB
/
MultiCompiler.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
const {DepGraph} = require('dependency-graph')
const MultiCompiler = require('webpack/lib/MultiCompiler')
const Stats = require('webpack/lib/Stats')
const compatPlugin = require('./util/compatPlugin')
const getDebugger = require('./util/debug')
const isCompilerV4 = require('./util/isCompilerV4')
const STAGE = {
'environment': 1,
'after-environment': 2,
'afterEnvironment': 2,
'entry-option': 3,
'entryOption': 3,
'after-plugins': 4,
'afterPlugins': 4,
'after-resolvers': 5,
'afterResolvers': 5,
'before-run': 6,
'beforeRun': 6,
'run': 7,
'watch-run': 8,
'watchRun': 8,
'normal-module-factory': 9,
'normalModuleFactory': 9,
'context-module-factory': 10,
'contextModuleFactory': 10,
'before-compile': 11,
'beforeCompile': 11,
'compile': 12,
'this-compilation': 13,
'thisCompilation': 13,
'compilation': 14,
'make': 15,
'after-compile': 16,
'afterCompile': 16,
'should-emit': 17,
'shouldEmit': 17,
'need-additional-pass': 18,
'needAdditionalPass': 18,
'emit': 19,
'after-emit': 20,
'afterEmit': 20,
'done': 21,
'failed': 22,
'invalid': 0,
'watch-close': -1,
'watchClose': -1
}
const STAGES_ASYNC = [
'before-run',
'run',
'watch-run',
'before-compile',
'make',
'after-compile',
'emit',
'after-emit'
]
class MultiCompiler2 extends MultiCompiler {
constructor (compilers) {
if (!compilers || (!Array.isArray(compilers) && typeof compilers !== 'object')) {
throw new Error('MultiCompiler2 accepts array of compilers only')
}
if (!Array.isArray(compilers)) {
compilers = Object.keys(compilers).map((key) => {
compilers[key].name = key // side effect
return compilers[key]
})
}
if (!compilers.length) {
throw new Error('No compilers was given (array of compilers is empty)')
}
const namesDistinct = compilers.filter((c, index, arr) => {
if (!c.name) {
throw new Error(`Each compiler must have a name defined: compiler #${index} has none`)
}
return arr.find(({name}) => name === c.name) === c
})
if (namesDistinct.length !== compilers.length) {
throw new Error(`Each config must have an unique name: ${compilers.map((c) => c.name).join(', ')}`)
}
const depGraph = getDepGraph(compilers)
const overallOrder = depGraph.overallOrder()
compilers = overallOrder.map((compilerName) => {
const c = depGraph.getNodeData(compilerName)
const dbug = getCompilerDebugger(c)
const run = c.run.bind(c)
const watch = c.watch.bind(c)
const callbacks = []
const watchings = []
const {
dependants,
dependencies
} = getNodeRelations(depGraph, c.name)
c._udk = { // side effect
callbacks,
dependants,
dependencies,
lastChange: {},
lastCompilation: undefined,
lastError: undefined,
lastStats: undefined,
running: false,
watchings
}
if (isCompilerV4(c)) {
Object.keys(c.hooks).forEach((stageName) => {
c.hooks[stageName].tap('DebugStagePlugin', () => {
dbug('%o %s', STAGE[stageName], stageName)
})
})
} else {
pluginManyStages(c, Object.keys(STAGE).slice(2), (stage, stageParam, cb) => { // out of scope
dbug(stage.level, stage.name)
stage.async && cb()
}, {prepend: true})
}
c.run = (callback) => {
const cancelledStats = getCancelledStats(c)
if (cancelledStats) {
dbug('cancel compilation %o', c.name)
c._udk.lastStats = cancelledStats // side effect
callback(null, cancelledStats)
return
}
run(callback)
}
c.watch = (watchOptions, handler) => {
const watching = watch(watchOptions, handler)
watchings.indexOf(watching) === -1 && watchings.push(watching)
return watching
}
compatPlugin(
c,
'watchRun',
'UDKWatchRunPlugin',
'tapPromise',
(compilerV4OrWatchingv3, doneV3) => {
c._udk.running = true // side effect
return new Promise((done) => {
holdOnDependencies(c, () => process.nextTick(() => {
const cancelledStats = getCancelledStats(c)
if (cancelledStats) {
dbug('cancel compilation %o', c.name)
return process.nextTick(() => {
watchings.forEach((watching) => {
!watching.closed && watching._done(null, cancelledStats.compilation)
})
})
}
done()
doneV3 && doneV3()
}))
})
}
)
compatPlugin(c, 'thisCompilation', 'UDKThisCompilationPlugin', (compilation) => {
c._udk.lastCompilation = compilation // side effect
})
compatPlugin(c, 'done', 'UDKDonePlugin', (stats) => {
c._udk.lastError = null // side effect
c._udk.lastStats = stats // side effect
c._udk.running = false // side effect
for (; callbacks.length; ((cb) => cb(null, stats))(callbacks.shift())); // side effect
invalidateDependants(c)
})
compatPlugin(c, 'failed', 'UDKFailedPlugin', (err) => {
c._udk.lastError = err // side effect
c._udk.lastStats = null // side effect
c._udk.running = false // side effect
for (; callbacks.length; ((cb) => cb(err))(callbacks.shift())); // side effect
invalidateDependants(c)
})
compatPlugin(c, 'invalid', 'UDKInvalidPlugin', (fileName, changeTime) => {
c._udk.lastChange = {fileName, changeTime} // side effect
})
return c
})
super(compilers)
this._udk = {
depGraph,
overallOrder
}
}
getCompiler (name) {
return this._udk.depGraph.getNodeData(name)
}
}
function getCancelledCompilation (compiler) {
const {
dependencies,
lastCompilation
} = compiler._udk
const compilesBadly = dependencies.filter((d) => {
return d._udk.lastStats ? d._udk.lastStats.hasErrors() : d._udk.lastError
})
if (compilesBadly.length) {
const previousCompilers = compilesBadly.map((c) => c.name).join(', ')
const {
fileDependencies = [],
contextDependencies = [],
missingDependencies = []
} = lastCompilation || {}
const compilation = compiler.createCompilation()
compilation.name = compiler.name
compilation.errors.push(new Error(`[udk] compilation cancelled due to errors in previous compilers: ${previousCompilers}`))
compilation.fileDependencies = fileDependencies
compilation.contextDependencies = contextDependencies
compilation.missingDependencies = missingDependencies
compilation.createHash()
return compilation
}
}
function getCancelledStats (compiler, options = {}) {
const cancelledCompilation = getCancelledCompilation(compiler)
if (cancelledCompilation) {
const cancelledStats = new Stats(cancelledCompilation)
cancelledStats.startTime = options.startTime || Date.now()
cancelledStats.endTime = options.endTime || cancelledStats.startTime
return cancelledStats
}
}
function getCompilerDebugger (compiler) {
if (typeof compiler === 'string') {
compiler = {name: compiler}
}
return getDebugger('udk', compiler.name, 'cpl')
}
function getDepGraph (nodes) {
const graph = new DepGraph()
nodes.forEach((node) => graph.addNode(node.name, node))
nodes.forEach((node) => {
node.dependencies && node.dependencies.forEach((dep) => {
graph.addDependency(node.name, dep)
})
})
return graph
}
function getNodeRelations (depGraph, nodeName) {
const overallOrder = depGraph.overallOrder()
const mapping = (name) => depGraph.getNodeData(name)
const sorting = (a, b) => overallOrder.indexOf(a) > overallOrder.indexOf(b)
return {
dependants: depGraph.dependantsOf(nodeName).sort(sorting).map(mapping),
dependencies: depGraph.dependenciesOf(nodeName).sort(sorting).map(mapping)
}
}
function holdOnDependencies (compiler, callback) {
const dbug = getCompilerDebugger(compiler)
const {dependencies} = compiler._udk
const depsRunning = compiler._udk.dependencies.filter((d) => d._udk.running)
depsRunning.forEach((d, index) => {
dbug('wait for compiler %o', d.name)
d._udk.callbacks.push(() => { // side effect
dbug('waiting ended for compiler %o', d.name)
depsRunning.splice(index, 1)
!depsRunning.length && callback()
})
})
!depsRunning.length && callback()
}
function invalidateDependants (compiler) {
const dbug = getCompilerDebugger(compiler)
const {
dependants,
lastChange
} = compiler._udk
const depsNotRunning = dependants.filter((d) => !d._udk.running)
depsNotRunning.forEach((d) => {
if (d._udk.watchings.length) {
dbug('invalid compiler %o due to change %o', d.name, lastChange)
if (isCompilerV4(compiler)) {
d.hooks.invalid.call(lastChange.fileName, lastChange.changeTime)
} else {
d.applyPlugins('invalid', lastChange.fileName, lastChange.changeTime)
}
d._udk.watchings.forEach((watching) => !watching.closed && watching.invalidate())
}
})
}
function pluginManyStages (compiler, stages, fn, options) {
if (typeof stages === 'string') {
stages = [stages]
}
stages.forEach((stageName) => {
const stage = {
async: STAGES_ASYNC.indexOf(stageName) > -1, // out of scope
level: STAGE[stageName], // out of scope
name: stageName
}
pluginStage(compiler, stageName, (stageParams, cb) => {
fn(stage, stageParams, cb)
}, options)
})
}
function pluginStage (compiler, stageName, fn, options = {}) {
if (!STAGE.hasOwnProperty(stageName)) { // out of scope
throw new Error(`Stage "${stageName}" is unknown`)
}
if (!compiler._plugins[stageName]) {
compiler._plugins[stageName] = []
}
const stagePlugins = compiler._plugins[stageName]
const plug = stagePlugins[options.prepend ? 'unshift' : 'push'].bind(stagePlugins)
plug(fn) // side effect
}
module.exports = MultiCompiler2