Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created April 18, 2012 00:41
Show Gist options
  • Save danshultz/2410147 to your computer and use it in GitHub Desktop.
Save danshultz/2410147 to your computer and use it in GitHub Desktop.
"Stitch" solution
<html>
<header>
<script type="text/javascript" src="stitch_header.js"></script>
<script type="text/javascript" src="my-module/name.js"></script>
<script type="text/javascript">
var Name = window.require("my-module/name");
window.nameApp = new Name("My App");
</script>
</header>
<body></body>
</html>
window.require.define("my-module/name": function(exports, require, module) {
module.exports = function(name) {
this.name = name
};
});
window.require.define("my-module/name": function(exports, require, module) {
module.exports = function(name) {
this.name = name
};
});
(function(/*! Stitch !*/) {
if (!this.require) {
var modules = {}, cache = {}, require = function(name, root) {
var path = expand(root, name), indexPath = expand(path, './index'), module, fn;
module = cache[path] || cache[indexPath]
if (module) {
return module;
} else if (fn = modules[path] || modules[path = indexPath]) {
module = {id: path, exports: {}};
cache[path] = module.exports;
fn(module.exports, function(name) {
return require(name, dirname(path));
}, module);
return cache[path] = module.exports;
} else {
throw 'module ' + name + ' not found';
}
}, expand = function(root, name) {
var results = [], parts, part;
if (/^\.\.?(\/|$)/.test(name)) {
parts = [root, name].join('/').split('/');
} else {
parts = name.split('/');
}
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part == '..') {
results.pop();
} else if (part != '.' && part != '') {
results.push(part);
}
}
return results.join('/');
}, dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};
this.require = function(name) {
return require(name, '');
}
this.require.define = function(bundle) {
for (var key in bundle)
modules[key] = bundle[key];
};
this.require.modules = modules;
this.require.cache = cache;
}
return this.require.define;
}).call(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment