Closed
Conversation
Contributor
|
FWIW, I think this is a good idea and I have an immediate use case for this. I would love to see this land with opt-in support. |
Member
Author
|
Main issue is that it breaks any kind of code splitting. If a var is used in a different file, or code splitting occurs, it will see the variable as unused and remove it. Haven't figured out what to do about this yet. |
|
Would it be possible to have a version of this for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a draft to get feedback. Please let me know what you think in the comments.
I'm not sure if this is a good idea, but I would like a way to remove CSS variables that aren't referenced. For example, in a design system you might have a ton of variables and only use a subset of them in a particular file. For example:
becomes:
This is very unsafe, so would need to be opt-in. Variable names are global so could be referenced from another CSS file, or from JavaScript. Also code splitting might break it if the variables were defined in a different bundle from where they are used. So this would basically only work if you are building a library and guarantee that only a single CSS file will be output, on which you can run this optimization pass.
Alternatives
I am hoping there is a better way. One idea I had was to make variables kinda like CSS modules. The names would get hashed, and if you wanted to reference a variable from a different file, you'd need to do so explicitly.
vars.css:
component.css:
compiled output:
Unfortunately, that syntax gets pretty annoying pretty fast if you have a lot of variables. If anyone has any ideas, let me know!