Reputation: 627
Is there any technique to break down or modularize the background page in a chrome extension? As I go on developing my extension almost all of my javascript is in the background page , since it's the only long lasting running script of the extension (the minority of my javascript is in popup.js and content scripts). Just wanted to know what do developers do - just accept a very big background.js file? Ideally I would like to "include" different js files containing data or objects and simply include them to background.js but that is obviously not possible in javascript .
Any advice ?
Upvotes: 1
Views: 201
Reputation: 6169
In your manifest.json file, you can specify multiple scripts to be loaded into your background event page:
"background": {
"scripts": [
"foo.js",
"bar.js",
"baz.js"
// etc
],
"persistent": true // or false!
}
Upvotes: 1
Reputation: 11438
Instead of specifying a background script, you can also specify your own background page, see http://developer.chrome.com/extensions/background_pages.html for docs. Such a background page allows you to load any number of scripts using regular script tags, or other techniques. When you specify a background script instead of a background page, a default background page (with one script tag pointing to your script) is generated for you.
You could also look into script loaders like RequireJS.
I'm currently developing an extension using CommonJS modules (i.e. the style used in Node.js) compiled with Browserify, which combines all modules into one big file. This works well for me.
Upvotes: 0