UNPKG

1.92 kBJavaScriptView Raw
1// @ts-check
2/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
3/** @typedef {import("webpack/lib/FileSystemInfo").Snapshot} Snapshot */
4'use strict';
5
6/**
7 *
8 * @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies
9 * @param {WebpackCompilation} mainCompilation
10 * @param {number} startTime
11 */
12function createSnapshot (fileDependencies, mainCompilation, startTime) {
13 return new Promise((resolve, reject) => {
14 mainCompilation.fileSystemInfo.createSnapshot(
15 startTime,
16 fileDependencies.fileDependencies,
17 fileDependencies.contextDependencies,
18 fileDependencies.missingDependencies,
19 null,
20 (err, snapshot) => {
21 if (err) {
22 return reject(err);
23 }
24 resolve(snapshot);
25 }
26 );
27 });
28}
29
30/**
31 * Returns true if the files inside this snapshot
32 * have not been changed
33 *
34 * @param {Snapshot} snapshot
35 * @param {WebpackCompilation} mainCompilation
36 * @returns {Promise<boolean>}
37 */
38function isSnapShotValid (snapshot, mainCompilation) {
39 return new Promise((resolve, reject) => {
40 mainCompilation.fileSystemInfo.checkSnapshotValid(
41 snapshot,
42 (err, isValid) => {
43 if (err) {
44 reject(err);
45 }
46 resolve(isValid);
47 }
48 );
49 });
50}
51
52/**
53 * Ensure that the files keep watched for changes
54 * and will trigger a recompile
55 *
56 * @param {WebpackCompilation} mainCompilation
57 * @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies
58 */
59function watchFiles (mainCompilation, fileDependencies) {
60 Object.keys(fileDependencies).forEach((depencyTypes) => {
61 fileDependencies[depencyTypes].forEach(fileDependency => {
62 mainCompilation[depencyTypes].add(fileDependency);
63 });
64 });
65}
66
67module.exports = {
68 createSnapshot,
69 isSnapShotValid,
70 watchFiles
71};