hot-reload
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -9,59 +9,29 @@ // third-party dependencies | ||
var simpleRegExpReplacements = { | ||
"*": ".*?", | ||
"?": ".?" | ||
}; | ||
var simpleRegExpTest = /[\?\*]/; | ||
function isSimpleRegExp(str) { | ||
return simpleRegExpTest.test(str); | ||
function ModuleFilter(moduleUri) { | ||
this._moduleUri = moduleUri; | ||
} | ||
function escapeRegExpStr(str) { | ||
return str.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); | ||
ModuleFilter.prototype.test = function(path) { | ||
return this._moduleUri === path; | ||
} | ||
function createSimpleRegExp(str) { | ||
var _this = this; | ||
return new RegExp("^" + str.replace(/[\*\?]|[^\*\?]*/g, function(match) { | ||
return simpleRegExpReplacements[match] || escapeRegExpStr(match); | ||
}) + "$"); | ||
function ModulePathFilters() { | ||
pathFilters.PathFilters.call(this); | ||
} | ||
function createSimpleRegExpFilter(str, matchResult) { | ||
var simpleRegExp = createSimpleRegExp(str); | ||
ModulePathFilters.prototype.createSimpleFilter = function(filter, recursive, matchResult) { | ||
return function(str) { | ||
//console.error('str ', str, simpleRegExp); | ||
return simpleRegExp.test(str) ? matchResult || true : false; | ||
} | ||
} | ||
function getMatch(filters, path) { | ||
for (var i=0, len=filters.length; i<len; i++) { | ||
var result = filters[i](path); | ||
if (result !== false) { | ||
return result; | ||
if (recursive !== false) { | ||
try { | ||
var moduleUri = this._require.resolve(filter); | ||
return new ModuleFilter(moduleUri) | ||
} catch(e) { | ||
// ignore and fall through | ||
} | ||
} | ||
return undefined; | ||
} | ||
function getMatches(filters, path) { | ||
var matches = []; | ||
for (var i=0, len=filters.length; i<len; i++) { | ||
var result = filters[i](path); | ||
if (result !== false) { | ||
matches.push(result); | ||
} | ||
} | ||
return matches; | ||
return pathFilters.PathFilters.prototype.createSimpleFilter.apply(this, arguments); | ||
} | ||
function hasMatch(filters, path) { | ||
return getMatch(filters, path) !== undefined ? true : false; | ||
} | ||
util.inherits(ModulePathFilters, pathFilters.PathFilters); | ||
@@ -72,11 +42,10 @@ function HotReloader(require) { | ||
this._require = require; | ||
this._uncacheIncludes = []; | ||
this._uncacheExcludes = []; | ||
this._uncacheIncludes = new ModulePathFilters(); | ||
this._uncacheExcludes = new ModulePathFilters(); | ||
this._watchIncludes = {}; | ||
this._watchExcludeFilters = []; | ||
this._watchExcludeFilters = pathFilters.create(); | ||
this._reloadIncludes = []; | ||
this._reloadExcludes = []; | ||
this._reloadIncludes = pathFilters.create(); | ||
this._reloadExcludes = pathFilters.create(); | ||
this._specialReloadIncludes = pathFilters.create(); | ||
@@ -97,66 +66,19 @@ this._specialReloadExcludes = pathFilters.create(); | ||
HotReloader.prototype._createFilterFunc = function(filter) { | ||
} | ||
HotReloader.prototype._addModuleFilters = function(target, args) { | ||
for (var i=0, len=args.length; i<len; i++) { | ||
var arg = args[i]; | ||
if (Array.isArray(arg)) { | ||
this._addModuleFilters(target, arg); | ||
return; | ||
} | ||
else { | ||
var filter = arg; | ||
var filterFunc; | ||
if (typeof filter === 'string') { | ||
if (isSimpleRegExp(filter)) { | ||
filterFunc = createSimpleRegExpFilter(filter); | ||
} | ||
else { | ||
var moduleUri = this._require.resolve(filter); | ||
filterFunc = function(input) { | ||
return moduleUri === input; | ||
} | ||
} | ||
} | ||
else if (filter.constructor === RegExp) { | ||
filterFunc = function(testModule) { | ||
return testModule.test(filter); | ||
} | ||
} | ||
else if (typeof filter === 'function') { | ||
filterFunc = filter; | ||
} | ||
else { | ||
throw new Error("Invalid module filter: " + filter); | ||
} | ||
target.push(filterFunc); | ||
} | ||
} | ||
} | ||
HotReloader.prototype.uncache = function(filter) { | ||
this._addModuleFilters(this._uncacheIncludes, arguments); | ||
HotReloader.prototype.uncache = function(filter, recursive, matchResult) { | ||
this._uncacheIncludes.add(filter, recursive, matchResult); | ||
return this; | ||
} | ||
HotReloader.prototype.uncacheExclude = function(filter) { | ||
this._addModuleFilters(this._uncacheExcludes, arguments); | ||
HotReloader.prototype.uncacheExclude = function(filter, recursive, matchResult) { | ||
this._uncacheExcludes.add(filter, recursive, matchResult); | ||
return this; | ||
} | ||
HotReloader.prototype.reload = function(filter) { | ||
this._addModuleFilters(this._reloadIncludes, arguments); | ||
HotReloader.prototype.reload = function(filter, recursive, matchResult) { | ||
this._reloadIncludes.add(filter, recursive, matchResult); | ||
return this; | ||
} | ||
HotReloader.prototype.reloadExclude = function(filter) { | ||
this._addModuleFilters(this._reloadExcludes, arguments); | ||
HotReloader.prototype.reloadExclude = function(filter, recursive, matchResult) { | ||
this._reloadExcludes.add(filter, recursive, matchResult); | ||
return this; | ||
@@ -170,3 +92,6 @@ } | ||
function callback(path, eventArgs) { | ||
watchIncludes[path] = {path: path, stat: eventArgs.stat}; | ||
watchIncludes[path] = { | ||
path: path, | ||
stat: eventArgs.stat | ||
}; | ||
} | ||
@@ -181,4 +106,4 @@ | ||
.onError(function(e) { | ||
console.error('Directory walk error: ', e); | ||
}) | ||
console.error('Directory walk error: ', e); | ||
}) | ||
.onComplete(this._handleComplete) | ||
@@ -200,3 +125,11 @@ .walk(dir); | ||
} | ||
this._specialReloadIncludes.add(filter, recursive, handlerFunc); | ||
var result = this._specialReloadIncludes.add(filter, recursive); | ||
if (Array.isArray(result)) { | ||
for (var i = 0; i < result.length; i++) { | ||
result[i].handler = handlerFunc; | ||
} | ||
} else { | ||
result.handler = handlerFunc; | ||
} | ||
return this; | ||
@@ -222,15 +155,15 @@ } | ||
HotReloader.prototype._shouldUncacheModule = function(moduleName) { | ||
if (!this._uncacheIncludes.length && !this._uncacheExcludes.length) { | ||
if (this._uncacheIncludes.isEmpty() && this._uncacheExcludes.isEmpty()) { | ||
return true; | ||
} | ||
if (hasMatch(this._uncacheExcludes, moduleName)) { | ||
if (_uncacheExcludes.hasMatch(moduleName)) { | ||
return false; | ||
} | ||
if (this._uncacheExcludes.length && !this._uncacheIncludes.length) { | ||
if (!this._uncacheExcludes.isEmpty() && this._uncacheIncludes.isEmpty()) { | ||
return true; | ||
} | ||
if (hasMatch(this._uncacheIncludes, moduleName)) { | ||
if (this._uncacheIncludes.hasMatch(moduleName)) { | ||
return true; | ||
@@ -243,15 +176,15 @@ } | ||
HotReloader.prototype._shouldReloadModule = function(moduleName) { | ||
if (!this._reloadIncludes.length && !this._reloadExcludes.length) { | ||
if (this._reloadIncludes.isEmpty() && this._reloadExcludes.isEmpty()) { | ||
return false; | ||
} | ||
if (hasMatch(this._reloadExcludes, moduleName)) { | ||
if (_reloadExcludes.hasMatch(moduleName)) { | ||
return false; | ||
} | ||
if (this._reloadExcludes.length && !this._reloadIncludes.length) { | ||
if (!this._reloadExcludes.isEmpty() && this._reloadIncludes.isEmpty()) { | ||
return true; | ||
} | ||
if (hasMatch(this._reloadIncludes, moduleName)) { | ||
if (this._reloadIncludes.hasMatch(moduleName)) { | ||
return true; | ||
@@ -273,7 +206,9 @@ } | ||
if (!hasMatch(this._specialReloadExcludes, path)) { | ||
specialReloadHandlers = getMatches(this._specialReloadIncludes, path); | ||
if (!this._specialReloadExcludes.hasMatch(path)) { | ||
specialReloadHandlers = this._specialReloadIncludes.getMatches(path); | ||
} | ||
} | ||
var eventArgs = {path: path}; | ||
var eventArgs = { | ||
path: path | ||
}; | ||
var i; | ||
@@ -284,4 +219,4 @@ | ||
for (i=0; i<specialReloadHandlers.length; i++) { | ||
var specialReloadHandler = specialReloadHandlers[i]; | ||
for (i = 0; i < specialReloadHandlers.length; i++) { | ||
var specialReloadHandler = specialReloadHandlers[i].handler; | ||
var result = specialReloadHandler(path); | ||
@@ -292,6 +227,5 @@ if (result === false) { | ||
} | ||
this.emit('afterSpecialReload', eventArgs); | ||
} | ||
else { | ||
} else { | ||
this.emit('beforeReload', eventArgs); | ||
@@ -310,3 +244,3 @@ | ||
delete require.cache[key]; | ||
console.log('[hot-reload] Uncached module: ' + module.filename); | ||
@@ -319,4 +253,4 @@ | ||
} | ||
} else { | ||
@@ -340,3 +274,3 @@ //console.log('[hot-reload] Not uncaching ' + module.filename); | ||
HotReloader.prototype._reloadModule = function(module) { | ||
console.log('[hot-reload] Reloading module "' + module.filename + '"...'); | ||
@@ -347,3 +281,3 @@ | ||
delete require.cache[module.filename]; | ||
try { | ||
@@ -361,8 +295,7 @@ var newModule = require(module.filename); | ||
console.log('[hot-reload] Reloaded module: ' + module.filename); | ||
} | ||
catch(e) { | ||
} catch (e) { | ||
console.error('[hot-reload] ERROR: Unable to reload module "' + module.filename + '". Exception: ' + e, e.stack); | ||
} | ||
this.emit('afterModuleReload', module); | ||
@@ -373,4 +306,4 @@ } | ||
var watchIncludes = this._watchIncludes, | ||
@@ -387,3 +320,3 @@ watchExcludes = this._watchExcludes, | ||
if (hasMatch(_this._watchExcludeFilters, path)) { | ||
if (_this._watchExcludeFilters.hasMatch(path)) { | ||
console.log('[hot-reload] Modified file ignored since it is excluded: ' + path + ' '); | ||
@@ -405,8 +338,6 @@ // The file excluded from being watched so ignore the event | ||
handleModified(event, watchInclude.path); | ||
} | ||
else { | ||
} else { | ||
handleModified(event, require('path').join(watchInclude.path, filename)); | ||
} | ||
} | ||
else { | ||
} else { | ||
handleModified(event, watchInclude.path); | ||
@@ -419,3 +350,3 @@ } | ||
if (watchIncludes.hasOwnProperty(path)) { | ||
if (hasMatch(_this._watchExcludeFilters, path)) { | ||
if (this._watchExcludeFilters.hasMatch(path)) { | ||
console.log('[hot-reload] Not watching "' + path + '" since it is excluded.'); | ||
@@ -427,4 +358,4 @@ // The path is excluded from being watched...skip it | ||
var watcher = fs.watch( | ||
fs.realpathSync(path), | ||
createWatcherFunc(watchInclude)); | ||
fs.realpathSync(path), | ||
createWatcherFunc(watchInclude)); | ||
@@ -440,7 +371,6 @@ console.log('[hot-reload] Watching ' + (watchInclude.stat.isDirectory() ? 'directory' : 'file') + ': ' + watchInclude.path); | ||
this.on('ready', startWatching); | ||
} | ||
else { | ||
} else { | ||
startWatching(); | ||
} | ||
return this; | ||
@@ -459,3 +389,2 @@ } | ||
exports.HotReloader = HotReloader; | ||
exports.HotReloader = HotReloader; |
{ | ||
"name": "hot-reload", | ||
"description": "Triggers reloading of Node.js modules", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"homepage": "https://github.com/philidem/node-hot-reload", | ||
@@ -6,0 +6,0 @@ "authors": [ |
46079
278