Skip to content

Commit

Permalink
feat: add change hook support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 7, 2018
1 parent e279575 commit 8057e4c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ProgressPlugin } from 'webpack';
import env from 'std-env';
import prettyTime from 'pretty-time';

import { startCase } from './utils';
import { startCase, shortenPath } from './utils';

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

// Watch compilation has been invalidated.
hook(compiler, 'invalid', (fileName, changeTime) => {
this.callReporters('change', {
path: fileName,
shortPath: shortenPath(fileName),
time: changeTime,
});
});

// Compilation has completed
hook(compiler, 'done', (stats) => {
const time = prettyTime(process.hrtime(this.state.start), 2);
Expand Down
4 changes: 4 additions & 0 deletions src/reporters/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default class SimpleReporter {
consola.info(`Compiling ${context.state.name}`);
}

change(context, { shortPath }) {
consola.info(`${shortPath} changed.`, `Rebuilding ${context.state.name}`);
}

done(context) {
const { hasError, message, name } = context.state;
consola[hasError ? 'error' : 'success'](`${name}: ${message}`);
Expand Down
11 changes: 9 additions & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sep } from 'path';

export function first(arr) {
return arr[0];
}
Expand Down Expand Up @@ -27,10 +29,15 @@ export function removeBefore(delimiter, str) {
return last(str.split(delimiter)) || '';
}

export const range = (len) => {
export function range(len) {
const arr = [];
for (let i = 0; i < len; i++) {
arr.push(i);
}
return arr;
};
}

export function shortenPath(path = '') {
const cwd = process.cwd() + sep;
return path.replace(cwd, '');
}

0 comments on commit 8057e4c

Please sign in to comment.