Skip to content

Commit 1aef676

Browse files
committed
refactor: remove webpack 4 file watcher api
1 parent 8c28aaa commit 1aef676

File tree

4 files changed

+69
-146
lines changed

4 files changed

+69
-146
lines changed

lib/cached-child-compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
2727
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
2828
/** @typedef {{hash: string, entry: any, content: string }} ChildCompilationResultEntry */
29-
/** @typedef {import("./webpack4/file-watcher-api").Snapshot} Snapshot */
29+
/** @typedef {import("./file-watcher-api").Snapshot} Snapshot */
3030
/** @typedef {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} FileDependencies */
3131
/** @typedef {{
3232
dependencies: FileDependencies,

lib/file-watcher-api.js

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,71 @@
11
// @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+
*/
12+
function 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+
230
/**
3-
* To use the available webpack core api
4-
* we have to use different child compilers
5-
* depending on the used webpack version
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>}
637
*/
7-
const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
8-
9-
// Typescript hack to test only the webpack 4 code
10-
/** @type {import('./webpack4/file-watcher-api')} */
11-
module.exports = webpackMajorVersion === 4
12-
? require('./webpack4/file-watcher-api.js')
13-
// Hack to ignore './webpack5/file-watcher-api.js' from typescript:
14-
: require('./webpack' + 5 + '/file-watcher-api.js');
38+
function 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+
*/
59+
function watchFiles (mainCompilation, fileDependencies) {
60+
Object.keys(fileDependencies).forEach((depencyTypes) => {
61+
fileDependencies[depencyTypes].forEach(fileDependency => {
62+
mainCompilation[depencyTypes].add(fileDependency);
63+
});
64+
});
65+
}
66+
67+
module.exports = {
68+
createSnapshot,
69+
isSnapShotValid,
70+
watchFiles
71+
};

lib/webpack4/file-watcher-api.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

lib/webpack5/file-watcher-api.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)