在 Firefox Extension 中加载多个 JS 文件

Load multiple JS files in Firefox Extension

我正在使用 JPM 创建 Firefox 的扩展。在 package.json 文件中,我们将这一行作为入口点...

"main": "index.js"

我该如何更改它,或者我还能做什么,以便在我的扩展中包含更多的 JS 文件?我基本上移植了一个 Chrome 扩展,我有 2-3 个 JS 文件。

我尝试了以下方法,但没有用。

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

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

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

再澄清一点,这两个文件都是在后台 运行 而不是内容脚本。

据我所知,通过 package.json 文件您只能指定一个 main.jsscript .

正如 developers' documentation 所说:

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.

因此,您可以在 lib 目录中创建自己的库,假设 test.js 遵循文档并包含在您的 main.js 中。看看这个例子:

test.js:

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

在你的 main.js:

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

我遇到了同样的问题。如果您将 'file2.js' 放在 'data' 目录中,您可以添加以下行 var x = require('./data/file2.js'); 在 'main.js' 文件中。然后像这样调用你的函数: x.yourFunc(); 请务必在 file2.js 中导出您的函数,并为每个函数分配一个 exports 变量: exports.func1 = function() { return yourFunc(); }