Skip to content

Commit 58d1682

Browse files
zhangyuangevilebottnawi
authored andcommitted
feat: add sockPort option (#1792)
1 parent 11bda15 commit 58d1682

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ client
99
coverage
1010
ssl/*.pem
1111
node_modules
12+
.vscode

client-src/default/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,14 @@ if (
221221
) {
222222
protocol = self.location.protocol;
223223
}
224-
225224
const socketUrl = url.format({
226225
protocol,
227226
auth: urlParts.auth,
228227
hostname,
229-
port,
228+
port:
229+
urlParts.path == null || urlParts.path === '/'
230+
? port
231+
: querystring.parse(urlParts.path).sockPort || port,
230232
// If sockPath is provided it'll be passed in via the __resourceQuery as a
231233
// query param so it has to be parsed out of the querystring in order for the
232234
// client to open the socket to the correct location.

lib/options.json

+13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@
6767
"sockPath": {
6868
"type": "string"
6969
},
70+
"sockPort": {
71+
"anyOf": [
72+
{
73+
"type": "number"
74+
},
75+
{
76+
"type": "string"
77+
},
78+
{
79+
"type": "null"
80+
}
81+
]
82+
},
7083
"watchOptions": {
7184
"type": "object"
7285
},

lib/utils/addEntries.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ function addEntries(config, options, server) {
1717

1818
const domain = createDomain(options, app);
1919
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
20+
const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
2021
const entries = [
21-
`${require.resolve('../../client/')}?${domain}${sockPath}`,
22+
`${require.resolve('../../client/')}?${domain}${sockPath}${sockPort}`,
2223
];
23-
2424
if (options.hotOnly) {
2525
entries.push(require.resolve('webpack/hot/only-dev-server'));
2626
} else if (options.hot) {

lib/utils/createConfig.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function createConfig(config, argv, { port }) {
3030
options.socket = argv.socket;
3131
}
3232

33+
if (argv.sockPort) {
34+
options.sockPort = argv.sockPort;
35+
}
36+
3337
if (argv.progress) {
3438
options.progress = argv.progress;
3539
}

test/Client.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,41 @@ describe('Client complex inline script path', () => {
109109
});
110110
});
111111
});
112+
113+
describe('Client complex inline script path with sockPort', () => {
114+
beforeAll((done) => {
115+
const options = {
116+
port: 9000,
117+
host: '0.0.0.0',
118+
inline: true,
119+
watchOptions: {
120+
poll: true,
121+
},
122+
sockPath: '/foo/test/bar/',
123+
sockPort: 8080,
124+
};
125+
helper.startAwaitingCompilation(config, options, done);
126+
});
127+
128+
afterAll(helper.close);
129+
130+
describe('browser client', () => {
131+
jest.setTimeout(30000);
132+
133+
it('uses the correct sockPort', (done) => {
134+
runBrowser().then(({ page, browser }) => {
135+
page
136+
.waitForRequest((requestObj) =>
137+
requestObj.url().match(/foo\/test\/bar/)
138+
)
139+
.then((requestObj) => {
140+
expect(requestObj.url()).toMatch(
141+
/^http:\/\/localhost:8080\/foo\/test\/bar/
142+
);
143+
browser.close().then(done);
144+
});
145+
page.goto('http://localhost:9000/main');
146+
});
147+
});
148+
});
149+
});

0 commit comments

Comments
 (0)