aklinker1 / vite-plugin-web-extension

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

Refactor plugin management #105

Closed aklinker1 closed 1 year ago

aklinker1 commented 1 year ago

Overview

This closes #102. Related to #99

What was happening? Essentially, we were re-using the fully resolved and expanded plugins from the original vite build, rather than starting fresh and reading from the vite.config.ts file for each build step.

This was done to fix recusion where the vite-plugin-web-extension plugin was loaded for each build step. See #56.

However, that had some unforeseen consequences. Since we were reusing plugin instances, any variables or flags they store internally would remain for subsequent builds. For example, the Vue plugin: the top level build is a dev server, so that plugin is configured to transform all files so they have HMR enabled and work with the dev server. However the child builds (like for content scripts) are not dev builds, and should not be transformed for HMR and dev mode. Since we were reusing the instance for the content script builds, it thought the dev server was running, transformed the vue code to include references to import.meta.hot, but that variable doesn't exist in production builds, so we saw errors like in #102.

Discovering config files and NOT reusing plugin instances fixed this. Each build creates it's own instances of their plugins, solving that problem.

We still needed to be able to remove the webExtension plugin from child builds, and that's done by setting a environment variable to return an empty plugin for the child builds:

https://github.com/aklinker1/vite-plugin-web-extension/blob/3969f423b29b826c6b35491a0bf45e5b3219dcf0/packages/vite-plugin-web-extension/src/index.ts#L12-L15

Todo