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 through jestPreviewConfigure, externalCss option #71

Closed
nvh95 opened this issue Apr 23, 2022 · 3 comments
Closed

Support CSS Modules through jestPreviewConfigure, externalCss option #71

nvh95 opened this issue Apr 23, 2022 · 3 comments
Assignees
Labels
feature request Request a new feature help wanted Extra attention is needed

Comments

@nvh95
Copy link
Owner

nvh95 commented Apr 23, 2022

Is your feature request related to a problem? Please describe.
#40 supports CSS Modules through Code transformation. However, if users have some CSS Modules files and want to configure them through jestPreviewConfigure, it is not possible.

Describe the solution you'd like
Support CSS Modules though jestPreviewConfigure, externalCss option

Describe how should jest-preview implements this feature
Apply logic to transform CSS Modules from processCSSModules (

function processCSSModules(src: string, filename: string): string {
return `const postcss = require('postcss');
const PostcssModulesPlugin = require('postcss-modules');
const cssSrc = ${JSON.stringify(src)};
class Token {
set(json) {
Object.keys(json).forEach((key) => {
this[key] = json[key];
});
}
}
const exportedTokens = new Token();
postcss(
PostcssModulesPlugin({
getJSON: (cssFileName, json, outputFileName) => {
exportedTokens.set(json);
},
}),
)
.process(cssSrc, { from: ${JSON.stringify(filename)} })
.then((result) => {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(result.css));
document.body.appendChild(style);
});
module.exports = exportedTokens;`;
}
) to jestPreviewConfigure.

Since the logic to transform code and process externalCss are similar (Sass, CSS Modules). Can we extract to reuse the logic? (Likely Sass can, not sure if it's possible with CSS Modules)

@nvh95 nvh95 added feature request Request a new feature help wanted Extra attention is needed labels Apr 23, 2022
@nvh95
Copy link
Owner Author

nvh95 commented Apr 23, 2022

Sass support already did this via #52

if (cssFile.endsWith('.scss') || cssFile.endsWith('.sass')) {
const cssDestinationFile = destinationFile.replace(
/\.(scss|sass)$/,
'.css',
);
// Transform sass to css and save to cache folder
// We use exec instead of sass.compile because running sass.compile in jsdom environment cause unexpected behavior
// What we encountered is that filename is automatically added `http://localhost` as the prefix
// Example: style.scss => http://localhost/style.scss
// As a result, sass.compile cannot find the file
exec(
`./node_modules/.bin/sass ${cssFile} ${cssDestinationFile} --no-source-map`,
(err: any) => {
if (err) {
console.log(err);
}
},
);
return;
}

@nvh95 nvh95 self-assigned this May 2, 2022
@nvh95
Copy link
Owner Author

nvh95 commented May 28, 2022

We can close this once #124 is closed

@nvh95
Copy link
Owner Author

nvh95 commented Jun 12, 2022

We can import CSS Modules directly

// src/setupTest.js
import './global.module.css';

Reference: #132

@nvh95 nvh95 closed this as completed Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request a new feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant