-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Config option to opt out of CSS Injection #9465
Comments
I think this could be implemented as a Vite plugin at the meantime. We prefer to avoid adding another option unless it's not possible to be implemented as a plugin. Furthermore, it seems the feature request is only as a convenience to omit |
I know this is your stance, and I understand that for every non-configurable behavior, you have more flexibility internally to change things freely. However, as you've already allowed for certain CSS-specific configuration options, and since the default behavior of Vite here is very opiniated, I think this seems like something that would make a lot of sense to expose an option to disable
Adding Having the default behavior being one in which there is a non-obvious side-effect feels unnatural to me, personally, as I'd argue the more natural behavior for importing named bindings from modules is one in which there are no side-effects. I realize there is precedent for something like this across some bundlers, but when browsers eventually support importing stylesheets via the ESM loading mechanism, I think its probably safe to assume it will be free of side-effects and return a CSSStyleSheet that can be adopted by a root programmatically, which Constructable StyleSheets laid the groundwork for. But this is of course just my opinion. Happy New Year! |
Happy new year! 🙂
Vite is very strict with adding a new option too, so the rule has been if a feature can't be implemented as a plugin, then it should be an option. It's usually not added as a way to opt-out of it's opinionated nature.
I don't think you need to do that. You should be able to do something like: const esbuildCssInline = {
name: 'css-inline',
setup(build) {
build.onResolve({ filter: /\.css$/ }, ({ path }) => ({
path: path + '?inline',
external: true,
}))
}
}
Vite 4 has deprecated default imports from |
(Temporarily unlocked so further discussions are public from a DM) Re where to put the esbuild plugin, that should be put at So you might need this Vite plugin too: const plugin = {
name: 'css-inline',
enforce: 'pre',
resolveId(id, importer) {
if (id.endsWith('.css') { // or a more robust check if needed
return this.resolve(id + '?inline', importer)
}
}
} I'm not sure if the esbuild plugin is needed with this Vite plugin (not sure why I recommended esbuild too in the first place). Maybe some tinkering could get what you want. |
Thanks for your examples. You've made your stance clear, and I understand it. I do hope you will eventually expose this as a simple toggle to avoid asking users to implement one or more plugins to rewire the import mechanism for something that I'm confident will only continue to become the more dominant use case, considering the most likely standardization path. If a config option isn't on the table, a little explainer or example of how to achieve this behavior without manually adding the |
I'm still not really sure this would gain traction because this breaks CSS from deps by default. I suppose when/if Vite supports constructable stylesheets directly it would make more sense. |
Description
It is currently possible to opt out of CSS Injection by adding
?inline
to the relevant import declarations.However, for some applications, specifically those that rely on Constructable Stylesheets or more generally applications that rely on Shadow DOM, adding this to the end of every import feels unnecessarily redundant to me.
Suggested solution
It would be nice if a configuration option could be exposed to just never inject any CSS. There is already a few CSS-specific configuration options, and I propose adding an additional option
css.inject
which defaults totrue
Alternative
It can be worked around by adding
?inline
to every import declaration referencing a CSS file.Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: