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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(styled-components): Implement minification #235

Merged
merged 25 commits into from Nov 23, 2023

Conversation

ciffelia
Copy link
Contributor

@ciffelia ciffelia commented Nov 22, 2023

This implements minify option in styled-components plugin. The minification algorithm and unit tests are ported from babel-plugin-styled-components.

Note that CSS code in helper functions (keyframes, css, etc.) are currently not always processed. I think there is a bug in is_helper function, so I'll fix it in another pull request later.

fixes #143
cf. vercel/next.js#30802

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

I think we should use swc_css or lightningcss for minification.

(I prefer lightningcss, because turbopack is going to use it)

@ciffelia
Copy link
Contributor Author

I think we should use swc_css or lightningcss for minification.

(I prefer lightningcss, because turbopack is going to use it)

Lightningcss does not support line comments. swc_css supports line comments, but I doubt it works for all syntax styled-components supports. For example, swc_css might not be able to parse the following style into AST.

const Box = styled.div`
  animation: ${p => p.anim};
  ${p => p.type}: red;
  ${p => p.additionalRules};

  ${MyComponent} {
    color: red;
  }
`

// <Box animation="slidein 3s linear 1s" type="background-color" additionalRules="opacity: 0.1">foo</Box>

So I think using lightningcss or swc_css would cause regression against the regex-based method used in babel-plugin-styled-components.

@kdy1 kdy1 changed the title feat(styled-components): implement minification feat(styled-components): Implement minification Nov 23, 2023
@kdy1 kdy1 enabled auto-merge (squash) November 23, 2023 11:17
@kdy1 kdy1 merged commit 0538650 into swc-project:main Nov 23, 2023
6 checks passed
@ciffelia ciffelia deleted the feat-styled-components-minify branch November 23, 2023 12:19
@phiter
Copy link

phiter commented Nov 23, 2023

Do you know what might be needed to make minification work with the css helper? You mentioned it doesn't currently work and I tried it and only styled.* components get minified.

@phiter
Copy link

phiter commented Nov 23, 2023

Ah I just saw your other PR, looking forward to using that! Thank you.

@kdy1 kdy1 self-assigned this Mar 26, 2024
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.

styled-components minify doesn't work with provided target
3 participants