Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Watchpack = require("watchpack");
- class FooPlugin {
- constructor() {
- this.started = false;
- this.abort = false;
- this.aborted = false;
- this.files = [];
- this.dirs = [];
- }
- apply(compiler) {
- compiler.hooks.compilation.tap("FooPlugin", this.onCompilation.bind(this));
- compiler.hooks.afterCompile.tapAsync("FooPlugin", this.onAfterCompile.bind(this));
- compiler.hooks.invalid.tap("FooPlugin", this.onInvalid.bind(this));
- compiler.hooks.afterEmit.tapAsync("FooPlugin", this.onAfterEmit.bind(this));
- compiler.hooks.done.tapAsync("FooPlugin", this.onDone.bind(this));
- compiler.hooks.failed.tap("FooPlugin", this.onFailed.bind(this));
- compiler.hooks.shouldEmit.tap("FooPlugin", this.onShouldEmit.bind(this));
- }
- onCompilation(compilation) {
- if (this.abort && !this.aborted) {
- this.aborted = true;
- console.log(new Date(), "FooPlugin", "compilation", "should abort");
- // compilation.errors.push(this.newError());
- } else {
- console.log(new Date(), "FooPlugin", "compilation");
- }
- }
- onAfterCompile(compilation, done) {
- if (this.abort && !this.aborted) {
- this.aborted = true;
- console.log(new Date(), "FooPlugin", "afterCompile", "should abort");
- // done(this.newError());
- } else {
- console.log(new Date(), "FooPlugin", "afterCompile");
- done();
- }
- }
- onShouldEmit(compilation) {
- if (this.abort && this.aborted) {
- console.log(new Date(), "FooPlugin", "shouldEmit", "should abort");
- return false;
- } else {
- console.log(new Date(), "FooPlugin", "shouldEmit");
- return true;
- }
- }
- onInvalid(compiler) {
- console.log(new Date(), "FooPlugin", "invalid");
- this.started = true;
- }
- onAfterEmit(compilation, done) {
- this.files = Array.from(compilation.fileDependencies);
- this.dirs = Array.from(compilation.contextDependencies);
- done();
- }
- onDone(stats, done) {
- console.log(new Date(), "FooPlugin", "done", `started=${this.started} abort=${this.abort} aborted=${this.aborted}`);
- this.started = false;
- this.abort = false;
- this.aborted = false;
- if (this.watchpack) {
- this.watchpack.close();
- }
- this.watchpack = this.newWatchpack();
- this.watchpack.watch([...this.files], [...this.dirs]);
- this.watchpack.on("change", this.onChange.bind(this));
- done();
- }
- onFailed(error) {
- console.log(new Date(), "FooPlugin", "failed", error, `started=${this.started} abort=${this.abort} aborted=${this.aborted}`);
- this.started = false;
- this.abort = false;
- this.aborted = false;
- }
- onChange() {
- console.log(new Date(), "FooPlugin", "change");
- if (this.started) {
- this.abort = true;
- }
- }
- newWatchpack() {
- return new Watchpack({ aggregateTimeout: 100 });
- }
- newError() {
- return new Error("ファイルが変更されました");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement