Skip to content

Commit b00449a

Browse files
committed
fix(hmr): fix not work on differert host
1 parent 0ab9b76 commit b00449a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/commands/server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ exports.run = function (options) {
246246
}
247247

248248
// hot reload
249-
if (hot) {
249+
var hotEnabled = project.server && project.server.hot || hot;
250+
if (hotEnabled) {
250251
// 修改 publicPath 为当前服务
251252
var localPublicPath = wpConfig.output.local.publicPath;
252253
if (project.config.replacingPublicPath !== false) {
@@ -261,7 +262,7 @@ exports.run = function (options) {
261262
} else {
262263
// hot 且 未指定 publicPath 需要手动设置方式 hot.json 404
263264
var relativePath = sysPath.relative(projectCwd, wpConfig.output.local.path);
264-
wpConfig.output.local.publicPath = '/' + projectName + '/' + relativePath + '/';
265+
wpConfig.output.local.publicPath = 'http://127.0.0.1:' + port + '/' + projectName + '/' + relativePath + '/';
265266
}
266267
}
267268

@@ -274,7 +275,8 @@ exports.run = function (options) {
274275
var entryItem = wpConfig.entry[key];
275276
if (sysPath.extname(entryItem[entryItem.length - 1]) === '.js') {
276277
var whmPath = require.resolve('webpack-hot-middleware/client');
277-
entryItem.unshift(whmPath + '?reload=true&path=/__webpack_hmr&timeout=9999999');
278+
var hotPath = 'http://127.0.0.1:' + port + '/__webpack_hmr';
279+
entryItem.unshift(whmPath + '?reload=true&path=' + hotPath + '&timeout=9999999&overlay=false');
278280
}
279281
return entryItem;
280282
});
@@ -450,11 +452,12 @@ exports.run = function (options) {
450452
}
451453
});
452454

453-
if (hot) {
455+
if (hotEnabled) {
454456
app.use(require('webpack-hot-middleware')(compiler, {
455457
log: false,
456458
path: '/__webpack_hmr'
457459
}));
460+
458461
logInfo('Start hot reloader server.');
459462
}
460463

src/commands/server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ exports.run = (options) => {
242242
}
243243

244244
// hot reload
245-
if(hot) {
245+
const hotEnabled = (project.server && project.server.hot) || hot;
246+
if(hotEnabled) {
246247
// 修改 publicPath 为当前服务
247248
let localPublicPath = wpConfig.output.local.publicPath;
248249
if(project.config.replacingPublicPath !== false) {
@@ -257,7 +258,7 @@ exports.run = (options) => {
257258
} else {
258259
// hot 且 未指定 publicPath 需要手动设置方式 hot.json 404
259260
const relativePath = sysPath.relative(projectCwd, wpConfig.output.local.path);
260-
wpConfig.output.local.publicPath = `/${projectName}/${relativePath}/`;
261+
wpConfig.output.local.publicPath = `http://127.0.0.1:${port}/${projectName}/${relativePath}/`;
261262
}
262263
}
263264

@@ -270,7 +271,8 @@ exports.run = (options) => {
270271
let entryItem = wpConfig.entry[key];
271272
if(sysPath.extname(entryItem[entryItem.length - 1]) === '.js') {
272273
const whmPath = require.resolve('webpack-hot-middleware/client');
273-
entryItem.unshift(whmPath + '?reload=true&path=/__webpack_hmr&timeout=9999999');
274+
const hotPath = `http://127.0.0.1:${port}/__webpack_hmr`;
275+
entryItem.unshift(whmPath + '?reload=true&path=' + hotPath + '&timeout=9999999&overlay=false');
274276
}
275277
return entryItem;
276278
});
@@ -450,11 +452,12 @@ exports.run = (options) => {
450452
}
451453
);
452454

453-
if(hot) {
455+
if(hotEnabled) {
454456
app.use(require('webpack-hot-middleware')(compiler, {
455457
log: false,
456458
path: '/__webpack_hmr'
457459
}));
460+
458461
logInfo('Start hot reloader server.');
459462
}
460463

0 commit comments

Comments
 (0)