Last active
May 3, 2018 07:29
-
-
Save danyg/440f052ad8173f6fd859274718d62000 to your computer and use it in GitHub Desktop.
RequireJS monkey patching and dependency graph utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
'use strict'; | |
var DEBUG_DEPENDENCY_GRAPH = true; | |
var DEPENDENCY_GRAPH = {}; | |
function proxyGetter(where, what, from) { | |
where.__defineGetter__(what, function() { | |
return from[what]; | |
}); | |
} | |
function proxySetter(where, what, from) { | |
where.__defineSetter__(what, function(v) { | |
from[what] = v; | |
}); | |
} | |
function mimicRealAPI(fnc, handler) { | |
for(var prop in fnc) { | |
if(typeof fnc[prop] === 'function') { | |
handler[prop] = fnc[prop]; | |
} else { | |
proxyGetter(handler, prop, fnc); | |
proxySetter(handler, prop, fnc); | |
} | |
} | |
return handler; | |
} | |
function buildRequireDefineMonkeyPatch(reqDef) { | |
return mimicRealAPI(reqDef, function(nameDepsOrHandler, depsOrHandler, handler) { | |
var name; | |
var deps; | |
if(typeof nameDepsOrHandler !== 'function') { | |
if(typeof nameDepsOrHandler === 'string') { | |
name = nameDepsOrHandler; | |
deps = depsOrHandler; | |
} else { | |
deps = nameDepsOrHandler; | |
handler = depsOrHandler; | |
} | |
} | |
if(!!deps && typeof deps !== 'function') { | |
// MUTATION NEEDED, CANNOT USE FUNCTIONAL HERE (like filter) | |
for(var i = 0; i < deps.length; i++) { | |
deps[i] = deps[i].replace('#', '!'); | |
} | |
if(DEBUG_DEPENDENCY_GRAPH) { | |
var err = new Error(); | |
var where = 'UNKNOWN'; | |
try { | |
where = err.stack.split('\n') | |
.splice(2)[0] | |
.trim() | |
.substring(3) | |
.replace(window.location.origin, '') | |
; | |
} catch(e) { | |
} | |
deps.forEach(function(dep) { | |
if(!DEPENDENCY_GRAPH.hasOwnProperty(dep)) { | |
DEPENDENCY_GRAPH[dep] = []; | |
} | |
if(DEPENDENCY_GRAPH[dep].indexOf(where) === -1) { | |
DEPENDENCY_GRAPH[dep].push(where); | |
} | |
}); | |
} | |
} | |
return reqDef.apply(this, arguments); | |
}); | |
} | |
window.define = buildRequireDefineMonkeyPatch(window.define); | |
window.require = buildRequireDefineMonkeyPatch(window.require); | |
window.requirejs = buildRequireDefineMonkeyPatch(window.requirejs); | |
if(DEBUG_DEPENDENCY_GRAPH) { | |
window.DEPENDENCY_GRAPH = DEPENDENCY_GRAPH; | |
window.findDep = function(dep) { | |
var retObj = {}; | |
var r = new RegExp('.*' + dep + '.*'); | |
Object.keys(window.DEPENDENCY_GRAPH) | |
.filter(function(itm) { | |
return r.test(itm); | |
}) | |
.forEach(function(depName) { | |
retObj[depName] = DEPENDENCY_GRAPH[depName]; | |
}) | |
; | |
return retObj; | |
}; | |
window.console.log('%cYou can check the Dependecy Graph, available as window.DEPENDENCY_GRAPH, also search for dependencies by window.findDep(searchStr);', 'background: black; color: orange; font-size: 26px;'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment