Skip to content

Commit 00dcb46

Browse files
committed
fix(server.js): 修复url中带query时找不到本地资源的bug。
1 parent d80c3a0 commit 00dcb46

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

lib/commands/server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,16 @@ exports.run = function (options) {
173173
// 处理prd资源
174174
if (keys[2] === 'prd') {
175175
(function () {
176+
var rquery = /\?.+$/;
177+
var rversion = /@[\d\w]+(?=\.\w+$)/;
176178
// 去掉 query & 版本号
177-
var requestUrl = keys.slice(3).join('/').replace(/\?[\w=&]+$/, '').replace('.map', '');
179+
var requestUrl = keys.slice(3).join('/').replace(rquery, '').replace('.map', '');
178180

179181
// 只编译所请求的资源
180182
if (!isCompilingAll) {
181183
(function () {
182184

183-
var requestUrlNoVer = requestUrl.replace(/@[\d\w]+(?=\.\w+$)/, '');
185+
var requestUrlNoVer = requestUrl.replace(rversion, '');
184186

185187
// 从编译 cache 中取,map 文件不必生成重复 compiler TODO
186188
var cacheId = sysPath.join(projectName, requestUrlNoVer);
@@ -226,7 +228,7 @@ exports.run = function (options) {
226228
if (sysPath.normalize(entryPath) === sysPath.normalize(requestUrl)) {
227229
isRequestingEntry = true;
228230
} else if (sysPath.normalize(entryPath) === sysPath.normalize(requestUrlNoVer)) {
229-
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
231+
req.url = req.url.replace(rversion, '');
230232
isRequestingEntry = true;
231233
}
232234

@@ -247,7 +249,7 @@ exports.run = function (options) {
247249
if (promiseCache[projectName]) {
248250
Promise.all(promiseCache[projectName]).then(function () {
249251
// 统一去掉版本号
250-
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
252+
req.url = req.url.replace(rquery, '').replace(rversion, '');
251253
next();
252254
});
253255
} else {
@@ -257,6 +259,7 @@ exports.run = function (options) {
257259
}, 100);
258260
} else {
259261
(function () {
262+
req.url = req.url.replace(rquery, '').replace(rversion, '');
260263
// 生成该请求的 promiseCache
261264
var resolve = null;
262265
var reject = null;

src/commands/server.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ exports.run = (options) => {
5656
app.use((req, res, next) => {
5757
const extName = sysPath.extname(req.url);
5858

59-
if(extName === '.js') {
59+
if (extName === '.js') {
6060
res.setHeader('Content-Type', 'application/javascript');
61-
} else if(extName === '.css') {
61+
} else if (extName === '.css') {
6262
res.setHeader('Content-Type', 'text/css; charset=UTF-8');
6363
}
6464

@@ -168,13 +168,15 @@ exports.run = (options) => {
168168

169169
// 处理prd资源
170170
if (keys[2] === 'prd') {
171+
const rquery = /\?.+$/;
172+
const rversion = /@[\d\w]+(?=\.\w+$)/;
171173
// 去掉 query & 版本号
172-
const requestUrl = keys.slice(3).join('/').replace(/\?[\w=&]+$/, '').replace('.map', '');
174+
const requestUrl = keys.slice(3).join('/').replace(rquery, '').replace('.map', '');
173175

174176
// 只编译所请求的资源
175177
if (!isCompilingAll) {
176178

177-
let requestUrlNoVer = requestUrl.replace(/@[\d\w]+(?=\.\w+$)/, '');
179+
let requestUrlNoVer = requestUrl.replace(rversion, '');
178180

179181
// 从编译 cache 中取,map 文件不必生成重复 compiler TODO
180182
const cacheId = sysPath.join(projectName, requestUrlNoVer);
@@ -219,7 +221,7 @@ exports.run = (options) => {
219221
if (sysPath.normalize(entryPath) === sysPath.normalize(requestUrl)) {
220222
isRequestingEntry = true;
221223
} else if (sysPath.normalize(entryPath) === sysPath.normalize(requestUrlNoVer)) {
222-
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
224+
req.url = req.url.replace(rversion, '');
223225
isRequestingEntry = true;
224226
}
225227

@@ -242,7 +244,7 @@ exports.run = (options) => {
242244
if (promiseCache[projectName]) {
243245
Promise.all(promiseCache[projectName]).then(function () {
244246
// 统一去掉版本号
245-
req.url = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
247+
req.url = req.url.replace(rquery, '').replace(rversion, '');
246248
next();
247249
});
248250
} else {
@@ -251,6 +253,7 @@ exports.run = (options) => {
251253
}
252254
}, 100);
253255
} else {
256+
req.url = req.url.replace(rquery, '').replace(rversion, '');
254257
// 生成该请求的 promiseCache
255258
let resolve = null;
256259
let reject = null;
@@ -369,11 +372,11 @@ exports.run = (options) => {
369372

370373
let servers = [];
371374

372-
servers.push(extend(http.createServer(app), {_port: port}));
375+
servers.push(extend(http.createServer(app), { _port: port }));
373376
if (isHttps) {
374-
const globalConfig = JSON.parse(fs.readFileSync(YKIT_RC, {encoding: 'utf8'}));
377+
const globalConfig = JSON.parse(fs.readFileSync(YKIT_RC, { encoding: 'utf8' }));
375378

376-
if(!globalConfig['https-key'] || !globalConfig['https-crt']) {
379+
if (!globalConfig['https-key'] || !globalConfig['https-crt']) {
377380
warn('缺少 https 证书/秘钥配置,请使用以下命令设置:');
378381
!globalConfig['https-key'] && warn('ykit config set https-key <path-to-your-key>');
379382
!globalConfig['https-crt'] && warn('ykit config set https-crt <path-to-your-crt>');
@@ -384,7 +387,7 @@ exports.run = (options) => {
384387
key: fs.readFileSync(globalConfig['https-key']),
385388
cert: fs.readFileSync(globalConfig['https-crt'])
386389
};
387-
servers.push(extend(https.createServer(httpsOpts, app), {_port: '443', _isHttps: true}));
390+
servers.push(extend(https.createServer(httpsOpts, app), { _port: '443', _isHttps: true }));
388391
}
389392

390393
servers.forEach((server) => {
@@ -398,8 +401,8 @@ exports.run = (options) => {
398401
});
399402
server.listen(server._port, () => {
400403
const serverUrl = (server._isHttps ? 'https' : 'http')
401-
+ '://127.0.0.1:'
402-
+ server._port;
404+
+ '://127.0.0.1:'
405+
+ server._port;
403406

404407
!server._isHttps && log('Starting up server, serving at: ' + options.cwd);
405408
log('Available on: ' + serverUrl.underline);

0 commit comments

Comments
 (0)