Skip to content

Commit

Permalink
Introduce custom hooks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tooxie authored and alvaro-kianava committed Jun 3, 2021
1 parent 0d59077 commit 1e7a999
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/cli/lib/lib/webpack/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const SyncHook = require('tapable').SyncHook;

module.exports = {
devServerRunning: new SyncHook(),
};
2 changes: 2 additions & 0 deletions packages/cli/lib/lib/webpack/run-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const clientConfig = require('./webpack-client-config');
const serverConfig = require('./webpack-server-config');
const transformConfig = require('./transform-config');
const { error, isDir, warn } = require('../../util');
const { devServerRunning } = require('./hooks');

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

showStats(stats, false);
devServerRunning.call();
});

compiler.hooks.failed.tap('CliDevPlugin', rej);
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/tests/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { create, watch } = require('./lib/cli');
const { hooks } = require('./lib/utils');

describe('preact', () => {
let intervalId;

afterEach(() => {
clearInterval(intervalId);
intervalId = null;
});

it('should emit a devServerRunning event after the server starts', (done) => {
let hookCalled;
hooks.devServerRunning.tap('TestPlugin', () => {
hookCalled = true;
});

create('default').then((app) => {
watch(app, 8083).then((server) => {
// We need to wait not only for the server to start but also for the
// stats to be printed to stdout.
intervalId = setInterval(() => {
if (hookCalled) {
expect(hookCalled).toBe(true);
server.close();
done();
}
}, 1000);
});
});
});
});
2 changes: 2 additions & 0 deletions packages/cli/tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const minimatch = require('minimatch');
const pRetry = require('p-retry');
const { promisify } = require('util');
const glob = promisify(require('glob').glob);
const hooks = require('../../lib/lib/webpack/hooks');

const PER = 0.05; // % diff
const LOG = !!process.env.WITH_LOG;
Expand Down Expand Up @@ -68,4 +69,5 @@ module.exports = {
sleep,
hasKey,
isWithin,
hooks,
};

0 comments on commit 1e7a999

Please sign in to comment.