Skip to content

Commit 4c3915a

Browse files
authored
refactor: change order of params for Server (#3273)
1 parent 98d2acd commit 4c3915a

File tree

11 files changed

+128
-52
lines changed

11 files changed

+128
-52
lines changed

examples/api/middleware/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const webpackConfig = require('./webpack.config');
66

77
const compiler = Webpack(webpackConfig);
88
const devServerOptions = webpackConfig.devServer;
9-
const server = new WebpackDevServer(compiler, devServerOptions);
9+
const server = new WebpackDevServer(devServerOptions, compiler);
1010

1111
server.listen(8080, '127.0.0.1', () => {
1212
console.log('Starting server on http://localhost:8080');

examples/api/simple/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const compiler = Webpack(webpackConfig);
88
const devServerOptions = Object.assign({}, webpackConfig.devServer, {
99
open: true,
1010
});
11-
const server = new WebpackDevServer(compiler, devServerOptions);
11+
const server = new WebpackDevServer(devServerOptions, compiler);
1212

1313
server.listen(8080, '127.0.0.1', () => {
1414
console.log('Starting server on http://localhost:8080');

lib/Server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ if (!process.env.WEBPACK_SERVE) {
3939
}
4040

4141
class Server {
42-
constructor(compiler, options = {}) {
42+
constructor(options = {}, compiler) {
43+
// TODO: remove this after plugin support is published
44+
if (options.hooks) {
45+
[options, compiler] = [compiler, options];
46+
}
47+
4348
validate(schema, options, 'webpack Dev Server');
4449

4550
this.compiler = compiler;

test/helpers/test-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function startFullSetup(config, options, done) {
1818

1919
const compiler = webpack(config);
2020

21-
server = new Server(compiler, options);
21+
server = new Server(options, compiler);
2222

2323
let port;
2424
if (Object.prototype.hasOwnProperty.call(options, 'port')) {

test/server/Server.test.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const baseDevConfig = {
1515
static: false,
1616
};
1717

18+
const createServer = (compiler, options) => new Server(options, compiler);
19+
1820
describe('Server', () => {
1921
describe('sockjs', () => {
2022
it('add decorateConnection', () => {
@@ -48,7 +50,7 @@ describe('Server', () => {
4850

4951
it('add hot option', (done) => {
5052
const compiler = webpack(config);
51-
const server = new Server(
53+
const server = createServer(
5254
compiler,
5355
Object.assign({}, baseDevConfig, {
5456
hot: true,
@@ -65,9 +67,36 @@ describe('Server', () => {
6567
compiler.run(() => {});
6668
});
6769

70+
// TODO: remove this after plugin support is published
71+
it('should create and run server with old parameters order', (done) => {
72+
const compiler = webpack(config);
73+
const server = new Server(compiler, baseDevConfig);
74+
75+
getEntries(server);
76+
77+
compiler.hooks.done.tap('webpack-dev-server', () => {
78+
expect(entries).toMatchSnapshot('oldparam');
79+
server.close(done);
80+
});
81+
82+
compiler.run(() => {});
83+
});
84+
85+
// TODO: remove this after plugin support is published
86+
it('should create and run server with MultiCompiler with old parameters order', (done) => {
87+
const compiler = webpack([config, config]);
88+
const server = new Server(compiler, baseDevConfig);
89+
90+
compiler.hooks.done.tap('webpack-dev-server', () => {
91+
server.close(done);
92+
});
93+
94+
compiler.run(() => {});
95+
});
96+
6897
it('add hot-only option', (done) => {
6998
const compiler = webpack(config);
70-
const server = new Server(
99+
const server = createServer(
71100
compiler,
72101
Object.assign({}, baseDevConfig, {
73102
hot: 'only',
@@ -87,7 +116,7 @@ describe('Server', () => {
87116

88117
it('test server error reporting', () => {
89118
const compiler = webpack(config);
90-
const server = new Server(compiler, baseDevConfig);
119+
const server = createServer(compiler, baseDevConfig);
91120

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

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

116145
const compiler = webpack(config);
117-
const server = new Server(compiler, baseDevConfig);
146+
const server = createServer(compiler, baseDevConfig);
118147

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

155-
server = new Server(compiler, options);
184+
server = createServer(compiler, options);
156185

157186
if (!server.checkHost(headers)) {
158187
throw new Error("Validation didn't fail");
@@ -166,7 +195,7 @@ describe('Server', () => {
166195
const headers = {
167196
host: 'localhost',
168197
};
169-
server = new Server(compiler, options);
198+
server = createServer(compiler, options);
170199
if (!server.checkHost(headers)) {
171200
throw new Error("Validation didn't fail");
172201
}
@@ -181,7 +210,7 @@ describe('Server', () => {
181210
host: '127.0.0.1',
182211
};
183212

184-
server = new Server(compiler, options);
213+
server = createServer(compiler, options);
185214

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

203-
server = new Server(compiler, options);
232+
server = createServer(compiler, options);
204233

205234
tests.forEach((test) => {
206235
const headers = { host: test };
@@ -220,7 +249,7 @@ describe('Server', () => {
220249
host: 'test.hostname:80',
221250
};
222251

223-
server = new Server(compiler, options);
252+
server = createServer(compiler, options);
224253

225254
if (server.checkHost(headers)) {
226255
throw new Error("Validation didn't fail");
@@ -234,7 +263,7 @@ describe('Server', () => {
234263
const headers = {
235264
origin: 'https://test.host',
236265
};
237-
server = new Server(compiler, options);
266+
server = createServer(compiler, options);
238267
if (!server.checkOrigin(headers)) {
239268
throw new Error("Validation didn't fail");
240269
}
@@ -244,7 +273,7 @@ describe('Server', () => {
244273
it('should allow hosts in firewall', () => {
245274
const tests = ['test.host', 'test2.host', 'test3.host'];
246275
const options = { firewall: tests };
247-
server = new Server(compiler, options);
276+
server = createServer(compiler, options);
248277
tests.forEach((test) => {
249278
const headers = { host: test };
250279
if (!server.checkHost(headers)) {
@@ -255,7 +284,7 @@ describe('Server', () => {
255284

256285
it('should allow hosts that pass a wildcard in firewall', () => {
257286
const options = { firewall: ['.example.com'] };
258-
server = new Server(compiler, options);
287+
server = createServer(compiler, options);
259288
const tests = [
260289
'www.example.com',
261290
'subdomain.example.com',
@@ -278,7 +307,7 @@ describe('Server', () => {
278307
describe('Testing callback functions on calling invalidate without callback', () => {
279308
it('should use default `noop` callback', (done) => {
280309
const compiler = webpack(config);
281-
const server = new Server(compiler, baseDevConfig);
310+
const server = createServer(compiler, baseDevConfig);
282311

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

300329
server.invalidate(callback);
301330

test/server/__snapshots__/Server.test.js.snap.webpack4

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ Array [
3737
],
3838
]
3939
`;
40+
41+
exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
42+
Array [
43+
Array [
44+
"client",
45+
"index.js?http:",
46+
"0.0.0.0&port=8100",
47+
],
48+
Array [
49+
"node_modules",
50+
"webpack",
51+
"hot",
52+
"dev-server.js",
53+
],
54+
Array [
55+
"foo.js",
56+
],
57+
]
58+
`;

test/server/__snapshots__/Server.test.js.snap.webpack5

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ Array [
3737
],
3838
]
3939
`;
40+
41+
exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
42+
Array [
43+
Array [
44+
"client",
45+
"index.js?http:",
46+
"0.0.0.0&port=8100",
47+
],
48+
Array [
49+
"node_modules",
50+
"webpack",
51+
"hot",
52+
"dev-server.js",
53+
],
54+
Array [
55+
"foo.js",
56+
],
57+
]
58+
`;

0 commit comments

Comments
 (0)