Skip to content

Commit

Permalink
refactor: remove webpack 4 file watcher api
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jan 20, 2021
1 parent 8c28aaa commit 1aef676
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 146 deletions.
2 changes: 1 addition & 1 deletion lib/cached-child-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
/** @typedef {{hash: string, entry: any, content: string }} ChildCompilationResultEntry */
/** @typedef {import("./webpack4/file-watcher-api").Snapshot} Snapshot */
/** @typedef {import("./file-watcher-api").Snapshot} Snapshot */
/** @typedef {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} FileDependencies */
/** @typedef {{
dependencies: FileDependencies,
Expand Down
79 changes: 68 additions & 11 deletions lib/file-watcher-api.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,71 @@
// @ts-check
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
/** @typedef {import("webpack/lib/FileSystemInfo").Snapshot} Snapshot */
'use strict';

/**
*
* @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies
* @param {WebpackCompilation} mainCompilation
* @param {number} startTime
*/
function createSnapshot (fileDependencies, mainCompilation, startTime) {
return new Promise((resolve, reject) => {
mainCompilation.fileSystemInfo.createSnapshot(
startTime,
fileDependencies.fileDependencies,
fileDependencies.contextDependencies,
fileDependencies.missingDependencies,
null,
(err, snapshot) => {
if (err) {
return reject(err);
}
resolve(snapshot);
}
);
});
}

/**
* To use the available webpack core api
* we have to use different child compilers
* depending on the used webpack version
* Returns true if the files inside this snapshot
* have not been changed
*
* @param {Snapshot} snapshot
* @param {WebpackCompilation} mainCompilation
* @returns {Promise<boolean>}
*/
const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);

// Typescript hack to test only the webpack 4 code
/** @type {import('./webpack4/file-watcher-api')} */
module.exports = webpackMajorVersion === 4
? require('./webpack4/file-watcher-api.js')
// Hack to ignore './webpack5/file-watcher-api.js' from typescript:
: require('./webpack' + 5 + '/file-watcher-api.js');
function isSnapShotValid (snapshot, mainCompilation) {
return new Promise((resolve, reject) => {
mainCompilation.fileSystemInfo.checkSnapshotValid(
snapshot,
(err, isValid) => {
if (err) {
reject(err);
}
resolve(isValid);
}
);
});
}

/**
* Ensure that the files keep watched for changes
* and will trigger a recompile
*
* @param {WebpackCompilation} mainCompilation
* @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies
*/
function watchFiles (mainCompilation, fileDependencies) {
Object.keys(fileDependencies).forEach((depencyTypes) => {
fileDependencies[depencyTypes].forEach(fileDependency => {
mainCompilation[depencyTypes].add(fileDependency);
});
});
}

module.exports = {
createSnapshot,
isSnapShotValid,
watchFiles
};
64 changes: 0 additions & 64 deletions lib/webpack4/file-watcher-api.js

This file was deleted.

70 changes: 0 additions & 70 deletions lib/webpack5/file-watcher-api.js

This file was deleted.

0 comments on commit 1aef676

Please sign in to comment.