Skip to content

Commit 9836bed

Browse files
tooxiealvaro-kianava
authored andcommitted
Introduce custom hooks
They are useful for other plugins to tap into certain points of the build process. In this case a `devServerRunning` hook is introduced, which would allow a 3rd party plugin to, for example, display a QR code with the address the app is running on once the dev server started.
1 parent d6bde23 commit 9836bed

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const SyncHook = require('tapable').SyncHook;
2+
3+
module.exports = {
4+
devServerRunning: new SyncHook(),
5+
};

packages/cli/lib/lib/webpack/run-webpack.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const clientConfig = require('./webpack-client-config');
1010
const serverConfig = require('./webpack-server-config');
1111
const transformConfig = require('./transform-config');
1212
const { error, isDir, warn } = require('../../util');
13+
const { devServerRunning } = require('./hooks');
1314

1415
async function devBuild(env) {
1516
let userPort = parseInt(process.env.PORT || env.port, 10) || 8080;
@@ -68,6 +69,7 @@ async function devBuild(env) {
6869
}
6970

7071
showStats(stats, false);
72+
devServerRunning.call();
7173
});
7274

7375
compiler.hooks.failed.tap('CliDevPlugin', rej);

packages/cli/tests/hooks.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { create, watch } = require('./lib/cli');
2+
const { hooks } = require('./lib/utils');
3+
4+
describe('preact', () => {
5+
let intervalId;
6+
7+
afterEach(() => {
8+
clearInterval(intervalId);
9+
intervalId = null;
10+
});
11+
12+
it('should emit a devServerRunning event after the server starts', (done) => {
13+
let hookCalled;
14+
hooks.devServerRunning.tap('TestPlugin', () => {
15+
hookCalled = true;
16+
});
17+
18+
create('default').then((app) => {
19+
watch(app, 8083).then((server) => {
20+
// We need to wait not only for the server to start but also for the
21+
// stats to be printed to stdout.
22+
intervalId = setInterval(() => {
23+
if (hookCalled) {
24+
expect(hookCalled).toBe(true);
25+
server.close();
26+
done();
27+
}
28+
}, 1000);
29+
});
30+
});
31+
});
32+
});

packages/cli/tests/lib/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const minimatch = require('minimatch');
55
const pRetry = require('p-retry');
66
const { promisify } = require('util');
77
const glob = promisify(require('glob').glob);
8+
const hooks = require('../../lib/lib/webpack/hooks');
89

910
const PER = 0.05; // % diff
1011
const LOG = !!process.env.WITH_LOG;
@@ -68,4 +69,5 @@ module.exports = {
6869
sleep,
6970
hasKey,
7071
isWithin,
72+
hooks,
7173
};

0 commit comments

Comments
 (0)