4

In Google Chrome extensions before manifest v3 I could divide background.js into multiple scripts. And to load all scripts to be visible in background.js I only need to to put them in array in manifest like this:

    "background": {
    "scripts": ["helpers/countHelper.js", "helpers/networkHelper.js", "background/main.js"],
}

With this above sintax, all scripts will be merged into one file.

Now in documentation how to use manifest v3, they wrote:

Replace background.page or background.scripts with background.service_worker in manifest.json. Note that the service_worker field takes a string, not an array of string.

Cause background in manifest can't take multiple scripts, how to divide background script into multiple scripts. Also import syntax is not allowed as I know.

1 Answers1

5

With this above sintax, all scripts will be merged into one file.

It is not true, but let's fly over...

You can use importScript but not in manifest. Look at this template:

/* V3 manifest.json */
"background": {
    "service_worker": "worker_wrapper.js"
},

/* worker_wrapper.js */
try {
    importScripts("helpers/countHelper.js", "helpers/networkHelper.js", "background/main.js");
} catch (e) {
    console.log(e);
}

Bear in mind that SW file must be placed in root folder, whereas other scripts could be placed in other folders

Robbi
  • 702
  • 1
  • 5
  • 8