debian-mirror-gitlab/config/helpers/incremental_webpack_compiler/compiler.js

120 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-11-11 11:23:49 +05:30
/* eslint-disable max-classes-per-file */
const path = require('path');
const { History, HistoryWithTTL } = require('./history');
const log = require('./log');
2022-04-04 11:22:00 +05:30
const onRequestEntryPoint = (callback) => {
return (req, res, next) => {
2021-11-11 11:23:49 +05:30
const fileName = path.basename(req.url);
/**
* We are only interested in files that have a name like `pages.foo.bar.chunk.js`
* because those are the ones corresponding to our entry points.
*
* This filters out hot update files that are for example named "pages.foo.bar.[hash].hot-update.js"
*/
if (fileName.startsWith('pages.') && fileName.endsWith('.chunk.js')) {
const entryPoint = fileName.replace(/\.chunk\.js$/, '');
callback(entryPoint);
}
next();
2022-04-04 11:22:00 +05:30
};
2021-11-11 11:23:49 +05:30
};
/**
* The NoopCompiler does nothing, following the null object pattern.
*/
class NoopCompiler {
constructor() {
this.enabled = false;
}
// eslint-disable-next-line class-methods-use-this
filterEntryPoints(entryPoints) {
return entryPoints;
}
// eslint-disable-next-line class-methods-use-this
logStatus() {}
// eslint-disable-next-line class-methods-use-this
2022-04-04 11:22:00 +05:30
createMiddleware() {
return null;
}
2021-11-11 11:23:49 +05:30
}
/**
* The HistoryOnlyCompiler only records which entry points have been requested.
* This is so that if the user disables incremental compilation, history is
* still recorded. If they later enable incremental compilation, that history
* can be used.
*/
class HistoryOnlyCompiler extends NoopCompiler {
constructor(historyFilePath) {
super();
this.history = new History(historyFilePath);
}
2022-04-04 11:22:00 +05:30
createMiddleware() {
return onRequestEntryPoint((entryPoint) => {
2021-11-11 11:23:49 +05:30
this.history.onRequestEntryPoint(entryPoint);
});
}
}
// If we force a recompile immediately, the page reload doesn't seem to work.
// Five seconds seem to work fine and the user can read the message
const TIMEOUT = 5000;
/**
* The IncrementalWebpackCompiler tracks which entry points have been
* requested, and only compiles entry points visited within the last `ttl`
* days.
*/
class IncrementalWebpackCompiler {
constructor(historyFilePath, ttl) {
this.enabled = true;
this.history = new HistoryWithTTL(historyFilePath, ttl);
}
filterEntryPoints(entrypoints) {
return Object.fromEntries(
Object.entries(entrypoints).map(([entryPoint, paths]) => {
if (this.history.isRecentlyVisited(entryPoint)) {
return [entryPoint, paths];
}
return [entryPoint, ['./webpack_non_compiled_placeholder.js']];
}),
);
}
logStatus(totalCount) {
log(`Currently compiling route entrypoints: ${this.history.size} of ${totalCount}`);
}
2022-04-04 11:22:00 +05:30
createMiddleware(devServer) {
return onRequestEntryPoint((entryPoint) => {
2021-11-11 11:23:49 +05:30
const wasVisitedRecently = this.history.onRequestEntryPoint(entryPoint);
if (!wasVisitedRecently) {
log(`Have not visited ${entryPoint} recently. Adding to compilation.`);
setTimeout(() => {
2022-04-04 11:22:00 +05:30
devServer.invalidate(() => {
2022-05-07 20:08:51 +05:30
if (Array.isArray(devServer.webSocketServer && devServer.webSocketServer.clients)) {
2022-04-04 11:22:00 +05:30
devServer.sendMessage(devServer.webSocketServer.clients, 'static-changed');
2021-11-11 11:23:49 +05:30
}
});
}, TIMEOUT);
}
});
}
}
module.exports = {
NoopCompiler,
HistoryOnlyCompiler,
IncrementalWebpackCompiler,
};