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

Cosmiconfig fails to load ES module web-ext config #54

Closed Bauke closed 1 year ago

Bauke commented 1 year ago

Was playing around a bit with v2 and while migrating the web-ext config to web-ext-config.js, I found that Cosmiconfig can't load ES modules:

Error [ERR_REQUIRE_ESM]: require() of ES Module web-ext-config.js
from node_modules/.pnpm/cosmiconfig@7.0.1/node_modules/cosmiconfig/dist/loaders.js not supported.
aklinker1 commented 1 year ago

Yeah, this is a limitation of cosmiconfig. I'm gonna switch to rc or run-con, mainly because it will merge all the config files up to your home directory, which is useful for specifying browser binaries and other defaults for all your projects.

But that will "fix" this by not allowing JS config files at all.

What's your use case?

aklinker1 commented 1 year ago

Edit: I don't like that rc and run-config don't support file extensions, so I might roll my own config loader. It will only support JSON and YML. Maybe I'll try and support JS files, but we'll see.

Bauke commented 1 year ago

So before v2 I used webExtConfig to do browser-specific config based on an environment variable. As webExtConfig is no longer available in v2 I figured I could just copy and paste the code over to web-ext-config.js, only to be met with the error above.

I did figure out a different way of doing it though: generate the configuration inside Vite's config (same as v1) and then write it to .web-extrc.json.

In any case, I've worked out how to migrate everything once v2 releases and I'm looking forward to it.

aklinker1 commented 1 year ago

Sorry I never responded, I'll add the inline config option back. Like you pointed out, sometimes it make sense to commit some config.

Instead of a DIY solution, nuxt uses c12, and that loads cjs, esm, json, rc files, etc. Seems like that's a good tool that could solve all these issues.

aklinker1 commented 1 year ago

This should be fixed by https://github.com/aklinker1/vite-plugin-web-extension/pull/44/commits/f613684ea46033526c185b883813f46cc504126b. I added back the webExtConfig option and I added config file support for only YAML and JSON.

I wasn't able to get cosmiconfig, config, nor c12 to work exactly how I wanted, so I rolled my own. It's not perfect, but it works exactly how I want it to. I'll need more feedback from people to decide where to go from here.

aklinker1 commented 1 year ago

V2 is almost ready to be released. I published RC1, and I'm about to publish RC2.

aklinker1 commented 1 year ago

This is no longer an issue with v2 or v3, I ended up rolling my own config, and adding the webExtConfig option back.