diff --git a/README.md b/README.md index 17cf168..0372d0b 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,25 @@ $script.ready('my-awesome-plugin', function() { }) ``` +### Newly Added Feature - setup-main="" + +You can now add a "setup-main" attribute to the script tag for script.js and load a setup file with all dependencies +appropriately defined based on script.js nuances + +``` js + + + + // in js/test-setup.js + $script(['boo.js, baz.js'], 'stillonit') + $script(['foo.js'], 'almostdone') + $script.ready(['stillonit','almostdone'], function(){ + // all done! + // run more code... + }) + +``` + ### $script.path() Optionally to make working with large projects easier, there is a path variable you can set to set as a base. diff --git a/dist/script.js b/dist/script.js index 3114d21..63237f3 100644 --- a/dist/script.js +++ b/dist/script.js @@ -4,6 +4,10 @@ * (c) Dustin Diaz 2014 | License MIT */ + /*! + * With permissions from @ded to enable this module load css files asynchrnously as well + * $script.js JS dependency manager & CSS loader + */ (function (name, definition) { if (typeof module != 'undefined' && module.exports) module.exports = definition() else if (typeof define == 'function' && define.amd) define(definition) @@ -11,6 +15,10 @@ })('$script', function () { var doc = document , head = doc.getElementsByTagName('head')[0] + , all = doc.getElementsByTagName('script') + , thisfile = all[all.length - 1] + , tpath = thisfile.src + , loaderPath = thisfile.getAttribute('setup-main') , s = 'string' , f = false , push = 'push' @@ -20,8 +28,16 @@ , ids = {} , delay = {} , scripts = {} - , scriptpath + , scriptpath // = tpath.substring(0, tpath.lastIndexOf('/')+1) , urlArgs + , cwdRgx = /^(\.(?=\/)|[^\.\/]+?\.[a-z]{2,4})/i + , tagMap = { + "js":"script", + "css":"link", + "":"undefined" + } + + console.log("s: "+scriptpath); function every(ar, fn) { for (var i = 0, j = ar.length; i < j; ++i) if (!fn(ar[i])) return f @@ -69,20 +85,37 @@ } function create(path, fn) { - var el = doc.createElement('script'), loaded + var isStr = path.toString() === path, + el = path.nodeType && path || isStr && doc.createElement(tagMap[(path.match(/\.(js|css)$/i) || ["",""])[1]]), + type = el.nodeName.toLowerCase(), + loaded; + if(type == "undefined") return; el.onload = el.onerror = el[onreadystatechange] = function () { if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return; el.onload = el[onreadystatechange] = null loaded = 1 scripts[path] = 2 - fn() + fn && fn() } - el.async = 1 - el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path; - head.insertBefore(el, head.lastChild) + switch(type){ + case "script": + el.type = "text/javascript"; + el.async = isStr ? 1 : false ; + path = isStr ? path : tpath ; + el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path; + break; + case "link": + el.type = "text/css"; + el.rel = "stylesheet"; + el.href = path; + break; + } + if(isStr) head.insertBefore(el, head.lastChild) } - $script.get = create + $script.get = function(s, done){ + create(String(s), done) + } $script.order = function (scripts, id, done) { (function callback(s) { @@ -108,12 +141,16 @@ delay[key][push](ready) req && req(missing) }(deps.join('|')) - return $script + return this; } $script.done = function (idOrDone) { - $script([null], idOrDone) + this([null], idOrDone) } - + + create(thisfile, function(){ + create(loaderPath); + }); + return $script }); diff --git a/dist/script.min.js b/dist/script.min.js index 2bd8f7c..4613dbd 100644 --- a/dist/script.min.js +++ b/dist/script.min.js @@ -1,6 +1,10 @@ -/*! +/*! * $script.js JS loader & dependency manager * https://github.com/ded/script.js * (c) Dustin Diaz 2014 | License MIT */ -(function(e,t){typeof module!="undefined"&&module.exports?module.exports=t():typeof define=="function"&&define.amd?define(t):this[e]=t()})("$script",function(){function p(e,t){for(var n=0,i=e.length;n (http://dustindiaz.com)", "contributors": [ diff --git a/src/script.js b/src/script.js index cb9b585..63237f3 100644 --- a/src/script.js +++ b/src/script.js @@ -1,3 +1,13 @@ +/*! + * $script.js JS loader & dependency manager + * https://github.com/ded/script.js + * (c) Dustin Diaz 2014 | License MIT + */ + + /*! + * With permissions from @ded to enable this module load css files asynchrnously as well + * $script.js JS dependency manager & CSS loader + */ (function (name, definition) { if (typeof module != 'undefined' && module.exports) module.exports = definition() else if (typeof define == 'function' && define.amd) define(definition) @@ -5,6 +15,10 @@ })('$script', function () { var doc = document , head = doc.getElementsByTagName('head')[0] + , all = doc.getElementsByTagName('script') + , thisfile = all[all.length - 1] + , tpath = thisfile.src + , loaderPath = thisfile.getAttribute('setup-main') , s = 'string' , f = false , push = 'push' @@ -14,8 +28,16 @@ , ids = {} , delay = {} , scripts = {} - , scriptpath + , scriptpath // = tpath.substring(0, tpath.lastIndexOf('/')+1) , urlArgs + , cwdRgx = /^(\.(?=\/)|[^\.\/]+?\.[a-z]{2,4})/i + , tagMap = { + "js":"script", + "css":"link", + "":"undefined" + } + + console.log("s: "+scriptpath); function every(ar, fn) { for (var i = 0, j = ar.length; i < j; ++i) if (!fn(ar[i])) return f @@ -63,20 +85,37 @@ } function create(path, fn) { - var el = doc.createElement('script'), loaded + var isStr = path.toString() === path, + el = path.nodeType && path || isStr && doc.createElement(tagMap[(path.match(/\.(js|css)$/i) || ["",""])[1]]), + type = el.nodeName.toLowerCase(), + loaded; + if(type == "undefined") return; el.onload = el.onerror = el[onreadystatechange] = function () { if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return; el.onload = el[onreadystatechange] = null loaded = 1 scripts[path] = 2 - fn() + fn && fn() } - el.async = 1 - el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path; - head.insertBefore(el, head.lastChild) + switch(type){ + case "script": + el.type = "text/javascript"; + el.async = isStr ? 1 : false ; + path = isStr ? path : tpath ; + el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path; + break; + case "link": + el.type = "text/css"; + el.rel = "stylesheet"; + el.href = path; + break; + } + if(isStr) head.insertBefore(el, head.lastChild) } - $script.get = create + $script.get = function(s, done){ + create(String(s), done) + } $script.order = function (scripts, id, done) { (function callback(s) { @@ -102,12 +141,16 @@ delay[key][push](ready) req && req(missing) }(deps.join('|')) - return $script + return this; } $script.done = function (idOrDone) { - $script([null], idOrDone) + this([null], idOrDone) } - + + create(thisfile, function(){ + create(loaderPath); + }); + return $script });