Skip to content

Commit 8057e4c

Browse files
committed
feat: add change hook support
1 parent e279575 commit 8057e4c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/plugin.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ProgressPlugin } from 'webpack';
22
import env from 'std-env';
33
import prettyTime from 'pretty-time';
44

5-
import { startCase } from './utils';
5+
import { startCase, shortenPath } from './utils';
66

77
import * as reporters from './reporters'; // eslint-disable-line import/no-namespace
88
import { parseRequest, hook } from './utils/webpack';
@@ -149,6 +149,15 @@ export default class WebpackBarPlugin extends ProgressPlugin {
149149
this.callReporters('start');
150150
});
151151

152+
// Watch compilation has been invalidated.
153+
hook(compiler, 'invalid', (fileName, changeTime) => {
154+
this.callReporters('change', {
155+
path: fileName,
156+
shortPath: shortenPath(fileName),
157+
time: changeTime,
158+
});
159+
});
160+
152161
// Compilation has completed
153162
hook(compiler, 'done', (stats) => {
154163
const time = prettyTime(process.hrtime(this.state.start), 2);

src/reporters/basic.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export default class SimpleReporter {
55
consola.info(`Compiling ${context.state.name}`);
66
}
77

8+
change(context, { shortPath }) {
9+
consola.info(`${shortPath} changed.`, `Rebuilding ${context.state.name}`);
10+
}
11+
812
done(context) {
913
const { hasError, message, name } = context.state;
1014
consola[hasError ? 'error' : 'success'](`${name}: ${message}`);

src/utils/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { sep } from 'path';
2+
13
export function first(arr) {
24
return arr[0];
35
}
@@ -27,10 +29,15 @@ export function removeBefore(delimiter, str) {
2729
return last(str.split(delimiter)) || '';
2830
}
2931

30-
export const range = (len) => {
32+
export function range(len) {
3133
const arr = [];
3234
for (let i = 0; i < len; i++) {
3335
arr.push(i);
3436
}
3537
return arr;
36-
};
38+
}
39+
40+
export function shortenPath(path = '') {
41+
const cwd = process.cwd() + sep;
42+
return path.replace(cwd, '');
43+
}

0 commit comments

Comments
 (0)