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

feat: add webpack as argument to before and after options #1760

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class Server {
},
before: () => {
if (typeof options.before === 'function') {
options.before(app, this);
options.before(app, this, compiler);
}
},
middleware: () => {
Expand All @@ -507,7 +507,7 @@ class Server {
},
after: () => {
if (typeof options.after === 'function') {
options.after(app, this);
options.after(app, this, compiler);
}
},
headers: () => {
Expand Down
76 changes: 76 additions & 0 deletions test/BeforeAndAfter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

const request = require('supertest');
const helper = require('./helper');
const config = require('./fixtures/simple-config/webpack.config');

describe('Before And After options', () => {
let server;
let req;

beforeAll((done) => {
server = helper.start(
config,
{
before: (app, server, compiler) => {
if (!app) {
throw new Error('app is not defined');
}

if (!server) {
throw new Error('server is not defined');
}

if (!compiler) {
throw new Error('compiler is not defined');
}

app.get('/before/some/path', (req, res) => {
res.send('before');
});
},
after: (app, server, compiler) => {
if (!app) {
throw new Error('app is not defined');
}

if (!server) {
throw new Error('server is not defined');
}

if (!compiler) {
throw new Error('compiler is not defined');
}

app.get('/after/some/path', (req, res) => {
res.send('after');
});
},
},
done
);
req = request(server.app);
});

afterAll(helper.close);

it('should handle before route', () => {
return req
.get('/before/some/path')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.then((response) => {
expect(response.text).toBe('before');
});
});

it('should handle after route', () => {
return req
.get('/after/some/path')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.then((response) => {
expect(response.text).toBe('after');
});
});
});
12 changes: 11 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
if (options.quiet === undefined) {
options.quiet = true;
}

// originally, inline was not working by default for tests with the API
// if you need to test inline, it should be set explicitly,
// rather than expecting it to be defaulted to
Expand All @@ -26,19 +27,26 @@ module.exports = {
) {
options.inline = false;
}

// defaulting to this will hopefully help with problems on OSX in tests
if (options.watchOptions === undefined) {
options.watchOptions = {
poll: true,
};
}

const compiler = webpack(config);

server = new Server(compiler, options);

const port = options.port || 8080;
const host = options.host || 'localhost';

server.listen(port, host, (err) => {
if (err) return done(err);
if (err) {
return done(err);
}

done();
});

Expand All @@ -57,9 +65,11 @@ module.exports = {
};

const fullSetup = this.startFullSetup(config, options, ready);

// wait for compilation, since dev server can start before this
// https://github.com/webpack/webpack-dev-server/issues/847
fullSetup.compiler.hooks.done.tap('done', ready);

return fullSetup;
},
startAwaitingCompilation(config, options, done) {
Expand Down