file-watcher-api.js 1.9 KB

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