Reputation: 859
I am developing a Firefox addon that has a Chrome counterpart. To eliminate code duplication there are some JavaScript libraries that they will share in common. The libraries are not written with the CommonJS design that Firefox jetpack modules expect. Is there a good way to import and use the shared JavaScript into my Firefox modules?
Preferably the shared code can sit in a directory in my root directory, as the shared library has its own rich directory structure and I don't want to stuff it all into the data or lib directory.
Upvotes: 4
Views: 335
Reputation: 5830
It's easy to make a JS file loadable via commonjs, you just need to add the functions needed as properties to an exports object, something like:
function foo() {}
function bar() {}
if (exports !== void 0) {
exports.foo = foo;
exports.bar = bar;
}
Upvotes: 1
Reputation: 21667
If you are building a xul based addon and you have a library that you want to use in it, you just have to put it on your content directory and link to the library files in the xul files.
- content\
- yourAddon.xul
- yourAddon.js
- yourLibDirectory\
- libfile1.js
Then, on yourAddon.xul you include it in the header:
<script type="application/x-javascript" src="chrome://yourExtension/content/yourAddon.js"/>
<script type="application/x-javascript" src="chrome://yourExtension/content/yourLibDirectory/libfile1.js"/>
If you do it with the addon-sdk, i'm not of much help. :)
Upvotes: 1