Skip to content

add own class name hashing #220

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

Merged
merged 27 commits into from
Apr 4, 2025
Merged

add own class name hashing #220

merged 27 commits into from
Apr 4, 2025

Conversation

jantimon
Copy link
Collaborator

@jantimon jantimon commented Nov 25, 2024

This PR is a first draft for minified CSS class names in production by generating deterministic but unique class names directly in the SWC plugin

The change will:

  • reduce the class name size in css file (upto 20% gzip file size savings)
  • reduce the ssr html size (because the class names are also used there)
  • reduce the js bundle size (because we can skip the mappings and the class names are also there)

The generated code from the SWC plugin changes from

import styleYak from "./input.yak.module.css"; 
styled.button(styleYak.Button);

to the following:

```tsx
import "./input.yak.module.css";
styled.button("Button_m7uBBu");

The PR will only work for Next.js versions with a postcss-modules-local-by-default version 4.1 or newer

Still todo:

  • Test if single line comments // work for postcss ignore markers
  • Update css-loader imports from cross-file selectors and mixins to use the new class naming scheme

Copy link

changeset-bot bot commented Nov 25, 2024

🦋 Changeset detected

Latest commit: 1ca44c9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
yak-swc Minor
next-yak Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codspeed-hq bot commented Nov 25, 2024

CodSpeed Performance Report

Merging #220 will not alter performance

Comparing custom-class-naming (7ffa69b) with main (07bed79)

Summary

✅ 2 untouched benchmarks

@jantimon jantimon force-pushed the custom-class-naming branch from 6eeed5a to 5a043c2 Compare November 25, 2024 12:49
@jantimon jantimon force-pushed the custom-class-naming branch from b80af04 to e6dfbfe Compare November 25, 2024 13:05
@jantimon
Copy link
Collaborator Author

jantimon commented Dec 12, 2024

This PR reduces CSS by over 20% it also reduces the js size

Overall it saves 10% after gzip for the example app (excluding next.js framework files)

size change

@jantimon jantimon force-pushed the custom-class-naming branch 2 times, most recently from 489d3fd to 8d1d4d9 Compare April 2, 2025 18:03
# Conflicts:
#	package.json
#	packages/cross-file-tests/__tests__/fixtures/constant/output/index.tsx
#	packages/cross-file-tests/__tests__/fixtures/namespaceConstant/output/index.tsx
#	packages/cross-file-tests/__tests__/fixtures/nestedMixin/output/icon.tsx
#	packages/cross-file-tests/__tests__/fixtures/nestedMixin/output/index.tsx
#	packages/cross-file-tests/__tests__/fixtures/sameFileMixin/output/index.tsx
#	packages/cross-file-tests/__tests__/fixtures/selector/output/icon.tsx
#	packages/cross-file-tests/__tests__/fixtures/selector/output/index.tsx
#	packages/cross-file-tests/__tests__/fixtures/staticMixin/output/index.tsx
#	packages/cross-file-tests/__tests__/fixtures/yak-file/output/index.tsx
#	packages/cross-file-tests/__tests__/fixtures/yakFileMixin/output/index.tsx
#	packages/yak-swc/yak_swc/src/yak_transforms.rs
#	packages/yak-swc/yak_swc/tests/fixture/attrs/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/attrs/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/basic-styled-component/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/basic-styled-component/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/comments/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/comments/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/complex-nested-structure/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/complex-nested-structure/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/constants/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/constants/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-constants/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-constants/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-mixin-dynamic-export/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-mixin-dynamic-export/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-mixin-export/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-mixin-export/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-mixin-import/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-mixin-import/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-selectors/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/cross-file-selectors/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-grid-layout/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-grid-layout/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-import-order/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-mixin-inline/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-mixin-inline/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-mixin-nested/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-mixin-nested/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-mixin/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-mixin/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-prop/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-prop/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-variables/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/css-variables/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/dynamic-prop-error/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/dynamic-prop-error/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/encoded/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/encoded/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/extending-styles/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/extending-styles/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframe-animations-function/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframe-animations-function/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframe-animations-object/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframe-animations-object/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframe-animations/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframe-animations/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframes-nested-error/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/keyframes-nested-error/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/media-queries/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/media-queries/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/mixing-with-tailwind-error/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/mixing-with-tailwind-error/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/mixing-with-tailwind/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/mixing-with-tailwind/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/nested-dynamic-mixin/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/nested-dynamic-mixin/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/nested-selectors/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/nested-selectors/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/parens/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/parens/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/props-and-dynamic-styles/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/props-and-dynamic-styles/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/styled-componets/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/styled-componets/output.prod.tsx
#	packages/yak-swc/yak_swc/tests/fixture/theming-with-context/output.dev.tsx
#	packages/yak-swc/yak_swc/tests/fixture/theming-with-context/output.prod.tsx
#	pnpm-lock.yaml
@jantimon jantimon force-pushed the custom-class-naming branch from 8d1d4d9 to ec4c658 Compare April 2, 2025 18:06
@jantimon jantimon marked this pull request as ready for review April 2, 2025 18:41
@jantimon
Copy link
Collaborator Author

jantimon commented Apr 2, 2025

This PR requires

next 15.3.0-canary.30 (for CI pipelines)
next 15.3.0 (for stable release)

@jantimon jantimon requested a review from Mad-Kat April 3, 2025 06:03
@jantimon jantimon requested review from Mad-Kat and chklauser April 3, 2025 14:52
Copy link
Collaborator

@Mad-Kat Mad-Kat left a comment

Choose a reason for hiding this comment

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

LGTM

@jantimon jantimon merged commit d018370 into main Apr 4, 2025
6 checks passed
@jantimon jantimon deleted the custom-class-naming branch April 4, 2025 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants