Skip to content

Commit 2193385

Browse files
committed
fix(server): js文件入口不存在时,依然会等待其他资源编译结束resolve而不是直接返回404,这时候会变成一个永远pending的请求。
1 parent 84a347b commit 2193385

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

lib/commands/server.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ exports.run = function (options) {
200200
entryPath = UtilPath.normalize(entryPath.replace(cssReg, '.css'));
201201

202202
// 如果是 ykit 处理过的样式文件,将其变为正常的请求路径(../.ykit_cache/main/index.css.js => main/index.css)
203-
204203
if (entryPath.indexOf('.css.js') && entryPath.indexOf('.ykit_cache/') > 1) {
205204
entryPath = entryPath.split('.ykit_cache/')[1].replace('.css.js', '.css');
206205
}
@@ -226,19 +225,27 @@ exports.run = function (options) {
226225

227226
// 如果没找到该资源,在整个编译过程结束后再返回
228227
if (Object.keys(nextConfig.entry).length === 0) {
228+
// 如果是js入口没找到,那肯定是出错了,这时候应该直接next让后面报错
229+
if (req.url.match(/\.js$/)) {
230+
next();
231+
}
229232
setTimeout(function () {
230233
promiseCache[projectName] ? Promise.all(promiseCache[projectName]).then(function () {
231234
// 统一去掉版本号
232235
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
233236
next();
237+
}).catch(function (err) {
238+
throw err;
234239
}) : null;
235240
}, 1000);
236241
} else {
237242
(function () {
238243
// 生成该请求的 promiseCache
239244
var resolve = null;
240-
var requestPromise = new Promise(function (res) {
245+
var reject = null;
246+
var requestPromise = new Promise(function (res, rej) {
241247
resolve = res;
248+
reject = rej;
242249
});
243250

244251
if (!promiseCache[projectName]) {
@@ -249,7 +256,7 @@ exports.run = function (options) {
249256

250257
compiler.watch({}, function (err) {
251258
if (err) {
252-
error(err);
259+
reject(err);
253260
} else {
254261
middlewareCache[cacheId] = req.url;
255262
next();
@@ -375,7 +382,7 @@ exports.run = function (options) {
375382

376383
server.on('error', function (e) {
377384
if (e.code === 'EACCES') {
378-
warn('权限不足, 请使用sudo执行');
385+
warn('权限不足, 请使用sudo/管理员模式执行');
379386
} else if (e.code === 'EADDRINUSE') {
380387
warn('端口 ' + port + ' 已经被占用, 请关闭占用该端口的程序或者使用其它端口.');
381388
}

lib/models/Project.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,15 @@ var Project = function () {
155155
var userConfigObj = configMethod.config.call(userConfig, options, this.cwd);
156156

157157
if (userConfigObj) {
158+
var exports = null;
158159
if (Array.isArray(userConfigObj.export)) {
159-
userConfigObj.export = userConfigObj.export.filter(function (item) {
160+
exports = userConfigObj.export;
161+
} else if (Array.isArray(userConfigObj.exports)) {
162+
exports = userConfigObj.exports;
163+
}
164+
165+
if (exports) {
166+
exports = exports.filter(function (item) {
160167
if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object') {
161168
_this.config.setGroupExports(item.name, item.export);
162169
return false;
@@ -165,7 +172,7 @@ var Project = function () {
165172
}
166173
});
167174
}
168-
this.config.setExports(userConfigObj.export);
175+
this.config.setExports(exports);
169176
this.config.setCompiler(userConfigObj.modifyWebpackConfig);
170177
this.config.setSync(userConfigObj.sync);
171178
this.setCommands(userConfigObj.command);

src/commands/server.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,14 @@ exports.run = (options) => {
195195
entryPath = UtilPath.normalize(entryPath.replace(cssReg, '.css'));
196196

197197
// 如果是 ykit 处理过的样式文件,将其变为正常的请求路径(../.ykit_cache/main/index.css.js => main/index.css)
198-
199-
if(entryPath.indexOf('.css.js') && entryPath.indexOf('.ykit_cache/') > 1) {
198+
if (entryPath.indexOf('.css.js') && entryPath.indexOf('.ykit_cache/') > 1) {
200199
entryPath = entryPath.split('.ykit_cache/')[1].replace('.css.js', '.css');
201200
}
202201

203202
// 判断所请求的资源是否在入口配置中
204-
if(sysPath.normalize(entryPath) === sysPath.normalize(requestUrl)) {
203+
if (sysPath.normalize(entryPath) === sysPath.normalize(requestUrl)) {
205204
isRequestingEntry = true;
206-
} else if(sysPath.normalize(entryPath) === sysPath.normalize(requestUrlNoVer)) {
205+
} else if (sysPath.normalize(entryPath) === sysPath.normalize(requestUrlNoVer)) {
207206
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
208207
isRequestingEntry = true;
209208
}
@@ -222,19 +221,29 @@ exports.run = (options) => {
222221
});
223222

224223
// 如果没找到该资源,在整个编译过程结束后再返回
225-
if(Object.keys(nextConfig.entry).length === 0) {
224+
if (Object.keys(nextConfig.entry).length === 0) {
225+
// 如果是js入口没找到,那肯定是出错了,这时候应该直接next让后面报错
226+
if (req.url.match(/\.js$/)) {
227+
next();
228+
}
226229
setTimeout(() => {
227-
promiseCache[projectName] ? Promise.all(promiseCache[projectName]).then(() => {
228-
// 统一去掉版本号
229-
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
230-
next();
231-
}) : null;
230+
promiseCache[projectName] ?
231+
Promise.all(promiseCache[projectName]).then(() => {
232+
// 统一去掉版本号
233+
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
234+
next();
235+
}).catch((err) => {
236+
throw err;
237+
}) :
238+
null;
232239
}, 1000);
233240
} else {
234241
// 生成该请求的 promiseCache
235242
let resolve = null;
236-
const requestPromise = new Promise((res) => {
243+
let reject = null;
244+
const requestPromise = new Promise((res, rej) => {
237245
resolve = res;
246+
reject = rej;
238247
});
239248

240249
if (!promiseCache[projectName]) {
@@ -244,8 +253,8 @@ exports.run = (options) => {
244253
}
245254

246255
compiler.watch({}, (err) => {
247-
if(err) {
248-
error(err);
256+
if (err) {
257+
reject(err);
249258
} else {
250259
middlewareCache[cacheId] = req.url;
251260
next();
@@ -309,7 +318,7 @@ exports.run = (options) => {
309318
compiler.watch({}, (err) => {
310319
creatingCompiler = false;
311320

312-
if(err) {
321+
if (err) {
313322
error(err);
314323
} else {
315324
middlewareCache[projectName] = projectName;

src/models/Project.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ class Project {
139139
const userConfigObj = configMethod.config.call(userConfig, options, this.cwd);
140140

141141
if (userConfigObj) {
142+
let exports = null;
142143
if (Array.isArray(userConfigObj.export)) {
143-
userConfigObj.export = userConfigObj.export.filter((item) => {
144+
exports = userConfigObj.export;
145+
} else if (Array.isArray(userConfigObj.exports)) {
146+
exports = userConfigObj.exports;
147+
}
148+
149+
if (exports) {
150+
exports = exports.filter((item) => {
144151
if (typeof item === 'object') {
145152
this.config.setGroupExports(item.name, item.export);
146153
return false;
@@ -149,7 +156,7 @@ class Project {
149156
}
150157
});
151158
}
152-
this.config.setExports(userConfigObj.export);
159+
this.config.setExports(exports);
153160
this.config.setCompiler(userConfigObj.modifyWebpackConfig);
154161
this.config.setSync(userConfigObj.sync);
155162
this.setCommands(userConfigObj.command);

0 commit comments

Comments
 (0)