Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this PR references #76 issue #77

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<script setup-main="./js/test-setup.js" type="text/javascript" src="./script.js"></script>

// 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.
Expand Down
57 changes: 47 additions & 10 deletions dist/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
* (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)
else this[name] = definition()
})('$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'
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
});
8 changes: 6 additions & 2 deletions dist/script.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"jsonp",
"loader"
],
"version": "2.5.7",
"version": "2.5.8",
"homepage": "https://github.com/ded/script.js",
"author": "Dustin Diaz <dustin@dustindiaz.com> (http://dustindiaz.com)",
"contributors": [
Expand Down
63 changes: 53 additions & 10 deletions src/script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
/*!
* $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)
else this[name] = definition()
})('$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'
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
});