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

Support CSS Modules's jest code transformation #40

Merged
merged 13 commits into from Apr 14, 2022
Merged

Support CSS Modules's jest code transformation #40

merged 13 commits into from Apr 14, 2022

Conversation

nvh95
Copy link
Owner

@nvh95 nvh95 commented Apr 9, 2022

Features

  • To support CSS Modules

@nvh95 nvh95 linked an issue Apr 9, 2022 that may be closed by this pull request
@nvh95
Copy link
Owner Author

nvh95 commented Apr 9, 2022

Getting blocked since we cannot write async logic in jest code transformation if we are using CommonJS.

Reference:
jestjs/jest#11458 (comment)
jestjs/jest#11081 (comment)

@nvh95 nvh95 changed the title Css modules Support CSS Modules Apr 9, 2022
@nvh95
Copy link
Owner Author

nvh95 commented Apr 9, 2022

We can try a different approach. We can move code to transform CSS Modules into the transformed file itself. However, I have some concerns about performance, since compiling CSS Modules moves from build time to run time. (But now we don't have any better solutions)

@nvh95 nvh95 changed the title Support CSS Modules Support CSS Modules's jest code transformation Apr 9, 2022
@nvh95
Copy link
Owner Author

nvh95 commented Apr 12, 2022

Successfully support CSS Modules.
The hardest problem is transforming CSS Modules is a kind of async task, which is:

  • Not Supported by jest CommonJS
  • Cannot export async in CommonJS as well.

The solution is to use https://github.com/schiehll/postcss-modules-sync. However, the docs is not correct.
This commit nvh95/postcss-modules-sync@c4dc878 is to correct the incorrect instruction in README.md

P.S: This is frustrating to support CSS Modules but after solving it. It's rewarding!!!!!!!

@nvh95 nvh95 requested a review from ntt261298 April 13, 2022 01:25
@nvh95
Copy link
Owner Author

nvh95 commented Apr 13, 2022

Published CSS Modules support in https://www.npmjs.com/package/jest-preview/v/0.1.1-alpha.0

src/transform.ts Outdated
Comment on lines 87 to 107
const postcss = require('postcss');
const CSSModulesSync = require('postcss-modules-sync').default;
const cssSrc = ${JSON.stringify(src)};

let exportedTokens = {};

const result = postcss(
CSSModulesSync({
getJSON: (tokens) => {
exportedTokens = tokens;
},
}),
)
.process(cssSrc, { from: ${JSON.stringify(filename)} })

const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(result.css));
document.body.appendChild(style);

module.exports = exportedTokens`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think creating an indent here would be better and consistent

Copy link
Owner Author

Choose a reason for hiding this comment

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

This is intentional since transformed code shouldn't have an indentation at every line.
(However, in the first line we there is a redundant blank line, I will remove this)

Copy link
Owner Author

Choose a reason for hiding this comment

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

We don't have consistency across transform functions. Let me make them consistent. Thanks

@@ -16,6 +18,7 @@ function App() {
<img src={logo2} className="logo2" alt="logo2" />
<p>Vite Example</p>
<StyledText>This text is styled by styled-components</StyledText>
<p className={styles.green}>This text is styled by CSS Modules</p>
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should add this for CRA example

Copy link
Owner Author

Choose a reason for hiding this comment

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

@ntt261298 Sure. Actually. I'm working on #41 and I was about to integrate it into this PR as well. But I think we can just update CRA example and put it to another PR. Will update CRA example.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Update: I couldn't find a way to configure jest in package.json to work with CSS Modules. I think we can just leave it there, and wait for #41 to resolve this problem

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.

Support CSS Modules
2 participants