Skip to content

Commit 8c90abb

Browse files
committed
fix(client): update SockJSClient path resolution
1 parent eb69e4c commit 8c90abb

File tree

7 files changed

+39
-42
lines changed

7 files changed

+39
-42
lines changed

client-src/default/socket.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
camelcase
66
*/
77

8-
let Client;
9-
try {
10-
// if __webpack_dev_server_client__ is undefined, we should fall back
11-
// to SockJSClient
12-
Client = __webpack_dev_server_client__;
13-
} catch (e) {
14-
// this SockJSClient is here as a default fallback, in case inline mode
15-
// is off or the client is not injected. This will be switched to
16-
// WebsocketClient when it becomes the default
17-
18-
// eslint-disable-next-line global-require
19-
const SockJSClient = require('../clients/SockJSClient');
20-
Client = SockJSClient;
21-
}
8+
// this SockJSClient is here as a default fallback, in case inline mode
9+
// is off or the client is not injected. This will be switched to
10+
// WebsocketClient when it becomes the default
11+
12+
// important: the path to SockJSClient here is made to work in the 'client'
13+
// directory, but is updated via the webpack compilation when compiled from
14+
// the 'client-src' directory
15+
const Client =
16+
typeof __webpack_dev_server_client__ !== 'undefined'
17+
? __webpack_dev_server_client__
18+
: // eslint-disable-next-line import/no-unresolved
19+
require('./clients/SockJSClient');
2220

2321
let retries = 0;
2422
let client = null;

client-src/default/webpack.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const webpack = require('webpack');
4+
35
module.exports = {
46
mode: 'production',
57
module: {
@@ -15,4 +17,12 @@ module.exports = {
1517
},
1618
],
1719
},
20+
plugins: [
21+
new webpack.NormalModuleReplacementPlugin(/\/clients\//, (resource) => {
22+
resource.request = resource.request.replace(
23+
/\/clients\//,
24+
'/../clients/'
25+
);
26+
}),
27+
],
1828
};

client-src/live/webpack.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const webpack = require('webpack');
45
const CopyPlugin = require('copy-webpack-plugin');
56

67
module.exports = {
@@ -33,5 +34,11 @@ module.exports = {
3334
to: path.resolve(__dirname, '../../client/live.html'),
3435
},
3536
]),
37+
new webpack.NormalModuleReplacementPlugin(/\/clients\//, (resource) => {
38+
resource.request = resource.request.replace(
39+
/\/clients\//,
40+
'/../clients/'
41+
);
42+
}),
3643
],
3744
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"test": "npm run test:coverage",
2626
"pretest": "npm run lint",
2727
"prepare": "rimraf ./ssl/*.pem && npm run build:client",
28-
"build:client:default": "babel client-src/default --out-dir client/default --ignore \"./client-src/default/*.config.js\"",
28+
"build:client:default": "babel client-src/default --out-dir client --ignore \"./client-src/default/*.config.js\"",
2929
"build:client:clients": "babel client-src/clients --out-dir client/clients",
3030
"build:client:index": "webpack ./client-src/default/index.js -o client/index.bundle.js --color --config client-src/default/webpack.config.js",
3131
"build:client:live": "webpack ./client-src/live/index.js -o client/live.bundle.js --color --config client-src/live/webpack.config.js",

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ exports[`Server addEntries add hot option 1`] = `
44
Array [
55
Array [
66
"client",
7-
"default",
87
"index.js?http:",
98
"localhost:8090",
109
],
@@ -35,7 +34,6 @@ exports[`Server addEntries add hotOnly option 1`] = `
3534
Array [
3635
Array [
3736
"client",
38-
"default",
3937
"index.js?http:",
4038
"localhost:8090",
4139
],

test/server/inline-option.test.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ describe('inline option', () => {
2727
afterAll(testServer.close);
2828

2929
it('should include inline client script in the bundle', (done) => {
30-
const url = new RegExp(
31-
`client/default/index.js\\?http://0.0.0.0:${port}`
32-
);
30+
const url = new RegExp(`client/index.js\\?http://0.0.0.0:${port}`);
3331

3432
req.get('/main.js').expect(200, url, done);
3533
});
@@ -56,9 +54,7 @@ describe('inline option', () => {
5654
afterAll(testServer.close);
5755

5856
it('should include inline client script in the bundle', (done) => {
59-
const url = new RegExp(
60-
`client/default/index.js\\?http://0.0.0.0:${port}`
61-
);
57+
const url = new RegExp(`client/index.js\\?http://0.0.0.0:${port}`);
6258

6359
req.get('/main.js').expect(200, url, done);
6460
});
@@ -85,9 +81,7 @@ describe('inline option', () => {
8581
.get('/main.js')
8682
.expect(200)
8783
.then(({ text }) => {
88-
expect(
89-
text.includes(`client/default/index.js?http://0.0.0.0:${port}`)
90-
);
84+
expect(text.includes(`client/index.js?http://0.0.0.0:${port}`));
9185
done();
9286
});
9387
});

test/server/utils/addEntries.test.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ describe('addEntries util', () => {
1717

1818
expect(webpackOptions.entry.length).toEqual(2);
1919
expect(
20-
normalize(webpackOptions.entry[0]).indexOf('client/default/index.js?') !==
21-
-1
20+
normalize(webpackOptions.entry[0]).indexOf('client/index.js?') !== -1
2221
).toBeTruthy();
2322
expect(normalize(webpackOptions.entry[1])).toEqual('./foo.js');
2423
});
@@ -34,8 +33,7 @@ describe('addEntries util', () => {
3433

3534
expect(webpackOptions.entry.length).toEqual(3);
3635
expect(
37-
normalize(webpackOptions.entry[0]).indexOf('client/default/index.js?') !==
38-
-1
36+
normalize(webpackOptions.entry[0]).indexOf('client/index.js?') !== -1
3937
).toBeTruthy();
4038
expect(webpackOptions.entry[1]).toEqual('./foo.js');
4139
expect(webpackOptions.entry[2]).toEqual('./bar.js');
@@ -56,9 +54,7 @@ describe('addEntries util', () => {
5654
expect(webpackOptions.entry.foo.length).toEqual(2);
5755

5856
expect(
59-
normalize(webpackOptions.entry.foo[0]).indexOf(
60-
'client/default/index.js?'
61-
) !== -1
57+
normalize(webpackOptions.entry.foo[0]).indexOf('client/index.js?') !== -1
6258
).toBeTruthy();
6359
expect(webpackOptions.entry.foo[1]).toEqual('./foo.js');
6460
expect(webpackOptions.entry.bar[1]).toEqual('./bar.js');
@@ -296,9 +292,7 @@ describe('addEntries util', () => {
296292

297293
if (expectInline) {
298294
expect(
299-
normalize(webpackOptions.entry[0]).indexOf(
300-
'client/default/index.js?'
301-
) !== -1
295+
normalize(webpackOptions.entry[0]).indexOf('client/index.js?') !== -1
302296
).toBeTruthy();
303297
}
304298

@@ -330,9 +324,7 @@ describe('addEntries util', () => {
330324

331325
if (expectInline) {
332326
expect(
333-
normalize(webpackOptions.entry[0]).indexOf(
334-
'client/default/index.js?'
335-
) !== -1
327+
normalize(webpackOptions.entry[0]).indexOf('client/index.js?') !== -1
336328
).toBeTruthy();
337329
}
338330

@@ -388,9 +380,7 @@ describe('addEntries util', () => {
388380
expect(webWebpackOptions.entry.length).toEqual(2);
389381

390382
expect(
391-
normalize(webWebpackOptions.entry[0]).indexOf(
392-
'client/default/index.js?'
393-
) !== -1
383+
normalize(webWebpackOptions.entry[0]).indexOf('client/index.js?') !== -1
394384
).toBeTruthy();
395385

396386
expect(normalize(webWebpackOptions.entry[1])).toEqual('./foo.js');

0 commit comments

Comments
 (0)