Skip to content

Commit

Permalink
rename module.externalDep -> module.externalDependency; improve runni…
Browse files Browse the repository at this point in the history
…ng without a DOM
  • Loading branch information
Paul Baumgart committed Aug 14, 2012
1 parent 46ac9c9 commit 31f3b04
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,33 @@ in the sense that when the resulting script is executed in the browser, the pack

For production deployment, you'll probably want to pipe the resulting output to the JavaScript minifier of your choice.

### Loading external scripts (e.g. CDN hosted jQuery) with module.externalDep
### Loading external scripts (e.g. CDN hosted jQuery) with module.externalDependency

In any of your modules that are browser-only, you can easily add external dependencies that are guaranteed to be loaded before your code executes by using the <code>module.externalDep</code> function.
In any of your modules that are browser-only, you can easily add external dependencies that are guaranteed to be loaded before your code executes by using the <code>module.externalDependency</code> function.

module.externalDep('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.externalDependency('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');

Note that the URL you pass to this function must be a constant string.

Multiple calls to module.externalDep will load any given script URL only once. However, it only does naive string matching, so:
Multiple calls to module.externalDependency will load any given script URL only once. However, it only does naive string matching, so:

module.externalDep('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.externalDep('https://AJAX.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.externalDependency('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.externalDependency('https://AJAX.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');

will load jQuery twice. To avoid bugs caused by typos, it's advisable to create a wrapper module around each external script you load and then load the wrapper module in the rest of your code. For example:

jquery.js:

module.externalDep('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.externalDependency('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.exports = $.noConflict(true);

myscript.js:

var $ = require('./jquery');
// etc...

If you execute a bundle with external dependencies without a DOM (e.g. in node.js), the dependencies will be silently ignored.


### devcdn

Expand Down
2 changes: 1 addition & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Module.prototype = {
itself.error = new Error(itself._errorMessageWithContext("can't handle non-const require", src, this[0].start && (this[0].start.line + 1)));
throw itself.error;
}
} else if (funcName === 'module.externalDep') {
} else if (funcName === 'module.externalDependency') {
var paramToken = this[2][0][0];
if (paramToken.start.type === 'string') {
scriptsToLoad.push({ token: this[0], value: paramToken.start.value });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "jsbundle",
"description": "Simple, clean, and automatic bundling of your Node modules and packages for use in the browser.",
"keywords": "browser require bundle module package static-analysis",
"version": "0.12.0",
"version": "0.13.0",
"repository": {
"type": "git",
"url": "git://github.com/proxv/jsbundle.git"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/abc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (typeof alert !== 'undefined') {

if (typeof document !== 'undefined') {
var $ = require('./jquery');
$('<h1>externalDep works!</h1>').appendTo($('body'));
$('<h1>externalDependency works!</h1>').appendTo($('body'));
}

require('./ghi.js');
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/jquery.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.externalDep('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.externalDependency('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
module.exports = $.noConflict(true);

18 changes: 9 additions & 9 deletions tmpl/bundle.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@
}

function loadScript(url, index) {
if (typeof document === 'undefined' ||
!document.createElement ||
!document.getElementsByTagName) {
return registerScriptLoad(url, index);
}

var scriptNode = document.createElement('script');
scriptNode.setAttribute('src', url);

Expand All @@ -73,10 +67,16 @@
}


for (var i = 0, len = _$_scriptsToLoad.length; i < len; i++) {
loadScript(_$_scriptsToLoad[i], i);
if (typeof document === 'undefined' ||
!document.createElement ||
!document.getElementsByTagName) {
// no DOM, so don't load external scripts
_$_scriptsToLoad = [];
} else {
for (var i = 0, len = _$_scriptsToLoad.length; i < len; i++) {
loadScript(_$_scriptsToLoad[i], i);
}
}

})();

@moduleDefs
Expand Down

0 comments on commit 31f3b04

Please sign in to comment.