Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
refine dev level
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed May 21, 2016
1 parent f970fbf commit d485f92
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
61 changes: 43 additions & 18 deletions js.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$js - asynchronous module definition framework
or just simple lightweight javascript dependencies manager
@version 4.8
@version 5.0
@link http://github.com/redcatphp/js/
@author Jo Surikat <[email protected]>
@website http://redcatphp.com
Expand Down Expand Up @@ -32,19 +32,35 @@
return true;
};
var ts = (new Date().getTime()).toString();
var cacheFix = function(fileName,dev,min,ext,cdn){
if(dev||min){
var relative = fileName.indexOf('//')<0||(cdn&&fileName.indexOf(cdn)===0);
if(min){
if(relative&&fileName.indexOf('.min.'+ext)<0&&fileName.indexOf('.'+ext)){
var p = fileName.lastIndexOf('.'+ext);
if(p>-1)
fileName = fileName.substr(0,p)+'.min'+fileName.substr(p);
}
var devOverride = function(u,ext){
var ref, dev;
if(ext=='js') ref = $js;
else ref = $css;
dev = ref.dev;
if(dev){
if(ref.inProdFiles.indexOf(u)!==-1){
dev = false;
}
else{
if(relative&&fileName.indexOf('_t=')<0)
fileName += (fileName.indexOf('?')<0?'?':'&')+'_t='+ts;
}
else{
if(ref.inDevFiles.indexOf(u)!==-1){
dev = true;
}
}
return dev;
};
var cacheFix = function(fileName,dev,min,ext,cdn){
if(!min&&!dev) return fileName;
if(!fileName.indexOf('//')<0||(cdn&&fileName.indexOf(cdn)===0)) return; //relative
if(dev){
if(fileName.indexOf('_t=')<0)
fileName += (fileName.indexOf('?')<0?'?':'&')+'_t='+ts;
}
else if(min){
if(fileName.indexOf('.min.'+ext)<0&&fileName.indexOf('.'+ext)){
var p = fileName.lastIndexOf('.'+ext);
if(p>-1)
fileName = fileName.substr(0,p)+'.min'+fileName.substr(p);
}
}
return fileName;
Expand Down Expand Up @@ -96,15 +112,15 @@
};
return callback;
};
var createScript = function(u){
var createScript = function(u,dev){
var callback = makeSrcCallback(u);
var s = d.createElement('script');
d.type = 'text/javascript';
d.body.appendChild(s);
s.onload = callback;
s.onreadystatechange = function(){if(callback&&this.readyState==='loaded'){callback();}}; //old browsers
s.setAttribute('async','async');
s.src = cacheFix(u,$js.dev,$js.min,'js',$js.cdn);
s.src = cacheFix(u,dev,$js.min,'js',$js.cdn);
};
var resolve = function(u, c){
if(typeof(c)=='function') c();
Expand All @@ -124,10 +140,11 @@
c();
return;
}
var dev = devOverride(u,'js');
u = getSrc(u);
if(!requiring[u]){
requiring[u] = [];
createScript(u);
createScript(u,dev);
}
if(typeof(c)=='function')
requiring[u].push(c);
Expand Down Expand Up @@ -542,7 +559,7 @@
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
}
httpRequest.open('HEAD', cacheFix(url,$js.dev,$js.min,'js',$js.cdn), true);
httpRequest.open('HEAD', cacheFix(url,devOverride(s,'js'),$js.min,'js',$js.cdn), true);
httpRequest.onreadystatechange = function(){
if(httpRequest.readyState==4){
if(httpRequest.status!=404){
Expand Down Expand Up @@ -640,6 +657,8 @@
js.pathSuffix = '.js';
js.min = false;
js.cdn = false;
js.inProdFiles = [];
js.inDevFiles = [];

//methods
js.alias = function(alias,concrete){
Expand Down Expand Up @@ -774,6 +793,10 @@
$js.dependencies(o.dependencies);
if(o.call)
$js(o.call);
if(o.inProdFiles)
$js.inProdFiles = o.inProdFiles;
if(o.inDevFiles)
$js.inDevFiles = o.inDevFiles;
};
js.intercept = function(){
var interceptor = {};
Expand Down Expand Up @@ -827,7 +850,7 @@
style.rel = 'stylesheet';
if(media)
style.media = media;
style.href = cacheFix(fileName,$css.dev,$css.min,'css',$css.cdn);
style.href = cacheFix(fileName,devOverride(s,'css'),$css.min,'css',$css.cdn);
d.getElementsByTagName('head')[0].appendChild(style);
}
}
Expand All @@ -838,6 +861,8 @@
css.pathSuffix = '.css';
css.min = false;
css.cdn = false;
css.inProdFiles = [];
css.inDevFiles = [];
return css;
})();

Expand Down
Loading

0 comments on commit d485f92

Please sign in to comment.