Skip to content

Commit

Permalink
fix: ensure state exists on hooks (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 9, 2018
1 parent a894a31 commit 8f0085a
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,24 @@ export default class WebpackBarPlugin extends ProgressPlugin {
return globalStates[this.options.name];
}

_ensureState() {
// Keep our state in shared object
if (!this.states[this.options.name]) {
this.states[this.options.name] = {
...DEFAULT_STATE,
color: this.options.color,
name: startCase(this.options.name),
};
}
}

apply(compiler) {
super.apply(compiler);

// 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] = {
...DEFAULT_STATE,
color: this.options.color,
name: startCase(this.options.name),
};
}
});

// Hook into the compiler before a new compilation is created.
hook(compiler, 'compile', () => {
this._ensureState();

Object.assign(this.state, {
...DEFAULT_STATE,
start: process.hrtime(),
Expand All @@ -148,6 +149,8 @@ export default class WebpackBarPlugin extends ProgressPlugin {

// Watch compilation has been invalidated.
hook(compiler, 'invalid', (fileName, changeTime) => {
this._ensureState();

this.callReporters('change', {
path: fileName,
shortPath: shortenPath(fileName),
Expand All @@ -157,6 +160,8 @@ export default class WebpackBarPlugin extends ProgressPlugin {

// Compilation has completed
hook(compiler, 'done', (stats) => {
this._ensureState();

const time = prettyTime(process.hrtime(this.state.start), 2);
const hasErrors = stats.hasErrors();
const status = hasErrors ? 'with some errors' : 'succesfuly';
Expand Down

0 comments on commit 8f0085a

Please sign in to comment.