Skip to content

Commit 8f0085a

Browse files
committed
fix: ensure state exists on hooks (#30)
1 parent a894a31 commit 8f0085a

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/plugin.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,24 @@ export default class WebpackBarPlugin extends ProgressPlugin {
121121
return globalStates[this.options.name];
122122
}
123123

124+
_ensureState() {
125+
// Keep our state in shared object
126+
if (!this.states[this.options.name]) {
127+
this.states[this.options.name] = {
128+
...DEFAULT_STATE,
129+
color: this.options.color,
130+
name: startCase(this.options.name),
131+
};
132+
}
133+
}
134+
124135
apply(compiler) {
125136
super.apply(compiler);
126137

127-
// Initialize our state before actual build
128-
hook(compiler, 'afterPlugins', () => {
129-
// Keep our state in shared object
130-
if (!this.states[this.options.name]) {
131-
this.states[this.options.name] = {
132-
...DEFAULT_STATE,
133-
color: this.options.color,
134-
name: startCase(this.options.name),
135-
};
136-
}
137-
});
138-
139138
// Hook into the compiler before a new compilation is created.
140139
hook(compiler, 'compile', () => {
140+
this._ensureState();
141+
141142
Object.assign(this.state, {
142143
...DEFAULT_STATE,
143144
start: process.hrtime(),
@@ -148,6 +149,8 @@ export default class WebpackBarPlugin extends ProgressPlugin {
148149

149150
// Watch compilation has been invalidated.
150151
hook(compiler, 'invalid', (fileName, changeTime) => {
152+
this._ensureState();
153+
151154
this.callReporters('change', {
152155
path: fileName,
153156
shortPath: shortenPath(fileName),
@@ -157,6 +160,8 @@ export default class WebpackBarPlugin extends ProgressPlugin {
157160

158161
// Compilation has completed
159162
hook(compiler, 'done', (stats) => {
163+
this._ensureState();
164+
160165
const time = prettyTime(process.hrtime(this.state.start), 2);
161166
const hasErrors = stats.hasErrors();
162167
const status = hasErrors ? 'with some errors' : 'succesfuly';

0 commit comments

Comments
 (0)