-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(client): add clientMode option #1977
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
Changes from all commits
3833725
d784686
3ec66a6
5ef83e5
ed53db6
e205363
5745e7d
c4453fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
function getSocketClientPath(options) { | ||
let ClientImplementation; | ||
let clientImplFound = true; | ||
switch (typeof options.clientMode) { | ||
case 'string': | ||
// could be 'sockjs', in the future 'ws', or a path that should be required | ||
if (options.clientMode === 'sockjs') { | ||
// eslint-disable-next-line global-require | ||
ClientImplementation = require('../../client/clients/SockJSClient'); | ||
} else { | ||
try { | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
ClientImplementation = require(options.clientMode); | ||
} catch (e) { | ||
clientImplFound = false; | ||
} | ||
} | ||
break; | ||
default: | ||
clientImplFound = false; | ||
} | ||
|
||
if (!clientImplFound) { | ||
throw new Error( | ||
"clientMode must be a string denoting a default implementation (e.g. 'sockjs') or a full path to " + | ||
'a JS file which exports a class extending BaseClient (webpack-dev-server/client-src/clients/BaseClient) ' + | ||
'via require.resolve(...)' | ||
); | ||
} | ||
|
||
return ClientImplementation.getClientPath(options); | ||
} | ||
|
||
module.exports = getSocketClientPath; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
|
||
const testServer = require('../helpers/test-server'); | ||
const config = require('../fixtures/client-config/webpack.config'); | ||
const runBrowser = require('../helpers/run-browser'); | ||
const port = require('../ports-map').ClientMode; | ||
|
||
describe('clientMode', () => { | ||
describe('sockjs', () => { | ||
beforeAll((done) => { | ||
const options = { | ||
port, | ||
host: '0.0.0.0', | ||
inline: true, | ||
clientMode: 'sockjs', | ||
}; | ||
testServer.startAwaitingCompilation(config, options, done); | ||
}); | ||
|
||
describe('on browser client', () => { | ||
it('logs as usual', (done) => { | ||
runBrowser().then(({ page, browser }) => { | ||
const res = []; | ||
page.goto(`http://localhost:${port}/main`); | ||
page.on('console', ({ _text }) => { | ||
res.push(_text); | ||
}); | ||
|
||
setTimeout(() => { | ||
testServer.close(() => { | ||
// make sure the client gets the close message | ||
setTimeout(() => { | ||
browser.close().then(() => { | ||
expect(res).toMatchSnapshot(); | ||
done(); | ||
}); | ||
}, 1000); | ||
}); | ||
}, 3000); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('custom client', () => { | ||
beforeAll((done) => { | ||
const options = { | ||
port, | ||
host: '0.0.0.0', | ||
inline: true, | ||
clientMode: require.resolve( | ||
'../fixtures/custom-client/CustomSockJSClient' | ||
), | ||
}; | ||
testServer.startAwaitingCompilation(config, options, done); | ||
}); | ||
|
||
describe('on browser client', () => { | ||
it('logs additional messages to console', (done) => { | ||
runBrowser().then(({ page, browser }) => { | ||
const res = []; | ||
page.goto(`http://localhost:${port}/main`); | ||
page.on('console', ({ _text }) => { | ||
res.push(_text); | ||
}); | ||
|
||
setTimeout(() => { | ||
testServer.close(() => { | ||
// make sure the client gets the close message | ||
setTimeout(() => { | ||
browser.close().then(() => { | ||
expect(res).toMatchSnapshot(); | ||
done(); | ||
}); | ||
}, 1000); | ||
}); | ||
}, 3000); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`clientMode custom client on browser client logs additional messages to console 1`] = ` | ||
Array [ | ||
"Hey.", | ||
"open", | ||
"liveReload", | ||
"[WDS] Live Reloading enabled.", | ||
"hash", | ||
"ok", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hiroppy |
||
"close", | ||
"[WDS] Disconnected!", | ||
] | ||
`; | ||
|
||
exports[`clientMode sockjs on browser client logs as usual 1`] = ` | ||
Array [ | ||
"Hey.", | ||
"[WDS] Live Reloading enabled.", | ||
"[WDS] Disconnected!", | ||
] | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable | ||
no-unused-vars | ||
*/ | ||
const SockJS = require('sockjs-client/dist/sockjs'); | ||
const BaseClient = require('../../../client/clients/BaseClient'); | ||
|
||
module.exports = class SockJSClient extends BaseClient { | ||
constructor(url) { | ||
super(); | ||
this.sock = new SockJS(url); | ||
} | ||
|
||
static getClientPath(options) { | ||
return require.resolve('./CustomSockJSClient'); | ||
} | ||
|
||
onOpen(f) { | ||
this.sock.onopen = () => { | ||
console.log('open'); | ||
f(); | ||
}; | ||
} | ||
|
||
onClose(f) { | ||
this.sock.onclose = () => { | ||
console.log('close'); | ||
f(); | ||
}; | ||
} | ||
|
||
// call f with the message string as the first argument | ||
onMessage(f) { | ||
this.sock.onmessage = (e) => { | ||
const obj = JSON.parse(e.data); | ||
console.log(obj.type); | ||
f(e.data); | ||
}; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
const getSocketClientPath = require('../../../lib/utils/getSocketClientPath'); | ||
// 'npm run prepare' must be done for this test to pass | ||
const sockjsClientPath = require.resolve( | ||
'../../../client/clients/SockJSClient' | ||
); | ||
const baseClientPath = require.resolve('../../../client/clients/BaseClient'); | ||
|
||
describe('getSocketClientPath', () => { | ||
it("should work with clientMode: 'sockjs'", () => { | ||
let result; | ||
|
||
expect(() => { | ||
result = getSocketClientPath({ | ||
clientMode: 'sockjs', | ||
}); | ||
}).not.toThrow(); | ||
|
||
expect(result).toEqual(sockjsClientPath); | ||
}); | ||
|
||
it('should work with clientMode: SockJSClient full path', () => { | ||
let result; | ||
|
||
expect(() => { | ||
result = getSocketClientPath({ | ||
clientMode: sockjsClientPath, | ||
}); | ||
}).not.toThrow(); | ||
|
||
expect(result).toEqual(sockjsClientPath); | ||
}); | ||
|
||
it('should throw with clientMode: bad path', () => { | ||
expect(() => { | ||
getSocketClientPath({ | ||
clientMode: '/bad/path/to/implementation', | ||
}); | ||
}).toThrow(/clientMode must be a string/); | ||
}); | ||
|
||
it('should throw with clientMode: unimplemented client', () => { | ||
expect(() => { | ||
getSocketClientPath({ | ||
clientMode: baseClientPath, | ||
}); | ||
}).toThrow('Client needs implementation'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we move this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evilebottnawi I reorganized it to have the default setting for
serverMode
andclientMode
together above that.updateCompiler
usesthis.options.clientMode
, so the default setting for it must be set beforeupdateCompiler
is called. It can be switched back, I just thought it would be nice to have default setting forserverMode
andclientMode
right next to one another