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 Tailwindcss #127

Closed
wants to merge 1 commit into from

Conversation

sundaycrafts
Copy link
Contributor

Features

Fixes

Nothing

Chores

Nothing

Note

Context of this pull request.

#100 (comment)

@netlify
Copy link

netlify bot commented May 28, 2022

Deploy Preview for jest-preview-library canceled.

Name Link
🔨 Latest commit 483b684
🔍 Latest deploy log https://app.netlify.com/sites/jest-preview-library/deploys/6291c456092c850008307175

.then((result) => {
const style = document.createElement("style");
style.type = "text/css";
style.appendChild(document.createTextNode(result.css.replace(/\\\\/g, '')));
Copy link
Contributor Author

@sundaycrafts sundaycrafts May 28, 2022

Choose a reason for hiding this comment

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

This part result.css.replace(/\\\\/g, '') is hard to read even more regex is already complex.
It is parsed as result.css.replace(/\\/g, '')) on runtime.

Tailwindcss has CSS classes like .hover:text-red-500 {}. It will be compiled as .hover\:text-red-500 {}. But JSDom can't parse it so we have to remove that escaping character.

@tailwind components;
@tailwind utilities;

/* TODO: Redundant. Tailwind css inserts own reset css. */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

about Tailwinds' reset.css https://tailwindcss.com/docs/preflight

return {code: `
const postcss = require("postcss");
const cssSrc = ${JSON.stringify(src)};
postcss([require("tailwindcss")(), require("autoprefixer")()])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I want to use postcss-load-config but it can't because it seems that this code template will run on the browser environment so the module gives us itself as ESModule that Jest can't parse.

@nvh95
Copy link
Owner

nvh95 commented May 29, 2022

@sundaycrafts Thank you for your efforts in trying to make jest-preview works with TailwindCSS.
I will try to run it locally in a few days and give some feedback. But first, can you take a look at the test? it's currently failing

https://github.com/nvh95/jest-preview/runs/6636161961?check_suite_focus=true

@sundaycrafts
Copy link
Contributor Author

Ok sure!

@nvh95 nvh95 linked an issue May 29, 2022 that may be closed by this pull request
@ixartz
Copy link

ixartz commented Jun 13, 2022

@nvh95 and @sundaycrafts, both thank you for your work.

@nvh95 What is the update? I'm also highly interested by the Tailwind CSS support.

@nvh95
Copy link
Owner

nvh95 commented Jun 13, 2022

Hi @ixartz. Thanks for your kind words.

Supporting TailwindCSS is currently one of our goals in the next release. To support it well, we even need to rearchitect our internal a little bit (#133). Unfortunately, I am busy at the moment for at least the next 10 days. I will get to you once we have significant progress.

In the meantime, you can absolutely use TailwindCSS in your tests by this workaround:

  1. Build your CSS file using Tailwind CLI (reference step 4 https://tailwindcss.com/docs/installation)
npx tailwindcss -i ./src/index.css -o ./src/tailwind-output.css

Note: We are pretending index.css contains TailwindCSS directives such as:

@tailwind base;
@tailwind components;
@tailwind utilities;
  1. Import tailwind-output.css to jest setup file (configured by setupFilesAfterEnv in jest.config.js), for e.g: src/setupTests.js
// src/setupTests.js
import './tailwind-output.css';

This solution is not optimal since you need to re-generate tailwind-output.css everytime you update CSS, but it absolutely works while we are figuring out the best solution to support TailwindCSS out of the box.

Thank you. Please let me know if the above solution helps you.

@nvh95
Copy link
Owner

nvh95 commented Jun 13, 2022

Hi @sundaycrafts,

To support TailwindCSS, I need to adjust the architecture of CSS Code Transformation. So, after consideration, I decided the best solution is to include TailwindCSS in #133, to avoid conflicts and reduce the workload and time.

But no worries, I already cherry-picked your commit from this PR to #133 (5412539). You will get full credit for the efforts you spend on this PR. I will not forget to welcome you to contributors either.

I hope you understand. I will keep this PR open until the support for TailwindCSS gets stable.

If you have anything want to discuss, please let me know. Thank you very much.

@sundaycrafts
Copy link
Contributor Author

Hi @nvh95 , thank you for informing me that. I'm proud to contribute to you guys project and very happy that you respect it! I will continue to contribute to this project.

@nvh95
Copy link
Owner

nvh95 commented Jun 13, 2022

@sundaycrafts Wonderful! jest-preview still needs a lot of community support to reach a "stable" point. For example: as you already know, it's kind of hard to support TailwindCSS based on user's custom config (Now it's just the default config). I appreciate every help from you (in the past and in the future).

Don't hesitate to contact me on GitHub or via Twitter or Discord Channel if you need more in-depth discussion.

Thank you again.

@nvh95
Copy link
Owner

nvh95 commented Jun 13, 2022

@sundaycrafts @ixartz @hsheikhali1
FYI that Tailwind CSS support will land in 0.2.5 at the latest at the end of this month.
For now jest-preview@0.2.5-alpha.0 is out and it already supports BASIC TailwindCSS out of the box (I tried on a CRA project and it works fine). You can try it out as follow:

npm install jest-preview@alpha

Don't forget to import your global CSS file to jest setup file, for example:

// setupTest.js
import "./index.css"; // contains @tailwind base; @tailwind components; @tailwind utilities;

Please let me know if you guys spot any bugs. Thanks.

@nvh95
Copy link
Owner

nvh95 commented Jun 17, 2022

Included in #133
TODO task for @nvh95: https://github.com/nvh95/jest-preview/pull/127/files#r884083236

@nvh95 nvh95 closed this Jun 17, 2022
@nvh95
Copy link
Owner

nvh95 commented Jun 17, 2022

Thank you very much @sundaycrafts for your help. Your PR is so awesome. But to reduce the code conflicts, I decided to cherry-pick your commit to #133.

Enjoy React Summit 😉!

@nvh95
Copy link
Owner

nvh95 commented Jun 17, 2022

@all-contributors please add @sundaycrafts for code

@allcontributors
Copy link
Contributor

@nvh95

I've put up a pull request to add @sundaycrafts! 🎉

@nvh95 nvh95 mentioned this pull request Aug 18, 2022
3 tasks
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 Tailwind CSS
3 participants