Skip to content

Instantly share code, notes, and snippets.

@brettz9
Forked from erikvold/require.js
Created October 31, 2012 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettz9/3989023 to your computer and use it in GitHub Desktop.
Save brettz9/3989023 to your computer and use it in GitHub Desktop.
Imports a commonjs style javascript file with loadSubScrpt for restartless Firefox add-ons.
/* Imports a commonjs style javascript file with loadSubScrpt
* By Erik Vold <erikvvold@gmail.com> http://erikvold.com/
*
* @param src (String)
* The url of a javascript file.
*/
(function(global) {
var modules = {};
global.require = function require(src) {
if (modules[src]) return modules[src];
var scope = {require: global.require, exports: {}};
var tools = {};
Components.utils.import("resource://gre/modules/Services.jsm", tools);
var baseURI = tools.Services.io.newURI(__SCRIPT_URI_SPEC__, null, null);
try {
var uri = tools.Services.io.newURI(
"packages/" + src + ".js", null, baseURI);
tools.Services.scriptloader.loadSubScript(uri.spec, scope);
} catch (e) {
var uri = tools.Services.io.newURI(src, null, baseURI);
tools.Services.scriptloader.loadSubScript(uri.spec, scope);
}
return modules[src] = scope.exports || scope.module.exports;
}
})(this);
@brettz9
Copy link
Author

brettz9 commented Oct 31, 2012

Work with pattern allowing module.exports for function assignment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment