I’m trying to load a module from within addon code (not content script) that is compatible with CommonJS but requires a module whose path needs to be specified.
After reading through the documentation for toolkit/loader
, I thought I could accomplish what I’m after by simply creating a custom Loader
and specifying a modules
export in its options. Like so:
let { Loader, Module, load } = require("toolkit/loader");
let self = require("sdk/self");
// `bar` is required by `foo`
let loader = Loader({
modules: {
"bar": require("./bar")
}
});
let module = Module("foo", self.data.url("../lib/foo.js"));
load(loader, module);
And in foo
, I simply require bar
:
require("bar");
For whatever reason, this approach doesn’t work. Maybe it requires the use of Cu.import
or some such. The documentation is anything but clear.
I then took a different approach, one relying on specifying the paths
attribute:
let self = require("sdk/self");
let loader = Loader({
paths: {
"bar": self.data.url("../lib/bar.js"),
}
});
let module = Module("foo", self.data.url("../lib/foo.js"));
load(loader, module);
But now nothing pertaining to the SDK loads inside foo
. For instance, the following fails:
require("sdk/timers")
Questions:
- how can I specify the path to a module that is included somewhere without that causing havoc?
- how can I retrieve the URL of an asset in the extension’s
lib
directory? I’m currently usingrequire("sdk/self").data.url("../lib/asset")
, as you can see above, but surely there is a better way?