lib/define.js
function ensure(parent, ids, fn) {
		ids = [].concat(ids);
		var visited = {}, wait = 0;

		function done() {
			if (fn) { fn.apply(global, map(ids, function(id) { return require(parent, id); })); }
			fn = undefined;
		}
		function visit(module) {
			if (module.id in visited) { return; }
			if ((visited[module.id] = module.loaded)) { return map(module.children, visit); }

			++wait;
			waits[module.id].push(function() {
				map(module.children, visit);
				if (--wait <= 0) { setTimeout(done, 0); }
			});

			var script;
			map(doc.querySelectorAll('script'), function(node) {
				if (canonicalize(node.src) === module.uri) { script = node; }
			});

			if (!script) {
				script = doc.createElement('script');
				script.onload = script.onerror = script.onreadystatechange = function() {
					if ('loading' === script.readyState) { return; }
					script.onload = script.onerror = script.onreadystatechange = null;
					fireWaits(module);
				};
				script.defer = true;
				script.src = module.uri;
				doc.querySelector('head').appendChild(script);
			}
		}

		map(ids, function(id) { visit(getModule(parent, id)); });
		if (wait <= 0) { setTimeout(done, 0); } // always async
	}
function ensure(parent, ids, fn) {
		ids = [].concat(ids);
		var visited = {}, wait = 0;

		function done() {
			if (fn) { fn.apply(global, map(ids, function(id) { return require(parent, id); })); }
			fn = undefined;
		}
		function visit(module) {
			if (module.id in visited) { return; }
			if ((visited[module.id] = module.loaded)) { return map(module.children, visit); }

			++wait;
			waits[module.id].push(function() {
				map(module.children, visit);
				if (--wait <= 0) { setTimeout(done, 0); }
			});

			var script;
			map(doc.querySelectorAll('script'), function(node) {
				if (canonicalize(node.src) === module.uri) { script = node; }
			});

			if (!script) {
				script = doc.createElement('script');
				script.onload = script.onerror = script.onreadystatechange = function() {
					if ('loading' === script.readyState) { return; }
					script.onload = script.onerror = script.onreadystatechange = null;
					fireWaits(module);
				};
				script.defer = true;
				script.src = module.uri;
				doc.querySelector('head').appendChild(script);
			}
		}

		map(ids, function(id) { visit(getModule(parent, id)); });
		if (wait <= 0) { setTimeout(done, 0); } // always async
	}
function ensure ( parent , ids , fn ) { ids = [ ] . concat ( ids ) ; var visited = { } , wait = 0 ; function done ( ) { if ( fn ) { fn . apply ( global , map ( ids , function ( id ) { return require ( parent , id ) ; } ) ) ; } fn = undefined ; } function visit ( module ) { if ( module . id in visited ) { return ; } if ( ( visited [ module . id ] = module . loaded ) ) { return map ( module . children , visit ) ; } ++ wait ; waits [ module . id ] . push ( function ( ) { map ( module . children , visit ) ; if ( -- wait <= 0 ) { setTimeout ( done , 0 ) ; } } ) ; var script ; map ( doc . querySelectorAll ( 'script' ) , function ( node ) { if ( canonicalize ( node . src ) === module . uri ) { script = node ; } } ) ; if ( ! script ) { script = doc . createElement ( 'script' ) ; script . onload = script . onerror = script . onreadystatechange = function ( ) { if ( 'loading' === script . readyState ) { return ; } script . onload = script . onerror = script . onreadystatechange = null ; fireWaits ( module ) ; } ; script . defer = true ; script . src = module . uri ; doc . querySelector ( 'head' ) . appendChild ( script ) ; } } map ( ids , function ( id ) { visit ( getModule ( parent , id ) ) ; } ) ; if ( wait <= 0 ) { setTimeout ( done , 0 ) ; } // always async }
make sure the modules are loaded
make sure the modules are loaded