file-watcher-api.js 1.9 KB

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