Skip to content

Commit 751c1a6

Browse files
committed
fix(server): 修复请求资源中带query引起的重复编译
1 parent 43e08b2 commit 751c1a6

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

lib/commands/server.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,14 @@ exports.run = function (options) {
133133
if (!isCompilingAll) {
134134
(function () {
135135
// 去掉版本号和打头的"/"
136-
var urlNoVer = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
137-
urlNoVer = urlNoVer[0] === '/' ? urlNoVer.slice(1) : urlNoVer;
136+
var pureSourcePath = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '');
137+
pureSourcePath = pureSourcePath[0] === '/' ? pureSourcePath.slice(1) : pureSourcePath;
138+
139+
// 去掉url query
140+
pureSourcePath = pureSourcePath.replace(/\?[\w=&]+$/, '');
138141

139142
// 从编译cache中取,map文件不必生成重复middleware
140-
var cacheId = sysPath.join(projectName, urlNoVer.replace('.map', ''));
143+
var cacheId = sysPath.join(projectName, pureSourcePath.replace('.map', ''));
141144
var middleware = middlewareCache[cacheId];
142145

143146
if (!middleware) {
@@ -160,7 +163,7 @@ exports.run = function (options) {
160163

161164
var cssReg = new RegExp(config.entryExtNames.css.join('|'));
162165
entryPath = entryPath.replace(cssReg, '.css'); // 将入口的.scss/.less后缀替换为.css
163-
isRequestingEntry = entryPath.indexOf(urlNoVer) > -1;
166+
isRequestingEntry = entryPath.indexOf(pureSourcePath) > -1;
164167

165168
if (isRequestingEntry) {
166169
nextConfig.entry = _defineProperty({}, entryKey, entryItem);
@@ -171,7 +174,7 @@ exports.run = function (options) {
171174

172175
compiler.watch({}, function (err, stats) {
173176
// compiler complete
174-
middleware = middlewareCache[cacheId] = webpackDevMiddleware(compiler, { quiet: true });
177+
middleware = middlewareCache[cacheId] = webpackDevMiddleware(compiler, { quiet: true, clientLogLevel: 'error' });
175178
middleware(req, res, next);
176179
});
177180
// 检测config文件变化

src/commands/server.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,14 @@ exports.run = (options) => {
130130
// 只编译所请求的资源
131131
if(!isCompilingAll) {
132132
// 去掉版本号和打头的"/"
133-
let urlNoVer = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '')
134-
urlNoVer = urlNoVer[0] === '/' ? urlNoVer.slice(1) : urlNoVer
133+
let pureSourcePath = req.url.replace(/@[\d\w]+(?=\.\w+$)/, '')
134+
pureSourcePath = pureSourcePath[0] === '/' ? pureSourcePath.slice(1) : pureSourcePath
135+
136+
// 去掉url query
137+
pureSourcePath = pureSourcePath.replace(/\?[\w=&]+$/, '')
135138

136139
// 从编译cache中取,map文件不必生成重复middleware
137-
const cacheId = sysPath.join(projectName, urlNoVer.replace('.map', ''))
140+
const cacheId = sysPath.join(projectName, pureSourcePath.replace('.map', ''))
138141
let middleware = middlewareCache[cacheId]
139142

140143
if (!middleware) {
@@ -157,7 +160,7 @@ exports.run = (options) => {
157160

158161
const cssReg = new RegExp(config.entryExtNames.css.join('|'))
159162
entryPath = entryPath.replace(cssReg, '.css') // 将入口的.scss/.less后缀替换为.css
160-
isRequestingEntry = entryPath.indexOf(urlNoVer) > -1
163+
isRequestingEntry = entryPath.indexOf(pureSourcePath) > -1
161164

162165
if(isRequestingEntry) {
163166
nextConfig.entry = {
@@ -170,7 +173,7 @@ exports.run = (options) => {
170173

171174
compiler.watch({}, function(err, stats) {
172175
// compiler complete
173-
middleware = middlewareCache[cacheId] = webpackDevMiddleware(compiler, {quiet: true,});
176+
middleware = middlewareCache[cacheId] = webpackDevMiddleware(compiler, {quiet: true, clientLogLevel: 'error'});
174177
middleware(req, res, next);
175178
});
176179
// 检测config文件变化

0 commit comments

Comments
 (0)