-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Insert client implementation from within Dev Server API #1933
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e95cdd
refactor(client): insert client implementation as entry
knagaitsev 631e76b
refactor(client): removed entry of client, renamed helper
knagaitsev 376d470
refactor(client): made static method to get client path
knagaitsev c80536c
test(client): added e2e ProvidePlugin tests
knagaitsev 536a11d
test(client): added SockJSClient test
knagaitsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable | ||
no-unused-vars | ||
*/ | ||
module.exports = class BaseClient { | ||
static getClientPath(options) { | ||
throw new Error('Client needs implementation'); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable | ||
no-unused-vars | ||
*/ | ||
const SockJS = require('sockjs-client/dist/sockjs'); | ||
const BaseClient = require('./BaseClient'); | ||
|
||
module.exports = class SockJSClient extends BaseClient { | ||
constructor(url) { | ||
super(); | ||
this.sock = new SockJS(url); | ||
} | ||
|
||
static getClientPath(options) { | ||
return require.resolve('./SockJSClient'); | ||
} | ||
|
||
onOpen(f) { | ||
this.sock.onopen = f; | ||
} | ||
|
||
onClose(f) { | ||
this.sock.onclose = f; | ||
} | ||
|
||
// call f with the message string as the first argument | ||
onMessage(f) { | ||
this.sock.onmessage = (e) => { | ||
f(e.data); | ||
}; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
const BaseClient = require('./BaseClient'); | ||
|
||
module.exports = class WebsocketClient extends BaseClient {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
*/ | ||
const webpack = require('webpack'); | ||
const addEntries = require('./addEntries'); | ||
const SockJSClient = require('./../clients/SockJSClient'); | ||
|
||
function updateCompiler(compiler, options) { | ||
if (options.inline !== false) { | ||
|
@@ -48,6 +49,11 @@ function updateCompiler(compiler, options) { | |
compilers.forEach((compiler) => { | ||
const config = compiler.options; | ||
compiler.hooks.entryOption.call(config.context, config.entry); | ||
|
||
const providePlugin = new webpack.ProvidePlugin({ | ||
__webpack_dev_server_client__: SockJSClient.getClientPath(options), | ||
}); | ||
providePlugin.apply(compiler); | ||
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. As i said above |
||
}); | ||
|
||
// do not apply the plugin unless it didn't exist before. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const http = require('http'); | ||
const express = require('express'); | ||
const sockjs = require('sockjs'); | ||
const SockJSClient = require('../lib/clients/SockJSClient'); | ||
|
||
describe('SockJSClient', () => { | ||
let socketServer; | ||
let listeningApp; | ||
|
||
beforeAll((done) => { | ||
// eslint-disable-next-line new-cap | ||
const app = new express(); | ||
listeningApp = http.createServer(app); | ||
listeningApp.listen(8080, 'localhost', () => { | ||
socketServer = sockjs.createServer(); | ||
socketServer.installHandlers(listeningApp, { | ||
prefix: '/sockjs-node', | ||
}); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('client', () => { | ||
it('should open, recieve message, and close', (done) => { | ||
socketServer.on('connection', (connection) => { | ||
connection.write('hello world'); | ||
setTimeout(() => { | ||
connection.close(); | ||
}, 1000); | ||
}); | ||
const client = new SockJSClient('http://localhost:8080/sockjs-node'); | ||
const data = []; | ||
client.onOpen(() => { | ||
data.push('open'); | ||
}); | ||
client.onClose(() => { | ||
data.push('close'); | ||
}); | ||
client.onMessage((msg) => { | ||
data.push(msg); | ||
}); | ||
setTimeout(() => { | ||
expect(data.length).toEqual(3); | ||
expect(data[0]).toEqual('open'); | ||
expect(data[1]).toEqual('hello world'); | ||
expect(data[2]).toEqual('close'); | ||
done(); | ||
}, 3000); | ||
}); | ||
}); | ||
|
||
afterAll((done) => { | ||
listeningApp.close(() => { | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const testServer = require('../helpers/test-server'); | ||
const config = require('../fixtures/provide-plugin-config/webpack.config'); | ||
const runBrowser = require('../helpers/run-browser'); | ||
|
||
describe('ProvidePlugin', () => { | ||
describe('inline', () => { | ||
beforeAll((done) => { | ||
const options = { | ||
port: 9000, | ||
host: '0.0.0.0', | ||
inline: true, | ||
watchOptions: { | ||
poll: true, | ||
}, | ||
}; | ||
testServer.startAwaitingCompilation(config, options, done); | ||
}); | ||
|
||
afterAll(testServer.close); | ||
|
||
describe('on browser client', () => { | ||
jest.setTimeout(30000); | ||
|
||
it('should inject SockJS client implementation', (done) => { | ||
runBrowser().then(({ page, browser }) => { | ||
page.waitForNavigation({ waitUntil: 'load' }).then(() => { | ||
page | ||
.evaluate(() => { | ||
return window.injectedClient === window.expectedClient; | ||
}) | ||
.then((isCorrectClient) => { | ||
expect(isCorrectClient).toBeTruthy(); | ||
browser.close().then(done); | ||
}); | ||
}); | ||
page.goto('http://localhost:9000/main'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('not inline', () => { | ||
beforeAll((done) => { | ||
const options = { | ||
port: 9000, | ||
host: '0.0.0.0', | ||
inline: false, | ||
watchOptions: { | ||
poll: true, | ||
}, | ||
}; | ||
testServer.startAwaitingCompilation(config, options, done); | ||
}); | ||
|
||
afterAll(testServer.close); | ||
|
||
describe('on browser client', () => { | ||
jest.setTimeout(30000); | ||
|
||
it('should not inject client implementation', (done) => { | ||
runBrowser().then(({ page, browser }) => { | ||
page.waitForNavigation({ waitUntil: 'load' }).then(() => { | ||
page | ||
.evaluate(() => { | ||
// eslint-disable-next-line no-undefined | ||
return window.injectedClient === undefined; | ||
}) | ||
.then((isCorrectClient) => { | ||
expect(isCorrectClient).toBeTruthy(); | ||
browser.close().then(done); | ||
}); | ||
}); | ||
page.goto('http://localhost:9000/main'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const SockJSClient = require('../../../lib/clients/SockJSClient'); | ||
|
||
window.expectedClient = SockJSClient; | ||
// eslint-disable-next-line camelcase, no-undef | ||
window.injectedClient = __webpack_dev_server_client__; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
mode: 'development', | ||
context: __dirname, | ||
entry: './foo.js', | ||
output: { | ||
path: '/', | ||
}, | ||
node: false, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Will be great standardize client API, but we can do this in future not in this PR
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.
So you want to revert this and have the new client files, but not use them yet?
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.
Keep this as is in this PR, we discussion about this in future