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

RESOLVED FIXED

Status

defect
P1
normal
RESOLVED FIXED
5 years ago
4 years ago

People

(Reporter: irakli, Assigned: irakli)

Tracking

({dev-doc-complete})

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(2 attachments)

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..
Duplicate of this bug: 1056396
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

Nice!
Attachment #8512234 - Flags: review?(evold) → review+
Priority: -- → P1

Comment 6

5 years ago
Commit pushed to master at https://github.com/mozilla/addon-sdk

https://github.com/mozilla/addon-sdk/commit/21fb4655e732fa6edc863eec7b216d527aeae000
Merge pull request #1687 from Gozala/shared-require-test@1070927

Bug 1070927 - Make shared require use rootURI it's loaded from.
Status: NEW → RESOLVED
Last Resolved: 5 years ago
Resolution: --- → FIXED

Comment 7

4 years ago
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: https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Module_structure_of_the_SDK#Using_modules_from_outside_the_Add-on_SDK.

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: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Using#Importing_CommonJS_modules.

Does it look OK?
Flags: needinfo?(trev.moz)

Comment 9

4 years ago
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 https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/_loader#Usage (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.

Comment 11

4 years ago
Looks good, thank you.
You need to log in before you can comment on or make changes to this bug.