Skip to content
Browse files

New module loader for Firebug

  • Loading branch information...
1 parent f634ee3 commit cec176bfff7c630a51b2a365a30a918d00fe88d9 @janodvarko janodvarko committed
View
2 extension/content/firebug/firefox/firebugFrame.xul
@@ -22,7 +22,7 @@
</script>
<script type="application/x-javascript" src="chrome://firebug/content/firefox/panelBarWaiter.js"/>
- <script type="application/x-javascript" src="resource://firebug/require.js"/>
+ <script type="application/x-javascript" src="resource://firebug/mini-require.js"/>
<script type="application/x-javascript" src="resource://firebug/require-debug.js"/>
<script type="application/x-javascript" src="chrome://firebug/content/main.js"/>
View
128 extension/content/firebug/main.js
@@ -1,7 +1,7 @@
/* See license.txt for terms of usage */
-try {
(function() {
+
// ********************************************************************************************* //
const Cc = Components.classes;
@@ -76,7 +76,7 @@ var modules = [
"firebug/chrome/chrome",
"firebug/lib/lib",
"firebug/firebug",
- "arch/browser"
+ "firebug/bti/inProcess/browser"
].concat(config.modules);
// ********************************************************************************************* //
@@ -85,74 +85,32 @@ require(config, modules, function(ChromeFactory, FBL, Firebug, Browser)
{
try
{
- if (FBTrace.DBG_INITIALIZE || FBTrace.DBG_MODULES)
- {
- var delta = (new Date().getTime()) - startLoading;
- FBTrace.sysout("main.js; Firebug modules loaded using RequireJS in "+delta+" ms");
- }
-
- // Extensions also shouldn't use the global require since it should be removed
- // in the future (if possible). Global 'require' could collide with other
- // extensions.
- Firebug.connection = new Browser(); // prepare for addListener calls
-
- Browser.onDebug = function()
- {
- FBTrace.sysout.apply(FBTrace, arguments);
- }
-
- Firebug.Options.initialize(prefDomain);
-
- function connect()
- {
- Firebug.connection.connect(); // start firing events
- }
-
// Wait till all modules (including those coming from Firebug extensions)
// are loaded and thus all panels, firebug-modules, bundles, etc. are properly
// registered and Firebug can start to send initialization events.
- var prevResourcesReady = requirejs.resourcesReady;
- requirejs.resourcesReady = function(isReady)
+ if (typeof(requirejs) != "undefined")
{
- // Extensions are using the same loader so, make sure to not
- // initialize Firebug twice.
- if (Firebug.isInitialized)
- return;
-
- if (isReady && requirejs.resourcesDone)
+ var prevResourcesReady = requirejs.resourcesReady;
+ requirejs.resourcesReady = function(isReady)
{
- if (FBTrace.DBG_INITIALIZE || FBTrace.DBG_MODULES)
- FBTrace.sysout("main.js; All RequireJS modules loaded");
-
- if (window.FBL.legacyPatch)
- {
- if (FBTrace.DBG_MODULES)
- FBTrace.sysout("firebug main.js; legacyPatch");
-
- window.FBL.legacyPatch(FBL, Firebug);
- }
-
- if (FBTrace.DBG_MODULES)
- require.analyzeDependencyTree();
-
- if (!window.panelBarWaiter && FBTrace.DBG_ERRORS)
- FBTrace.sysout("main; ERROR window.panelBarWaiter is not available " +
- ", Firebug already initialized: " + Firebug.isInitialized);
+ if (isReady && requirejs.resourcesDone)
+ onModulesLoaded(ChromeFactory, FBL, Firebug, Browser);
- if (window.panelBarWaiter)
- window.panelBarWaiter.waitForPanelBar(ChromeFactory, null, connect);
+ if (prevResourcesReady)
+ prevResourcesReady(isReady);
}
-
- if (prevResourcesReady)
- prevResourcesReady(isReady);
+ }
+ else
+ {
+ onModulesLoaded(ChromeFactory, FBL, Firebug, Browser);
}
}
catch(exc)
{
if (FBTrace)
- FBTrace.sysout("Firebug main initialization ERROR "+exc, exc);
+ FBTrace.sysout("Firebug main initialization ERROR " + exc, exc);
- window.dump("Firebug main initialization ERROR "+exc+"\n");
+ window.dump("Firebug main initialization ERROR " + exc);
if (Components)
Components.utils.reportError(exc);
@@ -160,15 +118,61 @@ require(config, modules, function(ChromeFactory, FBL, Firebug, Browser)
});
// ********************************************************************************************* //
-})();
-} catch (exc) {
+function onModulesLoaded(ChromeFactory, FBL, Firebug, Browser)
+{
+ // Extensions are using the same loader so, make sure to not
+ // initialize Firebug twice.
+ if (Firebug.isInitialized)
+ return;
- window.dump("Firebug main ERROR " + exc + "\n");
+ if (FBTrace.DBG_INITIALIZE || FBTrace.DBG_MODULES)
+ {
+ var delta = (new Date().getTime()) - startLoading;
+ FBTrace.sysout("main.js; Firebug modules loaded using RequireJS in " + delta + " ms");
+ }
+
+ // Extensions also shouldn't use the global require since it should be removed
+ // in the future (if possible). Global 'require' could collide with other
+ // extensions.
+ Firebug.connection = new Browser(); // prepare for addListener calls
+
+ Browser.onDebug = function()
+ {
+ FBTrace.sysout.apply(FBTrace, arguments);
+ }
+
+ Firebug.Options.initialize(prefDomain);
+
+ function connect()
+ {
+ Firebug.connection.connect(); // start firing events
+ }
+
+ if (FBTrace.DBG_INITIALIZE || FBTrace.DBG_MODULES)
+ FBTrace.sysout("main.js; All RequireJS modules loaded");
- if (Components)
- Components.utils.reportError(exc);
+ if (window.FBL.legacyPatch)
+ {
+ if (FBTrace.DBG_MODULES)
+ FBTrace.sysout("firebug main.js; legacyPatch");
+
+ window.FBL.legacyPatch(FBL, Firebug);
+ }
+
+ if (FBTrace.DBG_MODULES)
+ require.analyzeDependencyTree();
+
+ if (!window.panelBarWaiter && FBTrace.DBG_ERRORS)
+ FBTrace.sysout("main; ERROR window.panelBarWaiter is not available " +
+ ", Firebug already initialized: " + Firebug.isInitialized);
+
+ if (window.panelBarWaiter)
+ window.panelBarWaiter.waitForPanelBar(ChromeFactory, null, connect);
}
// ********************************************************************************************* //
+})();
+
+// ********************************************************************************************* //
View
163 extension/modules/mini-require.js
@@ -0,0 +1,163 @@
+/* See license.txt for terms of usage */
+
+// ********************************************************************************************* //
+// Module Loader Implementation
+
+var require, define;
+
+(function() {
+
+// ********************************************************************************************* //
+// Constants
+
+var Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+// ********************************************************************************************* //
+// Module Loader implementation
+
+var Loader =
+{
+ config: {},
+ modules: {},
+ currentModule: [],
+ payloads: {},
+
+ require: function(config, modules, callback)
+ {
+ this.config = config ? config : this.config;
+ this.currentModule = [];
+
+ var main = this.modules["main"] = {
+ scope: {}
+ };
+
+ this.currentModule.push(main);
+ this.lookup(modules, callback);
+ },
+
+ define: function(moduleId, deps, payload)
+ {
+ payload.deps = deps;
+ this.payloads[moduleId] = payload;
+ },
+
+ lookup: function(moduleId, deps, callback)
+ {
+ // Module name doesn't have to be specified.
+ if (arguments.length == 2)
+ {
+ callback = deps;
+ deps = moduleId;
+ moduleId = undefined;
+ }
+
+ var self = this;
+ var args = deps.map(function(dep) {
+ return self.loadModule(dep);
+ });
+
+ try
+ {
+ var module = this.currentModule[this.currentModule.length - 1];
+ module.exports = callback.apply(module.scope, args);
+ }
+ catch (err)
+ {
+ Cu.reportError(err);
+ }
+ },
+
+ loadModule: function(moduleId)
+ {
+ var module = this.modules[moduleId];
+ if (module)
+ return module.exports;
+
+ module = this.modules[moduleId] = {};
+ module.scope = {
+ define: this.lookup.bind(this)
+ }
+
+ this.currentModule.push(module);
+
+ // If the module is already registered, load all deps and execute.
+ // Otherwise we need to load the script from URL.
+ var payload = this.payloads[moduleId];
+ if (payload)
+ {
+ this.lookup(moduleId, payload.deps, payload);
+ }
+ else
+ {
+ this.load(module.scope, moduleId);
+ }
+
+ this.currentModule.pop();
+
+ // Exports (the module return value in case of AMD) is set in define function.
+ return module.exports;
+ },
+
+ load: function(moduleScope, moduleId)
+ {
+ //xxxHonza: Remaping moved modules
+
+ var moduleUrl = this.getModuleUrl(moduleId) + ".js";
+
+ try
+ {
+ Services.scriptloader.loadSubScript(moduleUrl, moduleScope);
+ }
+ catch (err)
+ {
+ Cu.reportError(moduleId + " -> " + moduleUrl);
+ Cu.reportError(err);
+ }
+ },
+
+ getModuleUrl: function(moduleId)
+ {
+ var baseUrl = this.config.baseUrl;
+ if (baseUrl.substr(-1) != "/")
+ baseUrl += "/";
+
+ // If there are no aliases just use baseUrl.
+ if (!this.config.paths)
+ return baseUrl + moduleId;
+
+ // Get module id path parts (excluding the module name).
+ var parts = moduleId.split("/");
+ var moduleName = parts.pop();
+
+ var self = this;
+ var resolved = parts.map(function(part)
+ {
+ var alias = self.config.paths[part];
+ return alias ? alias : part;
+ });
+
+ var moduleUrl = resolved.join("/");
+ if (moduleUrl.substr(-1) != "/")
+ moduleUrl += "/";
+
+ moduleUrl += moduleName;
+
+ var reProtocol = /^[^:]+(?=:\/\/)/;
+ if (moduleUrl.match(reProtocol))
+ return moduleUrl;
+
+ // If there is no protocol, use baseUrl.
+ return baseUrl + moduleUrl;
+ }
+}
+
+// ********************************************************************************************* //
+// Public API
+
+require = Loader.require.bind(Loader);
+define = Loader.define.bind(Loader);
+
+// ********************************************************************************************* //
+})();

0 comments on commit cec176b

Please sign in to comment.
Something went wrong with that request. Please try again.