Permalink
Browse files

rename module.externalDep -> module.externalDependency; improve runni…

…ng without a DOM
  • Loading branch information...
Paul Baumgart
Paul Baumgart committed Aug 14, 2012
1 parent 46ac9c9 commit 31f3b042141198956ae573b645dcc5b2b3764d6b
Showing with 22 additions and 20 deletions.
  1. +9 −7 README.md
  2. +1 −1 lib/module.js
  3. +1 −1 package.json
  4. +1 −1 test/fixtures/abc.js
  5. +1 −1 test/fixtures/jquery.js
  6. +9 −9 tmpl/bundle.tmpl
View
@@ -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
View
@@ -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 });
View
@@ -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"
View
@@ -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');
View
@@ -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);
View
@@ -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);
@@ -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

0 comments on commit 31f3b04

Please sign in to comment.