wxt icon indicating copy to clipboard operation
wxt copied to clipboard

WXT should bundle multiple `background.js` scripts like it does with `content.js`

Open placoderm opened this issue 1 year ago • 2 comments

Feature Request

Currently it is easy to modularize the content scripts in the entrypoints folder by simply appending .content.ts to the file name.

Add the same ability to background.js.

Is your feature request related to a bug?

N/A

What are the alternatives?

From @aaronklinker-st :

For now, a quick fix to using multiple background scripts is to use Vite's glob import:

// background/index.ts
const scripts = import.meta.glob<{ default: () => void }>('./*.background.ts', { eager: true });

export default defineBackground(() => {
  Object.values(scripts).map(script => script.default.main());
});

Then put your other files in like background/some-name.background.ts

Additional context

https://discord.com/channels/1212416027611365476/1290896813850886197

placoderm avatar Oct 03 '24 19:10 placoderm

But isn't manifest version 3 only takes one script for service_worker?

Also, background-wxt

Above screenshot will supposedly take all the entrypoints. But how to modularize this into one single file? Cause rollup will create separate files each background files even if we use getLibModeConfig or getMultiPageConfig functions of vite builder?

nishu-murmu avatar Dec 27 '24 09:12 nishu-murmu

We just need to update the virtual entrypoint to support multiple files. Right now it only supports one:

https://github.com/wxt-dev/wxt/blob/main/packages/wxt/src/virtual/background-entrypoint.ts

This is the file we actually bundle into the single background script.

aklinker1 avatar Dec 28 '24 14:12 aklinker1