Advertisement
Guest User

Untitled

a guest
May 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. const Watchpack = require("watchpack");
  2. class FooPlugin {
  3. constructor() {
  4. this.started = false;
  5. this.abort = false;
  6. this.aborted = false;
  7. this.files = [];
  8. this.dirs = [];
  9. }
  10.  
  11. apply(compiler) {
  12. compiler.hooks.compilation.tap("FooPlugin", this.onCompilation.bind(this));
  13. compiler.hooks.afterCompile.tapAsync("FooPlugin", this.onAfterCompile.bind(this));
  14. compiler.hooks.invalid.tap("FooPlugin", this.onInvalid.bind(this));
  15. compiler.hooks.afterEmit.tapAsync("FooPlugin", this.onAfterEmit.bind(this));
  16. compiler.hooks.done.tapAsync("FooPlugin", this.onDone.bind(this));
  17. compiler.hooks.failed.tap("FooPlugin", this.onFailed.bind(this));
  18. compiler.hooks.shouldEmit.tap("FooPlugin", this.onShouldEmit.bind(this));
  19. }
  20.  
  21. onCompilation(compilation) {
  22. if (this.abort && !this.aborted) {
  23. this.aborted = true;
  24. console.log(new Date(), "FooPlugin", "compilation", "should abort");
  25. // compilation.errors.push(this.newError());
  26. } else {
  27. console.log(new Date(), "FooPlugin", "compilation");
  28. }
  29. }
  30.  
  31. onAfterCompile(compilation, done) {
  32. if (this.abort && !this.aborted) {
  33. this.aborted = true;
  34. console.log(new Date(), "FooPlugin", "afterCompile", "should abort");
  35. // done(this.newError());
  36. } else {
  37. console.log(new Date(), "FooPlugin", "afterCompile");
  38. done();
  39. }
  40. }
  41.  
  42. onShouldEmit(compilation) {
  43. if (this.abort && this.aborted) {
  44. console.log(new Date(), "FooPlugin", "shouldEmit", "should abort");
  45. return false;
  46. } else {
  47. console.log(new Date(), "FooPlugin", "shouldEmit");
  48. return true;
  49. }
  50. }
  51.  
  52. onInvalid(compiler) {
  53. console.log(new Date(), "FooPlugin", "invalid");
  54. this.started = true;
  55. }
  56.  
  57. onAfterEmit(compilation, done) {
  58. this.files = Array.from(compilation.fileDependencies);
  59. this.dirs = Array.from(compilation.contextDependencies);
  60. done();
  61. }
  62.  
  63. onDone(stats, done) {
  64. console.log(new Date(), "FooPlugin", "done", `started=${this.started} abort=${this.abort} aborted=${this.aborted}`);
  65. this.started = false;
  66. this.abort = false;
  67. this.aborted = false;
  68. if (this.watchpack) {
  69. this.watchpack.close();
  70. }
  71. this.watchpack = this.newWatchpack();
  72. this.watchpack.watch([...this.files], [...this.dirs]);
  73. this.watchpack.on("change", this.onChange.bind(this));
  74. done();
  75. }
  76.  
  77. onFailed(error) {
  78. console.log(new Date(), "FooPlugin", "failed", error, `started=${this.started} abort=${this.abort} aborted=${this.aborted}`);
  79. this.started = false;
  80. this.abort = false;
  81. this.aborted = false;
  82. }
  83.  
  84. onChange() {
  85. console.log(new Date(), "FooPlugin", "change");
  86. if (this.started) {
  87. this.abort = true;
  88. }
  89. }
  90.  
  91. newWatchpack() {
  92. return new Watchpack({ aggregateTimeout: 100 });
  93. }
  94.  
  95. newError() {
  96. return new Error("ファイルが変更されました");
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement