@roots/bud-extensions
Version:
bud.js core module
80 lines (79 loc) • 2.28 kB
JavaScript
import { __decorate } from "tslib";
import { Extension } from '@roots/bud-framework/extension';
import { bind, label } from '@roots/bud-framework/extension/decorators';
import { BudError } from '@roots/bud-support/errors';
/**
* Webpack lifecycle plugin
*/
let BudWebpackLifecyclePlugin = class BudWebpackLifecyclePlugin extends Extension {
/**
* {@link Extension.apply}
*/
apply(compiler) {
const hooks = [
`afterCompile`,
`assetEmitted`,
`beforeCompile`,
`failed`,
`shouldEmit`,
]
.filter(k => k in compiler.hooks)
.filter(k => k in this)
.map(k => [compiler.hooks[k], this[k]]);
hooks.map(([hook, method]) => hook.tap(this.label, method));
}
/**
* Asset emitted hook
*/
assetEmitted(file, info) {
this.logger.log(`asset emitted:`, file, `=>`, this.app.relPath(info.targetPath));
}
/**
* Before compile hook
*/
beforeCompile(compilation) {
this.logger.log(`compilation started:`, compilation.hash);
}
/**
* After compile hook
*/
afterCompile(compilation) {
this.logger.log(`compilation completed:`, compilation.hash);
}
/**
* Failed hook
*/
failed(error) {
this.app.catch(BudError.normalize(error.message, {
details: `This error was thrown by the webpack compiler. It is likely a misconfiguration.`,
}));
}
/**
* Should emit hook
*/
shouldEmit() {
return this.app.context.dry !== true;
}
};
__decorate([
bind
], BudWebpackLifecyclePlugin.prototype, "apply", null);
__decorate([
bind
], BudWebpackLifecyclePlugin.prototype, "assetEmitted", null);
__decorate([
bind
], BudWebpackLifecyclePlugin.prototype, "beforeCompile", null);
__decorate([
bind
], BudWebpackLifecyclePlugin.prototype, "afterCompile", null);
__decorate([
bind
], BudWebpackLifecyclePlugin.prototype, "failed", null);
__decorate([
bind
], BudWebpackLifecyclePlugin.prototype, "shouldEmit", null);
BudWebpackLifecyclePlugin = __decorate([
label(`@roots/bud-extensions/webpack-lifecycle-plugin`)
], BudWebpackLifecyclePlugin);
export default BudWebpackLifecyclePlugin;