ko
Version:
build & lint library
43 lines (42 loc) • 1.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const tapable_1 = require("tapable");
const types_1 = require("../types");
const webpack_1 = __importDefault(require("webpack"));
class Hooks {
constructor() {
this.hookKeySet = types_1.HOOK_KEY_SET;
this.hooks = {};
}
register({ key, action, opts }) {
var _a;
(_a = this.hooks)[key] || (_a[key] = {
[types_1.ACTION.ADD]: [],
[types_1.ACTION.UPDATE]: [],
});
this.hooks[key][action].push(opts);
}
apply(opts) {
const hooks = this.hooks[opts.key];
if (!hooks)
return Promise.resolve(opts.context);
const tapInstance = new tapable_1.AsyncSeriesWaterfallHook(['ctx']);
hooks[types_1.ACTION.ADD].forEach(hook => {
tapInstance.tapPromise({ name: hook.name, stage: hook.stage, before: hook.before }, async (ctx) => {
const result = await hook.fn(ctx, webpack_1.default);
return ctx.concat(result);
});
});
hooks[types_1.ACTION.UPDATE].forEach(hook => {
tapInstance.tapPromise({ name: hook.name, stage: hook.stage, before: hook.before }, async (ctx) => {
const result = await hook.fn(ctx, webpack_1.default);
return result;
});
});
return tapInstance.promise(opts.context);
}
}
exports.default = Hooks;