index.js 967 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. module.exports = class TimeFixPlugin {
  2. constructor(watchOffset = 11000) {
  3. this.watchOffset = watchOffset
  4. }
  5. apply(compiler) {
  6. const context = this
  7. const watch = compiler.watch
  8. let watching
  9. let fixed
  10. // Modify the time for first run
  11. compiler.watch = function () {
  12. watching = watch.apply(this, arguments)
  13. watching.startTime += context.watchOffset
  14. return watching
  15. }
  16. // Modify the time for subsequent runs
  17. compiler.hooks.watchRun.tap('time-fix-plugin', () => {
  18. if (watching && !fixed) {
  19. watching.startTime += this.watchOffset
  20. }
  21. })
  22. // Reset time
  23. compiler.hooks.done.tap('time-fix-plugin', stats => {
  24. if (watching && !fixed) {
  25. // webpack 5: #3
  26. if (stats.compilation.startTime) {
  27. stats.compilation.startTime -= this.watchOffset
  28. } else {
  29. stats.startTime -= this.watchOffset
  30. }
  31. fixed = true
  32. }
  33. })
  34. }
  35. }