Skip to content

Commit 423d485

Browse files
committed
feat(server): 检测到config文件变化后可以重新生成compiler
1 parent b29d873 commit 423d485

File tree

6 files changed

+53
-39
lines changed

6 files changed

+53
-39
lines changed

lib/commands/server.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
44

55
var connect = require('connect'),
6+
fs = require('fs'),
67
http = require('http'),
78
serveStatic = require('serve-static'),
89
serveIndex = require('serve-index'),
@@ -173,31 +174,32 @@ exports.run = function (options) {
173174
projectCwd = sysPath.join(cwd, projectName),
174175
middleware = middlewareCache[projectName];
175176

177+
var compiler = void 0;
178+
176179
if (!middleware) {
177-
var project = Manager.getProject(projectCwd);
180+
var project = Manager.getProject(projectCwd, { cache: false });
181+
178182
if (project.check()) {
179-
var compiler = project.getServerCompiler();
180-
middleware = middlewareCache[projectName] = webpackDevMiddleware(compiler, {
181-
quiet: true
182-
});
183+
compiler = project.getServerCompiler();
183184

184-
// 输出server运行中 error/warning 信息
185185
compiler.watch({}, function (err, stats) {
186-
var statsInfo = stats.toJson({ errorDetails: false }),
187-
logMethods = {
188-
errors: error,
189-
warnings: warn
190-
};
191-
192-
Object.keys(logMethods).map(function (typeId) {
193-
statsInfo[typeId].map(function (logInfo) {
194-
logMethods[typeId](logInfo);
195-
});
186+
middleware = middlewareCache[projectName] = webpackDevMiddleware(compiler, {
187+
quiet: true
196188
});
197189

190+
// 输出server运行中 error/warning 信息
198191
req.url = '/' + keys.slice(3).join('/').replace(/(\@[\d\w]+)?\.(js|css)/, '.$2');
199192
middleware(req, res, next);
200193
});
194+
195+
// 检测config文件变化
196+
var projectConfigFilePath = sysPath.resolve(project.config._config.cwd, project.configFile);
197+
fs.watch(projectConfigFilePath, function (eventType, filename) {
198+
if (eventType === 'change') {
199+
// middleware.invalidate()
200+
middlewareCache[projectName] = null;
201+
}
202+
});
201203
} else {
202204
next();
203205
return {

lib/models/Project.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var Project = function () {
116116
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
117117
}
118118

119-
var configMethod = require(sysPath.join(this.cwd, this.configFile));
119+
var configMethod = this._requireUncached(sysPath.join(this.cwd, this.configFile));
120120
extend(true, userConfig.eslintConfig, Manager.loadEslintConfig(this.cwd));
121121
extend(true, userConfig.stylelintConfig, Manager.loadStylelintConfig(this.cwd));
122122
this.ignores.push(Manager.loadIgnoreFile(this.cwd));
@@ -382,6 +382,12 @@ var Project = function () {
382382
}
383383
return str;
384384
}
385+
}, {
386+
key: '_requireUncached',
387+
value: function _requireUncached(module) {
388+
delete require.cache[require.resolve(module)];
389+
return require(module);
390+
}
385391
}]);
386392

387393
return Project;

lib/modules/manager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ var Project = require('../models/Project.js'),
8080

8181
var projectCache = {};
8282

83-
exports.getProject = function (cwd) {
84-
if (!projectCache[cwd]) {
83+
exports.getProject = function (cwd, options) {
84+
if (!projectCache[cwd] || !options.cache) {
8585
projectCache[cwd] = new Project(cwd);
8686
}
8787
return projectCache[cwd];

src/commands/server.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
let connect = require('connect'),
4+
fs = require('fs'),
45
http = require('http'),
56
serveStatic = require('serve-static'),
67
serveIndex = require('serve-index'),
@@ -169,31 +170,32 @@ exports.run = (options) => {
169170
projectCwd = sysPath.join(cwd, projectName),
170171
middleware = middlewareCache[projectName];
171172

173+
let compiler
174+
172175
if (!middleware) {
173-
let project = Manager.getProject(projectCwd);
176+
let project = Manager.getProject(projectCwd, {cache: false});
177+
174178
if (project.check()) {
175-
let compiler = project.getServerCompiler();
176-
middleware = middlewareCache[projectName] = webpackDevMiddleware(compiler, {
177-
quiet: true,
178-
});
179+
compiler = project.getServerCompiler();
179180

180-
// 输出server运行中 error/warning 信息
181181
compiler.watch({}, function(err, stats) {
182-
const statsInfo = stats.toJson({errorDetails: false}),
183-
logMethods = {
184-
errors: error,
185-
warnings: warn
186-
};
187-
188-
Object.keys(logMethods).map((typeId) => {
189-
statsInfo[typeId].map((logInfo) => {
190-
logMethods[typeId](logInfo);
191-
});
182+
middleware = middlewareCache[projectName] = webpackDevMiddleware(compiler, {
183+
quiet: true,
192184
});
193185

186+
// 输出server运行中 error/warning 信息
194187
req.url = '/' + keys.slice(3).join('/').replace(/(\@[\d\w]+)?\.(js|css)/, '.$2');
195188
middleware(req, res, next);
196189
});
190+
191+
// 检测config文件变化
192+
const projectConfigFilePath = sysPath.resolve(project.config._config.cwd, project.configFile)
193+
fs.watch(projectConfigFilePath, (eventType, filename) => {
194+
if(eventType === 'change') {
195+
// middleware.invalidate()
196+
middlewareCache[projectName] = null
197+
}
198+
});
197199
} else {
198200
next();
199201
return;
@@ -202,7 +204,6 @@ exports.run = (options) => {
202204
req.url = '/' + keys.slice(3).join('/').replace(/(\@[\d\w]+)?\.(js|css)/, '.$2');
203205
middleware(req, res, next);
204206
}
205-
206207
} else {
207208
next();
208209
}

src/models/Project.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Project {
8989
}
9090
}
9191

92-
let configMethod = require(sysPath.join(this.cwd, this.configFile));
92+
let configMethod = this._requireUncached(sysPath.join(this.cwd, this.configFile));
9393
extend(true, userConfig.eslintConfig, Manager.loadEslintConfig(this.cwd));
9494
extend(true, userConfig.stylelintConfig, Manager.loadStylelintConfig(this.cwd));
9595
this.ignores.push(Manager.loadIgnoreFile(this.cwd));
@@ -345,6 +345,11 @@ class Project {
345345
}
346346
return str;
347347
}
348+
349+
_requireUncached(module){
350+
delete require.cache[require.resolve(module)]
351+
return require(module)
352+
}
348353
}
349354

350355
module.exports = Project;

src/modules/manager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ let Project = require('../models/Project.js'),
8181

8282
let projectCache = {};
8383

84-
exports.getProject = (cwd) => {
85-
if (!projectCache[cwd]) {
84+
exports.getProject = (cwd, options) => {
85+
if (!projectCache[cwd] || !options.cache) {
8686
projectCache[cwd] = new Project(cwd);
8787
}
8888
return projectCache[cwd];

0 commit comments

Comments
 (0)