aklinker1 / vite-plugin-web-extension

Vite plugin for developing Chrome/Web Extensions
https://vite-plugin-web-extension.aklinker1.io/
MIT License
553 stars 48 forks source link

Content Script Reloading #73

Closed aklinker1 closed 11 months ago

aklinker1 commented 1 year ago

With HMR, at least when using Vue inside content scripts, it's possible to save a .vue file that is used by both an extension page and a content script. When doing so, the page will reload immediately but when the content script finishes building, it will reload the entire extension, closing the extension page and slowing development down. You're then forced to reopen the extension page.

This can also happen with Vue when the file is not used by content scripts, but is in the same folder. Which sucks, really slowing things down.

So instead of reloading the entire extension when a content script changes, we should be able to reload individual content scripts from the background instead.

This would have the added benefit of not having to refresh the page to run the new content script code.

The idea here is to use the scripting APIs to register, unregister, and re-register the content scripts programmatically from the background instead of listing them in the manifest.

In addition to that logic, we will also need to stop the content scripts and silence the "Extension context invalidated" errors. We don't want two of the same content script running.

aklinker1 commented 11 months ago

I have implemented this on my new framework, wxt. See https://github.com/aklinker1/wxt/issues/4 and https://github.com/aklinker1/wxt/pull/25.

If I were to add support here, it would only be support for MV3 - the scripting APIs are only available for MV3, specifically the registerContentScripts and updateContentScripts functions.

The insertCss, removeCss, and executeScript functions available in MV2 are not enough to respect the run_at field of content scripts, and thus I don't want to support them if the dev experience will be different from production.

aklinker1 commented 11 months ago

I'm not going to implement this in the plugin. As a plugin, not being in control of the entire build process, there are certain limitations that make implementing this difficult. Use WXT if this is important to you.