-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove webpack 4 file watcher api
- Loading branch information
Showing
4 changed files
with
69 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.