mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
fix can not reload when base file updated
This commit is contained in:
parent
eeaba5eb50
commit
eecb792aab
87
src/index.js
87
src/index.js
@ -194,6 +194,9 @@ export default class {
|
||||
* @return {} []
|
||||
*/
|
||||
checkModuleConfig(){
|
||||
if(think.mode !== think.mode_module){
|
||||
return;
|
||||
}
|
||||
// check module config
|
||||
// some config must be set in common module
|
||||
let keys = [], errorKey = 'error_config_key';
|
||||
@ -225,7 +228,7 @@ export default class {
|
||||
let modules = this.getModule();
|
||||
//load modules config
|
||||
modules.forEach(module => {
|
||||
if(think.mode === think.mode_module && module !== 'common'){
|
||||
if(module !== 'common'){
|
||||
checkModuleConfig(module);
|
||||
}
|
||||
});
|
||||
@ -394,83 +397,11 @@ export default class {
|
||||
* @return {} []
|
||||
*/
|
||||
autoReload(){
|
||||
let autoReload = thinkCache(thinkCache.AUTO_RELOAD);
|
||||
//clear file cache
|
||||
let clearFileCache = file => {
|
||||
let mod = require.cache[file];
|
||||
//remove from parent module
|
||||
if(mod && mod.parent){
|
||||
mod.parent.children.splice(mod.parent.children.indexOf(mod), 1);
|
||||
}
|
||||
//remove children
|
||||
if(mod && mod.children){
|
||||
mod.children.length = 0;
|
||||
}
|
||||
//remove require cache
|
||||
require.cache[file] = null;
|
||||
};
|
||||
/**
|
||||
* log reload file
|
||||
* @param {String} file []
|
||||
* @return {} []
|
||||
*/
|
||||
let log = file => {
|
||||
if(!think.config('log_auto_reload') || !file){
|
||||
return;
|
||||
}
|
||||
//only log app files changed
|
||||
if(file.indexOf(think.APP_PATH) === 0){
|
||||
file = file.slice(think.APP_PATH.length);
|
||||
think.log(`reload file ${file}`, 'RELOAD');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* check change form cache
|
||||
* @return {} []
|
||||
*/
|
||||
let checkCacheChange = () => {
|
||||
let hasChange = false;
|
||||
for(let file in require.cache){
|
||||
if(!think.isFile(file)){
|
||||
clearFileCache(file);
|
||||
continue;
|
||||
}
|
||||
let mTime = fs.statSync(file).mtime.getTime();
|
||||
if(!autoReload[file]){
|
||||
autoReload[file] = mTime;
|
||||
continue;
|
||||
}
|
||||
if(mTime > autoReload[file]){
|
||||
clearFileCache(file);
|
||||
autoReload[file] = mTime;
|
||||
hasChange = true;
|
||||
log(file);
|
||||
}
|
||||
}
|
||||
return hasChange;
|
||||
};
|
||||
/**
|
||||
* check change from file
|
||||
* @return {} []
|
||||
*/
|
||||
let prevFilesLength = 0;
|
||||
let checkFileChange = () => {
|
||||
let nowFilesLength = think.getFiles(think.APP_PATH).filter(file => {
|
||||
let extname = path.extname(file);
|
||||
return extname === '.js';
|
||||
}).length;
|
||||
let flag = prevFilesLength === nowFilesLength;
|
||||
prevFilesLength = nowFilesLength;
|
||||
return flag;
|
||||
};
|
||||
|
||||
setInterval(() => {
|
||||
let hasChange = checkCacheChange() || checkFileChange();
|
||||
if(hasChange){
|
||||
this.load();
|
||||
}
|
||||
}, 200);
|
||||
let AutoReload = require('./util/auto_reload.js');
|
||||
let instance = new AutoReload(think.APP_PATH, () => {
|
||||
this.load();
|
||||
}, think.config('log_auto_reload'));
|
||||
instance.run();
|
||||
}
|
||||
/**
|
||||
* capture error
|
||||
|
||||
117
src/util/auto_reload.js
Normal file
117
src/util/auto_reload.js
Normal file
@ -0,0 +1,117 @@
|
||||
'use strict';
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* auto reload file
|
||||
*/
|
||||
export default class extends think.base {
|
||||
/**
|
||||
* init
|
||||
* @param {String} srcPath [source path]
|
||||
* @param {Function} callback [when file has changed, callback will be invoke]
|
||||
* @param {Boolean} log [log reload file]
|
||||
* @return {} []
|
||||
*/
|
||||
init(srcPath, callback, showLog){
|
||||
this.srcPath = srcPath;
|
||||
this.callback = callback;
|
||||
this.showLog = showLog;
|
||||
this.prevFilesCount = 0;
|
||||
}
|
||||
/**
|
||||
* log file
|
||||
* @param {String} file []
|
||||
* @return {} []
|
||||
*/
|
||||
log(file){
|
||||
if(!this.showLog || !file){
|
||||
return;
|
||||
}
|
||||
//only log app files changed
|
||||
if(file.indexOf(think.srcPath) === 0){
|
||||
file = file.slice(think.srcPath.length);
|
||||
think.log(`reload file ${file}`, 'RELOAD');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* clear file cache, also clear dependents file cache
|
||||
* @return {} []
|
||||
*/
|
||||
clearFileCache(file){
|
||||
|
||||
let mod = require.cache[file];
|
||||
//remove children
|
||||
if(mod && mod.children){
|
||||
mod.children.length = 0;
|
||||
}
|
||||
//remove require cache
|
||||
delete require.cache[file];
|
||||
|
||||
// clear module cache which dependents this module
|
||||
for(let fileItem in require.cache){
|
||||
let item = require.cache[fileItem];
|
||||
if(item && item.children && item.children.indexOf(mod)){
|
||||
this.clearFileCache(fileItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* check file change
|
||||
* compare files count
|
||||
* @return {} []
|
||||
*/
|
||||
checkFileChange(){
|
||||
let filesCount = think.getFiles(this.srcPath).filter(file => {
|
||||
let extname = path.extname(file);
|
||||
return extname === '.js';
|
||||
}).length;
|
||||
let flag = this.prevFilesCount && this.prevFilesCount !== filesCount;
|
||||
this.prevFilesCount = filesCount;
|
||||
return flag;
|
||||
}
|
||||
/**
|
||||
* check cache change
|
||||
* @return {} []
|
||||
*/
|
||||
checkCacheChange(){
|
||||
let autoReload = thinkCache(thinkCache.AUTO_RELOAD);
|
||||
let hasChange = false;
|
||||
let nodeModules = `${path.sep}node_modules${path.sep}`;
|
||||
for(let file in require.cache){
|
||||
//ignore file in node_modules path
|
||||
if(file.indexOf(nodeModules) > -1){
|
||||
continue;
|
||||
}
|
||||
if(!think.isFile(file)){
|
||||
this.clearFileCache(file);
|
||||
continue;
|
||||
}
|
||||
let mTime = fs.statSync(file).mtime.getTime();
|
||||
if(!autoReload[file]){
|
||||
autoReload[file] = mTime;
|
||||
continue;
|
||||
}
|
||||
if(mTime > autoReload[file]){
|
||||
this.clearFileCache(file);
|
||||
autoReload[file] = mTime;
|
||||
hasChange = true;
|
||||
this.log(file);
|
||||
}
|
||||
}
|
||||
return hasChange;
|
||||
}
|
||||
/**
|
||||
* run
|
||||
* @return {} []
|
||||
*/
|
||||
run(){
|
||||
this.timer = setInterval(() => {
|
||||
let hasChange = this.checkCacheChange() || this.checkFileChange();
|
||||
if(hasChange && this.callback){
|
||||
this.callback();
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
}
|
||||
@ -162,16 +162,16 @@ describe('index.js', function(){
|
||||
muk.restore();
|
||||
assert.equal(flag, true);
|
||||
})
|
||||
it('autoReload', function(){
|
||||
var instance = new thinkjs({
|
||||
APP_PATH: __dirname + '/testApp',
|
||||
ROOT_PATH: __dirname
|
||||
});
|
||||
muk(global, 'setInterval', function(fn, time){
|
||||
fn();
|
||||
})
|
||||
instance.autoReload();
|
||||
})
|
||||
// it('autoReload', function(){
|
||||
// var instance = new thinkjs({
|
||||
// APP_PATH: __dirname + '/testApp',
|
||||
// ROOT_PATH: __dirname
|
||||
// });
|
||||
// muk(global, 'setInterval', function(fn, time){
|
||||
// fn();
|
||||
// })
|
||||
// instance.autoReload();
|
||||
// })
|
||||
// it('checkAppPath, empty', function(){
|
||||
// var instance = new thinkjs({
|
||||
// ROOT_PATH: __dirname,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user