Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add granular CSS Modules options #739

Merged

Conversation

timneutkens
Copy link
Contributor

@timneutkens timneutkens commented May 12, 2024

Change in this PR

Adds granular options to switch on/off CSS module scoping for CSS features that are prefixed by default in Lightning CSS currently. For example animation, grid, and custom identifiers.

let { code, map, exports } = transform({
  // ...
  cssModules: {
    animation: true,
    grid: true,
    customIdents: true,
  }
});

The reason this is useful is that for example Webpack's CSS modules implementation does not do prefixing of grid. If you're migrating to Lightning CSS, like we're doing in Next.js / Turbopack it's helpful to disable the grid prefixing feature to ensure people have a smooth experience migrating.

Last test failure

The PR is mostly done, just one more case with animation: false where it somehow uses CustomIdent which in turn means it still prefixes.

CleanShot 2024-05-12 at 23 12 33@2x

PR feedback

I'm not proficient in writing Rust currently, using working on these smaller changes in Turbopack and other deps of Turbopack to learn as well, let me know if anything should be changed 馃憤

Improved assert_eq failure reports in tests

While investigating mismatches it was quite hard to read the assert_eq for these outputs when they mismatch. Added pretty_assertions to add much better diffs for these cases:

Before:

CleanShot 2024-05-12 at 23 17 36@2x

After:

CleanShot 2024-05-12 at 23 18 21@2x

napi/src/lib.rs Outdated
@@ -713,6 +716,9 @@ fn compile<'i>(
Default::default()
},
dashed_idents: c.dashed_idents.unwrap_or_default(),
animation: c.animation.unwrap_or_default(),
grid: c.grid.unwrap_or_default(),
custom_idents: c.custom_idents.unwrap_or_default(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should probably default to true like below.

@@ -58,17 +58,22 @@ impl<'i> ToCss for AnimationName<'i> {
where
W: std::fmt::Write,
{
let css_module_animation_enabled =
dest.css_module.as_mut().map_or(false, |css_module| css_module.config.animation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dest.css_module.as_mut().map_or(false, |css_module| css_module.config.animation);
dest.css_module.as_ref().map_or(false, |css_module| css_module.config.animation);

.as_mut()
.map_or(false, |css_module| css_module.config.custom_idents);

dest.write_dashed_ident(&self.0, true, css_module_custom_idents_enabled)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_dashed_ident already checks the dashed_idents option. I'm not sure that the custom_idents option also needs to affect dashed idents as well?

@@ -58,17 +58,22 @@ impl<'i> ToCss for AnimationName<'i> {
where
W: std::fmt::Write,
{
let css_module_animation_enabled =
dest.css_module.as_mut().map_or(false, |css_module| css_module.config.animation);

match self {
AnimationName::None => dest.write_str("none"),
AnimationName::Ident(s) => {
if let Some(css_module) = &mut dest.css_module {
css_module.reference(&s.0, dest.loc.source_index)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need the check here too

@devongovett devongovett force-pushed the add/css-modules-granular-options branch from e38e6bd to 39f4311 Compare May 15, 2024 05:04
@devongovett devongovett force-pushed the add/css-modules-granular-options branch from 39f4311 to e5d9ca1 Compare May 15, 2024 05:08
@devongovett devongovett merged commit 83839a9 into parcel-bundler:master May 15, 2024
3 checks passed
@timneutkens timneutkens deleted the add/css-modules-granular-options branch May 15, 2024 07:36
kdy1 added a commit to vercel/turbo that referenced this pull request May 23, 2024
Reverts #8060

parcel-bundler/lightningcss#739 is now applied so we don't need this lint anymore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants