From cf4b05bba3f6ea20477d4c7888462d4d99fa36be Mon Sep 17 00:00:00 2001 From: AitorC Date: Mon, 21 Mar 2016 10:42:06 +0100 Subject: [PATCH 1/3] .path now can be a String or a Function --- src/script.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/script.js b/src/script.js index e3fdfb7..9e54bc1 100644 --- a/src/script.js +++ b/src/script.js @@ -27,6 +27,9 @@ return 1 }) } + function isFunction(object) { + return typeof(object) === 'function'; + } function $script(paths, idOrDone, optDone) { paths = paths[push] ? paths : [paths] @@ -51,7 +54,11 @@ if (path === null) return callback() if (!force && !/^https?:\/\//.test(path) && scriptpath) { - path = (path.indexOf('.js') === -1) ? scriptpath + path + '.js' : scriptpath + path; + if (isFunction(scripts)) { + path = scriptpath(path); + } else { + path = (path.indexOf('.js') === -1) ? scriptpath + path + '.js' : scriptpath + path; + } } if (scripts[path]) { From 72c9d83deea1939940dfb35f64933b7ef5c24612 Mon Sep 17 00:00:00 2001 From: AitorC Date: Mon, 21 Mar 2016 11:04:35 +0100 Subject: [PATCH 2/3] Update Readme --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f89b844..c4153a6 100644 --- a/README.md +++ b/README.md @@ -127,15 +127,32 @@ $script.ready('my-awesome-plugin', function() { ### $script.path() -Optionally to make working with large projects easier, there is a path variable you can set to set as a base. +Optionally to make working with large projects easier, there's a path variable that can be either a String or a Function (that will recieve the given path as first +argument): ``` js +// use a String as base path $script.path('/js/modules/') $script(['dom', 'event'], function () { + // /js/modules/dom.js and /js/modules/event.js will be loaded + // use dom & event +}); + +//Or use a function as base path +var fnc = function(path) { + //Please note that when using as a function, .js wont be attached automatically + return path === 'dom' ? ('//external.cdn.com/some/path/' + path + '.js') : ('/js/modules/' + path + '.js'); +}; + +$script.path(fnc) +$script(['dom', 'event'], function () { + // external.cdn.com/some/path/dom.js and /js/modules/event.js will be loaded // use dom & event }); ``` +Please notice that the string ".js" is not appended automatically if a Function is used as path. + Note that this will include all scripts from here on out with the base path. If you wish to circumvent this for any single script, you can simply call $script.get() ``` js From 1602e257f6a86fbac601270e16b858c712202b12 Mon Sep 17 00:00:00 2001 From: AitorC Date: Mon, 21 Mar 2016 11:36:31 +0100 Subject: [PATCH 3/3] Update regexp, now "//xxx.yyy.com/xxx/yyy" is also detected as a full path --- src/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/script.js b/src/script.js index 9e54bc1..7ecfe5e 100644 --- a/src/script.js +++ b/src/script.js @@ -53,7 +53,7 @@ each(paths, function loading(path, force) { if (path === null) return callback() - if (!force && !/^https?:\/\//.test(path) && scriptpath) { + if (!force && !/^(https?:)?\/\//.test(path) && scriptpath) { if (isFunction(scripts)) { path = scriptpath(path); } else {