firefox-addonfirefox-addon-sdkjpm

Load multiple JS files in Firefox Extension


I am creating an extension for Firefox using JPM. In the package.json file, we have this line for the entry point...

"main": "index.js"

How do I change it, or what else can I do, to include more JS files in my extension? I am basically porting a Chrome extension where I have 2-3 JS files.

I tried the following, but it didn't work.

"main": ["index.js", "file2.js"]

"main": [{"index.js", "file2.js"}]

"main": "index.js,file2.js"

To clarify a bit more, both of these files are meant to run in the background and are not content scripts.


Solution

  • As far as I know, through the package.json file you can specify only one main.jsscript .

    As the developers' documentation says:

    Minimally you'll have a single module implemented by a script called "main.js", but you can include additional modules in lib, and import them using the require() function. To learn how to implement and import your own modules, see the tutorial on Implementing Reusable Modules.

    So, you can create your own library in the lib directory, let's say test.js following the documentation and included to your main.js. Take a look at an example of this:

    test.js:

    vat test = function() {console.log("I'm a module");};
    exports.test = test;
    

    and in your main.js:

    var test = require('./test');
    test.test();