Open nvh95 opened 2 years ago
Hey @ntt261298, please see my note above on a new approach to handle CSS. It would be great if you can give some of your thoughts. Thanks.
I think this approach is really promising. (Reduce the test running time, Be able to handle async tasks so we can scale easier in the future,…) I still have some thoughts about this approach:
postcssrc()
for every test. What if we process the CSS module in the Jest Preview Dashboard? Then it might take a much longer time for the browser to load CSS fully. (Not sure if the user has enough patience to wait for that). I think this is a trade-off.postcssrc()
might be the problem we need to solve for both approaches (current and new), but it’s probably the really hard one.To summarize, I think it’s feasible to implement the new approach and will bring more pros than cons.
[Just a note] This can fix #260
hi, is this on the roadmap? i would like to reduce the runtime in CI or make it only parse css based on some env variable. looks like this rewrite would solve that.
What is it?
Current approach to process CSS
document.head
ofjsdom
. "Jest Preview server" will servejsdom
as real DOM, so we can see the CSS on the "Jest Preview Dashboard". However, this approach encounters a number of problemsconsole.log
the output the stdout, then reading/ parsing to get the returned value. This is a tricky one and does not scale, and I considered it as a unscale code. https://github.com/nvh95/jest-preview/blob/d991e54382ae6e4c9fb697bdcc8a2b147cef8b23/src/transform.ts#L21-L42Is there any better solution
Yes. But first, let me describe how I come up with that idea.
A few days ago, I was working on #230. The issue is that when a CSS file that contains an
@import
statement,jest-preview
could not load the content of the imported file:If you are curious, I handled this in #248, by inline the imported CSS, using (
postcss-import
). But the thing is, when opening Jest Preview Dashboard myindex.css
will automatically request CSS files that are imported. (It's a CSS standard)Response:
This opens a new horizon for me to resolve all those issues above. What if instead of processing CSS at Jest Code Transformation, the "Jest Preview Dashboard" will process the CSS? I was in the middle of dinner with my family and I had to stop to think and draft the solution.
An HTTP-based approach
This is briefly how it would work:
jsdom
'sdocument.head
. Like this:relativeCssPath
from "Jest Preview Server"By doing this, we are moving the computation from
Jest
process to our "Jest Preview Server" process. Let's see the pros and cons Prospostinstall
script, after user installs/ updatesjest-preview
, see https://github.com/nvh95/jest-preview/issues/51#issuecomment-1221358730)postinstall
script, which is considered bad for "security". See https://socket.dev/npm/package/jest-previewCons
jsdom
does not contain CSS anymore. If someone wants to do some kinds of "visual testing" usingjest-preview/transforms/css
. It wouldn't be possible. (Or would it be? Can we reuseprocessCss
?)cache-control
/etag
) sides.Jest
to "Jest Preview Server".Are any caveats?
jest-preview
, despite the re-architecture, the user experience remains the same. I have only one concern that Jest Code Transform does not invalidate. But I personally think we can handle it by taking the code transformed itself into account ingetCacheKey
(https://github.com/nvh95/jest-preview/issues/51#issuecomment-1221358730).cssModulesExportedTokens
Describe alternatives you've considered
Do not implement this at all 😂. Even though the current (Jest Transformation) approach might be slow in CI, but it's not too slow and it's cache-able. If implementing this is too hard. We can just leave it AS-IS
Additional context
241
247