-
Notifications
You must be signed in to change notification settings - Fork 563
RFC: Support for hashing more types of identifiers #390
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
Comments
Yes, I completely agree with everything
Yeah, anyway let's list them in spec.
:root {
--EgL3uq_accent-color: hotpink;
}
.EgL3uq_button {
background: var(--EgL3uq_accent-color);
} Already supported in webpack built-in CSS support (under experemental flag now) .button {
background: var(--accent-color from "./vars.module.css");
} This is not supported and use we should not use
I like it, too, will we still support
Actually we can, new spec allows it (but spec it still not fully updated in may places about it) https://w3c.github.io/csswg-drafts/css-syntax/#consume-style-block and https://w3c.github.io/csswg-drafts/css-syntax/#consume-declaration I find this syntax a bit cumbersome. @with --accent-color from "./vars.css" {
.foo {
--accent-color: purple;
}
} |
I saw that syntax listed in webpack/webpack#14893 as planned, but yeah
sure.
hmm from my reading of "consume a declaration" step 1 in the second link you shared, it still sounds like only |
I have seen the discussion before for such changed and as far as I understood .class {
fn(): something;
} was planned in support, but the spec is changing quite quickly so how could you see. Let's ask @tabatkins, If it turns out that this is not possible and is not planned anymore, then we will return to the discussion of your syntax |
"Planned in support" is overstating; in the "Option 3" version of Nesting (which the spec currently describes) we wanted to make sure that And we're planning to simplify that anyway, since it turns out to be easier than we thought - the weird carveout in the parsing algorithm will get removed in that case, and it'll just parse as a declaration if it can, and as a rule otherwise. It'll still fail to parse, either way, since we don't have functions as property names or bare functions in selectors currently. |
@tabatkins Thanky you for feedback 👍 @devongovett Apparently the only safe solution is using |
Yeah... that's a danger with adding really any syntax extension to css. I suppose we could namespace them, like Also |
Yeah, I agree, on the one hand it would be the right idea, on the other hand it is too long
Yeah, I guess we should take this and hope nothing bad happens 😄 |
One idea for Accessing globals and dependencies What do you think about combining some ideas from esm,
It could be a file wide import similar to esm e.g.: @import --baz {
src: url("./colors.css");
}
body {
background: --baz;
} in addition to @import --baz {
src: url("./colors.css");
type: fooBar;
} combining such an approach with multiple value lists similar to the
could allow importing multiple values from the same file: @import --baz, --bar, --foo {
src: url("./colors.css");
}
body {
background: --baz;
} I am not sure if overloading |
Hello! I maintain Lightning CSS, a CSS processor which implements CSS modules. Since the CSS modules "spec" was last updated, there have been many new features added to CSS itself, which I think could be affected by CSS modules hashing. I'd like to implement some of this in Lightning CSS, but want to get some consensus on the syntax here so that it remains interoperable between tools.
<custom-ident>
The CSS specification defines the
<custom-ident>
type for author-defined identifiers. This is used for keyframe animation names and theanimation-name
property, CSS grid line names,@container
names,@counter-style
names, and probably more future features. At the moment, CSS modules officially mentioned@keyframes
, but not these other features. I think all<custom-ident>
values across all features should be hashed consistently.This has been implemented in Lightning CSS (and perhaps some other implementations) for a while, but I think it would be good if we could update the spec to define it in terms of the
<custom-ident>
type so that future additions to CSS automatically get hashed.<dashed-ident>
The CSS spec also defines the
<dashed-ident>
type for cases where either a spec-defined or author defined identifier may be present. The leading--
allows these to be distinguished. This is most commonly used for CSS variables, but it is also used for other features like the@font-palette-values
,@color-profile
, and@custom-media
rules, as well as other upcoming specs.Lightning CSS currently implements support for local (hashed) CSS variables and dashed idents under an opt-in flag. I think this feature should also exist in the CSS modules spec so that it can be interoperable between tools. It seems to be in-line with the general CSS modules philosophy to scope all identifiers local to the file by default.
I believe there was a plan for webpack to also implement this at some point (according to @sokra), though I'm not sure what the current status of that is.
Accessing globals and dependencies
One problem with hashing custom and dashed idents is that there isn't a syntax to define or reference a global ident, or reference a
<custom-ident>
or<dashed-ident>
in a different file. With selectors, the:global
pseudo class can be used to define a global class or id, and thecomposes
property supports referencing classes defined in another file or globally with thecomposes: name from "..."
syntax. But with general custom or dashed idents this is not currently possible.The spec currently mentions using
:global(xxx)
to define a global@keyframes
rule. I think this syntax is kinda weird because it is not a selector, so the pseudo-class like syntax with the colon is out of place. It would also require changing the parsing of that rule from the standard CSS syntax. There is also no way to reference a hashedanimation-name
from a different file at the moment.The CSS variables implementation in Lightning CSS currently allows referencing a variable defined in a different file using a syntax similar to
composes
–var(--foo from "./bar.css")
orvar(--foo from global)
. However, there is currently no way to define a global custom property within a CSS module file, or update the value of a custom property defined in a different file. The--foo from global: "value";
syntax seems pretty strange for that, and also violates the CSS spec which requires property names to be valid identifiers.Proposal
Defining a global identifier
I propose adding an
@global
at-rule, which would make all selectors and identifiers defined within it global (non-hashed). This would enable defining global identifiers using other nested at-rules without changing their syntax.This could also be used to define global custom properties or selectors. Everything inside the
@global
rule would become global.In this case, both
.foo
and--global-var
would not be hashed. With CSS nesting, you could also define only the--global-var
as global and keep.foo
hashed.I think this should only affect declarations and not references. Referencing another value within an
@global
rule should still reference a local (hashed) name.In this case,
fade
would be hashed, but.foo
would not. To reference a global name or an identifier from a different file see below.Referencing an identifier
To reference a
<custom-ident>
or<dashed-ident>
from a different file, or from the global namespace, I propose adding animport()
function. This would accept syntax similar to thecomposes
property to define where to import from.The reason this is a function rather than simply inline (e.g.
animation-name: fade from global
) is to disambiguate cases wherefrom
andglobal
could be valid identifiers as well, for example if a property accepted a space separated list.We could also consider keeping
var(--accent-color from "./vars.css")
as a shortcut, butimport()
is a more general solution that works in more places.Re-defining custom properties from a different file
Since the CSS spec requires that all declarations have property names that are identifiers in order to parse, we cannot use
import()
in a property name. Defining a global custom property can be achieved as described above, but re-defining or updating the value of a custom property from a different file is not possible.To solve this, we could introduce an
@with
rule to "import" an identifier name from another file so it can be referenced as if it were local.This would cause the reference to
--accent-color
to be hashed as a dependency of "./vars.css".This could also be used as an alternative to an inline
import()
call as described above. Here is the same example rewritten to use@with
:This is sort of similar to
@value
but with a couple very important differences:<custom-ident>
or<dashed-ident>
are accepted.Grammar
This is the formal grammar proposed above, using the value definition syntax from the CSS specification.
Within a CSS module file,
<custom-ident>
and<dashed-ident>
would also change their grammar to supportimport()
.Conclusion
I think these additions to CSS modules could help it support modern CSS features like variables, animations, custom media queries, and more. I'm hoping we can get some consensus on the syntax here so that it remains interoperable between different tools. I'd love to hear your feedback!
The text was updated successfully, but these errors were encountered: