Load multiple JS files in Firefox Extension

2k Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER

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();
0
On

I had the same problem. If you put your 'file2.js' in the 'data' directory you can add the line var x = require('./data/file2.js'); in the 'main.js' file. Then call your function like: x.yourFunc(); Be sure to export your functions in file2.js with an assignment to the exports variable for each function: exports.func1 = function() { return yourFunc(); }