diff --git a/src/plugin.js b/src/plugin.js index 7d31701..1c72a04 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -5,7 +5,7 @@ import prettyTime from 'pretty-time'; import { startCase } from './utils'; import * as reporters from './reporters'; // eslint-disable-line import/no-namespace -import { parseRequest } from './utils/request'; +import { parseRequest, hook } from './utils/webpack'; // Use bars when possible as default const isMinimal = env.ci || env.test || !env.tty; @@ -127,16 +127,8 @@ export default class WebpackBarPlugin extends ProgressPlugin { apply(compiler) { super.apply(compiler); - // Hook helper for webpack 3 + 4 support - function hook(hookName, fn) { - if (compiler.hooks) { - compiler.hooks[hookName].tap('WebpackBar:' + hookName, fn); - } else { - compiler.plugin(hookName, fn); - } - } - - hook('afterPlugins', () => { + // Initialize our state before actual build + hook(compiler, 'afterPlugins', () => { // Keep our state in shared object if (!this.states[this.options.name]) { this.states[this.options.name] = { @@ -148,7 +140,7 @@ export default class WebpackBarPlugin extends ProgressPlugin { }); // Hook into the compiler before a new compilation is created. - hook('compile', () => { + hook(compiler, 'compile', () => { Object.assign(this.state, { ...DEFAULT_STATE, start: process.hrtime(), @@ -158,7 +150,7 @@ export default class WebpackBarPlugin extends ProgressPlugin { }); // Compilation has completed - hook('done', (stats) => { + hook(compiler, 'done', (stats) => { const time = prettyTime(process.hrtime(this.state.start), 2); const hasErrors = stats.hasErrors(); const status = hasErrors ? 'with some errors' : 'succesfuly'; diff --git a/src/reporters/fancy.js b/src/reporters/fancy.js index a9131ab..0192f3d 100644 --- a/src/reporters/fancy.js +++ b/src/reporters/fancy.js @@ -3,7 +3,7 @@ import chalk from 'chalk'; import consola from 'consola'; import { renderBar, colorize, ellipsisLeft } from '../utils/cli'; -import { formatRequest } from '../utils/request'; +import { formatRequest } from '../utils/webpack'; import { BULLET, TICK, CROSS, CIRCLE_OPEN } from '../utils/consts'; import LogUpdate from '../utils/log-update'; diff --git a/src/utils/request.js b/src/utils/webpack.js similarity index 76% rename from src/utils/request.js rename to src/utils/webpack.js index 61ae216..12c0165 100644 --- a/src/utils/request.js +++ b/src/utils/webpack.js @@ -31,3 +31,12 @@ export const formatRequest = (request) => { return `${loaders}${NEXT}${request.file}`; }; + +// Hook helper for webpack 3 + 4 support +export function hook(compiler, hookName, fn) { + if (compiler.hooks) { + compiler.hooks[hookName].tap('WebpackBar:' + hookName, fn); + } else { + compiler.plugin(hookName, fn); + } +}