在Chrome扩展中使用多个JavaScript文件

在Chrome扩展中使用多个JavaScript文件,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我正在学习如何进行Chrome扩展,但我很难找出组织代码的最佳实践。在任何其他语言中,我都知道我会将代码库分解为多个文件并进行导入,但当我有这样一个文件树时: | |-background.js |-someotherfile.js 而someotherfile.js的函数如下 export function somefunction(){alert("somefunction was called");} 但在background.js中,当我这样做时 import * from 'som

我正在学习如何进行Chrome扩展,但我很难找出组织代码的最佳实践。在任何其他语言中,我都知道我会将代码库分解为多个文件并进行导入,但当我有这样一个文件树时:

|
|-background.js
|-someotherfile.js
而someotherfile.js的函数如下

export function somefunction(){alert("somefunction was called");}
但在background.js中,当我这样做时

import * from 'someotherfile';
chrome.runtime.onInstalled.addListener(function() {
    somefunction();
});
我加载了我的未打包扩展,background.js不会运行,即使我在background.js中添加了一些函数


在Chrome中组织代码的正确做法是什么?

Chrome还不支持动态加载导入。您需要捆绑它或使用类似于
require
的解决方案。或者使用polyfill:Bundle,比如,有一个Gulp任务将我的所有JavaScript文件连接到一个background.js中?是的。或网页包等。或只是列出清单中的其他文件。谢谢wOxxOm和Daniel!