Skip to content

refactor: change order of params for Server #3273

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

Merged
merged 12 commits into from
May 13, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/api/middleware/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const webpackConfig = require('./webpack.config');

const compiler = Webpack(webpackConfig);
const devServerOptions = webpackConfig.devServer;
const server = new WebpackDevServer(compiler, devServerOptions);
const server = new WebpackDevServer(devServerOptions, compiler);

server.listen(8080, '127.0.0.1', () => {
console.log('Starting server on http://localhost:8080');
Expand Down
2 changes: 1 addition & 1 deletion examples/api/simple/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const compiler = Webpack(webpackConfig);
const devServerOptions = Object.assign({}, webpackConfig.devServer, {
open: true,
});
const server = new WebpackDevServer(compiler, devServerOptions);
const server = new WebpackDevServer(devServerOptions, compiler);

server.listen(8080, '127.0.0.1', () => {
console.log('Starting server on http://localhost:8080');
Expand Down
7 changes: 6 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ if (!process.env.WEBPACK_SERVE) {
}

class Server {
constructor(compiler, options = {}) {
constructor(options = {}, compiler) {
// TODO: remove this after plugin support is published
if (options.hooks) {
[options, compiler] = [compiler, options];
}

validate(schema, options, 'webpack Dev Server');

this.compiler = compiler;
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function startFullSetup(config, options, done) {

const compiler = webpack(config);

server = new Server(compiler, options);
server = new Server(options, compiler);

let port;
if (Object.prototype.hasOwnProperty.call(options, 'port')) {
Expand Down
57 changes: 43 additions & 14 deletions test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const baseDevConfig = {
static: false,
};

const createServer = (compiler, options) => new Server(options, compiler);

describe('Server', () => {
describe('sockjs', () => {
it('add decorateConnection', () => {
Expand Down Expand Up @@ -48,7 +50,7 @@ describe('Server', () => {

it('add hot option', (done) => {
const compiler = webpack(config);
const server = new Server(
const server = createServer(
compiler,
Object.assign({}, baseDevConfig, {
hot: true,
Expand All @@ -65,9 +67,36 @@ describe('Server', () => {
compiler.run(() => {});
});

// TODO: remove this after plugin support is published
it('should create and run server with old parameters order', (done) => {
const compiler = webpack(config);
const server = new Server(compiler, baseDevConfig);

getEntries(server);

compiler.hooks.done.tap('webpack-dev-server', () => {
expect(entries).toMatchSnapshot('oldparam');
server.close(done);
});

compiler.run(() => {});
});

// TODO: remove this after plugin support is published
it('should create and run server with MultiCompiler with old parameters order', (done) => {
const compiler = webpack([config, config]);
const server = new Server(compiler, baseDevConfig);

compiler.hooks.done.tap('webpack-dev-server', () => {
server.close(done);
});

compiler.run(() => {});
});

it('add hot-only option', (done) => {
const compiler = webpack(config);
const server = new Server(
const server = createServer(
compiler,
Object.assign({}, baseDevConfig, {
hot: 'only',
Expand All @@ -87,7 +116,7 @@ describe('Server', () => {

it('test server error reporting', () => {
const compiler = webpack(config);
const server = new Server(compiler, baseDevConfig);
const server = createServer(compiler, baseDevConfig);

const emitError = () => server.server.emit('error', new Error('Error !!!'));

Expand All @@ -114,7 +143,7 @@ describe('Server', () => {
});

const compiler = webpack(config);
const server = new Server(compiler, baseDevConfig);
const server = createServer(compiler, baseDevConfig);

compiler.hooks.done.tap('webpack-dev-server', (s) => {
const output = server.getStats(s);
Expand Down Expand Up @@ -152,7 +181,7 @@ describe('Server', () => {
host: 'bad.host',
};

server = new Server(compiler, options);
server = createServer(compiler, options);

if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
Expand All @@ -166,7 +195,7 @@ describe('Server', () => {
const headers = {
host: 'localhost',
};
server = new Server(compiler, options);
server = createServer(compiler, options);
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
Expand All @@ -181,7 +210,7 @@ describe('Server', () => {
host: '127.0.0.1',
};

server = new Server(compiler, options);
server = createServer(compiler, options);

if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
Expand All @@ -200,7 +229,7 @@ describe('Server', () => {
'[ad42::1de2:54c2:c2fa:1234]:8080',
];

server = new Server(compiler, options);
server = createServer(compiler, options);

tests.forEach((test) => {
const headers = { host: test };
Expand All @@ -220,7 +249,7 @@ describe('Server', () => {
host: 'test.hostname:80',
};

server = new Server(compiler, options);
server = createServer(compiler, options);

if (server.checkHost(headers)) {
throw new Error("Validation didn't fail");
Expand All @@ -234,7 +263,7 @@ describe('Server', () => {
const headers = {
origin: 'https://test.host',
};
server = new Server(compiler, options);
server = createServer(compiler, options);
if (!server.checkOrigin(headers)) {
throw new Error("Validation didn't fail");
}
Expand All @@ -244,7 +273,7 @@ describe('Server', () => {
it('should allow hosts in firewall', () => {
const tests = ['test.host', 'test2.host', 'test3.host'];
const options = { firewall: tests };
server = new Server(compiler, options);
server = createServer(compiler, options);
tests.forEach((test) => {
const headers = { host: test };
if (!server.checkHost(headers)) {
Expand All @@ -255,7 +284,7 @@ describe('Server', () => {

it('should allow hosts that pass a wildcard in firewall', () => {
const options = { firewall: ['.example.com'] };
server = new Server(compiler, options);
server = createServer(compiler, options);
const tests = [
'www.example.com',
'subdomain.example.com',
Expand All @@ -278,7 +307,7 @@ describe('Server', () => {
describe('Testing callback functions on calling invalidate without callback', () => {
it('should use default `noop` callback', (done) => {
const compiler = webpack(config);
const server = new Server(compiler, baseDevConfig);
const server = createServer(compiler, baseDevConfig);

server.invalidate();
expect(server.middleware.context.callbacks.length).toEqual(1);
Expand All @@ -295,7 +324,7 @@ describe('Server', () => {
it('should use `callback` function', (done) => {
const compiler = webpack(config);
const callback = jest.fn();
const server = new Server(compiler, baseDevConfig);
const server = createServer(compiler, baseDevConfig);

server.invalidate(callback);

Expand Down
19 changes: 19 additions & 0 deletions test/server/__snapshots__/Server.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ Array [
],
]
`;

exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
Array [
Array [
"client",
"index.js?http:",
"0.0.0.0&port=8100",
],
Array [
"node_modules",
"webpack",
"hot",
"dev-server.js",
],
Array [
"foo.js",
],
]
`;
19 changes: 19 additions & 0 deletions test/server/__snapshots__/Server.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ Array [
],
]
`;

exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
Array [
Array [
"client",
"index.js?http:",
"0.0.0.0&port=8100",
],
Array [
"node_modules",
"webpack",
"hot",
"dev-server.js",
],
Array [
"foo.js",
],
]
`;
Loading