Closed Bug 1070927 Opened 9 years ago Closed 9 years ago

Expose `require` to contexts where `Cu.import` is present


(Add-on SDK Graveyard :: General, defect, P1)



(Not tracked)



(Reporter: irakli, Assigned: irakli)



(Keywords: dev-doc-complete)


(2 files)

CommonJS modules are second class citizens, at the moment even we favor JSMs if code needs to be shared. There are also other users who wish to use require (see Bug 1056396) in contexts like frame scripts etc..
before seeing this bug, i wrote up an explanation of my attempt to use our Loader (and thus require) in the child process in bug 1058698 comment 8, though that is somewhat different, and possibly out of scope for this bug, since it requires configuring the Loader with proper options so that it looks like module instances are part of the addon -- for debugging purposes, self stuff, etc..
Attachment #8508385 - Flags: review?(evold) → review+
Assignee: nobody → rFobic
OS: Mac OS X → All
Hardware: x86 → All
Comment on attachment 8512234 [details] [review]
override support + tests

Attachment #8512234 - Flags: review?(evold) → review+
Priority: -- → P1
Commit pushed to master at
Merge pull request #1687 from Gozala/shared-require-test@1070927

Bug 1070927 - Make shared require use rootURI it's loaded from.
Closed: 9 years ago
Resolution: --- → FIXED
This possibility was never documented, and I think it should be unless this was meant as a purely internal feature.
Keywords: dev-doc-needed
I've added a note and an example here:

Since it seems like this is going to be of most use to people *not* using the SDK already, I've also added a similar note here:

Does it look OK?
Flags: needinfo?(trev.moz)
It's news to me that you can pass in a full URL - and I don't think this is how people will normally use it (at least not with the built-in modules). It's also a bad idea, I doubt that the resource://gre/modules/commonjs URL is guaranteed to stay unchanged.

I think that the examples should be require("sdk/base64") and require("resource://my-extension/main.js") - full URL only if you need to load your own CommonJS modules.

Note that in the latter scenario one might be better off creating their own loader as explained on (should probably be linked).
Flags: needinfo?(trev.moz)
Thanks Wladimir, that makes sense. I've updated the text.

I don't want to refer to extensions here, because I think that in the medium/long term extensions will no longer get access to Cu.import, so encouraging people to acquire a new dependency on it now seems like a bad idea.

I'm marking this dev-doc-complete, but please do let me know if you need anything else.
Looks good, thank you.
You need to log in before you can comment on or make changes to this bug.