Welcome to mirror list, hosted at ThFree Co, Russian Federation.

compiler.js « incremental_webpack_compiler « helpers « config - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 480d7fa3263fa511cf40493b61d572e6f04bb11a (plain)
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
/* eslint-disable max-classes-per-file */

const path = require('path');
const { History, HistoryWithTTL } = require('./history');
const log = require('./log');

const onRequestEntryPoint = (app, callback) => {
  app.use((req, res, next) => {
    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();
  });
};

/**
 * 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
  setupMiddleware() {}
}

/**
 * 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);
  }

  setupMiddleware(app) {
    onRequestEntryPoint(app, (entryPoint) => {
      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}`);
  }

  setupMiddleware(app, server) {
    onRequestEntryPoint(app, (entryPoint) => {
      const wasVisitedRecently = this.history.onRequestEntryPoint(entryPoint);
      if (!wasVisitedRecently) {
        log(`Have not visited ${entryPoint} recently. Adding to compilation.`);

        setTimeout(() => {
          server.middleware.invalidate(() => {
            if (server.sockets) {
              server.sockWrite(server.sockets, 'content-changed');
            }
          });
        }, TIMEOUT);
      }
    });
  }
}

module.exports = {
  NoopCompiler,
  HistoryOnlyCompiler,
  IncrementalWebpackCompiler,
};