richardtallent / vite-plugin-singlefile

Vite plugin for inlining JavaScript and CSS resources
MIT License
808 stars 53 forks source link

Doesn't work if `base` is set to a folder #56

Closed dumbmatter closed 1 year ago

dumbmatter commented 1 year ago

I'm working on a vite app that I am deploying to a subfolder, like http://example.com/my-app/

So I had set the base option in vite to "/my-app/".

Later I realized a single file bundle made sense, so I tried using this plugin. It failed silently by not inlining any CSS/JS but still deleting my CSS/JS files.

Deleting the base option (which doesn't really do anything when bundling to a single file, I think) fixes this problem. But I figure you should at least show an error message in the case where you notice a CSS/JS file, try to inline it in index.html, but actually the regex doesn't match anything so no replacement happens. Or, make the regex a little more broad so it supports arbitrary values in base.

dumbmatter commented 1 year ago

Also, thanks for the plugin, it is very useful :)

richardtallent commented 1 year ago

In the next version, I've updated base to undefined when using the "recommended" configuration. For anything more, I think I'd need a test repo, ideally forked from the official example.

dumbmatter commented 1 year ago

Thanks that works in most cases, but would cause a problem if someone used the inlinePattern option to only inline some resources.

Example: https://github.com/dumbmatter/vite-plugin-singlefile-example/tree/base is set to only inline CSS, and to use a custom "base" option which is important because not everything is inlined. This results in the CSS failing to be included in index.html on npm run build.

A better solution would be to handle replacing script/link tags when a value for "base" is present.

dumbmatter commented 1 year ago

Also I think it's a bit invasive to automatically override user-defined settings like "base" from vite.config.js unless you manually opt out with useRecommendedBuildConfig. Changing the built-in defaults for settings that the user has not specified is less invasive, but as a user I don't expect things I manually specify to be overwritten. It'd be better to at least show a warning if it's doing that (idk much about vite so I don't know how easy that is!).