Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce devServerRunning hook #1564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};