Great solution! One thing that i'm still struggling with, i can't seem to use regular classes in my svelte files like <div class="bg-red-200">...</div>. It works if i rerun sapper dev. If i use @apply inside tag it works fine. Btw i added inside _layout.svelte. Any idea how to get this working without having to rerun npm run dev every time?
Thanks for the information! In fact, using process.env.NODE_ENV in both setups would be better. However since process.env.ROLLUP_WATCH is used to determine mode in rollup.config.js, I wanted to be consistent with it. I've changed it to process.env.NODE_ENV in Sapper template.
Thanks for sharing that. I'm having issues with responsive classes such as sm:block. Using it throws an error saying that class doesn't exist. Are others able to use responsive classes with the approach explained in this article?
Thanks for your response. I cloned your repo and changed App.svelte's style to the following:
h1{@applybg-blacktext-whitemd:text-red-200;}
This throws the same error I see in my project:
[!] (plugin svelte) CssSyntaxError: /home/mikenikles/dev/github/sarioglu/svelte-tailwindcss-template/src/App.svelte:3:3: `@apply` cannot be used with `.md\:text-red-200` because `.md\:text-red-200` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that `.md\:text-red-200` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.
src/App.svelte
Are you able to use responsive classes such as md:text-red-200?
I'm afraid it is a limitation of tailwindcss. It cannot @apply variants directly. You can use it directly or find a workaround at tailwindcss.com/docs/functions-and...
Thanks for sharing. I ran into a small issue where the production flag in the postcss config is always false and therefore purgecss is not invoked. The NODE_ENV is "development" even when running npm run build in my case. I am on windows and I am resolving this by changing the build script to set NODE_ENV=production && sapper build --legacy. With that purgecss is invoked and everything is fabulous, thanks again.
Same for me too. I didn't mention that because I couldn't find any official information whether it is an expected behavior or not. Thank you for mentioning.
Was a Civ Eng. then I dropped it and taught my self coding, marketing and design. Currently working at an international company doing Conversion Optimization and SEO. And also creating my own bizz.
Software hacker working in the industry since 2003. Currently loves: #Typescript and #Svelte and #golang. Founder of Chimera, the first makerspace in northern California.
Thank you! I've updated the template according to your comments. Issue with html and body was happening because index.html is in public folder. Purgecss config now checks every folder under project directory.
Thanks a lot for this wonderful article! Much appreciated!
I included it in Sapper by following exactly your description and then, instead of importing and including it in src/App.svelte, I just included it in src/routes/_layout.svelte. This way it should be included in all pages rendered by Sapper.
Works like a charm and, as you mentioned, no additional workflow side-by-side with Sapper/Svelte.
Thanks for making these templates. I noticed after switching the sapper template to typescript with node scripts/setupTypeScriptRollup.js,Tailwind stopped working. The TS script creates some conflicts in rollup.config.js. They are easy to fix though.
remove the extra sveltePreprocess import
in both client and server, in the svelte() call, remove the extra preprocess: sveltePreprocess() lines that got added
I am brand new to Svelte, so might have something wrong, but this seemed to both get me TypeScript and Tailwind.
Thank you Matt! I've noticed this issue. scripts/setupTypeScriptRollup.js does not check if there is an existing sveltePreprocess. So it results with a silent failure. Your fix is correct. I am going to examine the script deeper to find a solution.
Thank you so much for sharing this!
Got my build time down from 2:20 to just 19 seconds!
One note: noticing that my dev builds are slower than my production ones, i found that (at least for me) always executing purgecss gave me even better timings: 8 seconds!
i.e. replaced ...(production ? [purgecss] : []
with purgecss
Thank you for sharing your results! Glad to help you out.
I think it worths to disable Purgecss in development. I frequently use DevTools to add/remove some styles to get an instant preview. Therefore, I can sacrifice some seconds for better development experience 🙂
and try to use it with @apply, then I see something like the following:
`@apply` cannot be used with `.body-12-caps` because `.body-12-caps` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that `.body-12-caps` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.
i think this is great! i made a recipe blog based on the sapper at master-tailwind method you mentioned above. i would like to try this method instead. do you have any plans on coming out with sapper-tailwind template on github? i can't figure out which goes into the server and which into client configs as you said. i'm a noob with those sort of things and more of a UI/UX guy. thank you for this! i'll try it out on a svelte project. cheers!
It's just a minor annoyance, but since I've made those changes, the bundle.js.map file gets send twice on every request. Don't know why really. Everything else seems to be working great tho. :)
Great solution! One thing that i'm still struggling with, i can't seem to use regular classes in my svelte files like inside _layout.svelte. Any idea how to get this working without having to rerun npm run dev every time?
<div class="bg-red-200">...</div>. It works if i rerun sapper dev. If i use @apply inside tag it works fine. Btw i addedI'm not sure why it is happening. I will check this issue when I create the sapper template.
I faced the same problem but fixed it with the following change.
Change line12 in postcss.config.js
from : const production = !process.env.ROLLUP_WATCH
to : const production = process.env.NODE_ENV !=='development'
Thanks for the information! In fact, using
process.env.NODE_ENVin both setups would be better. However sinceprocess.env.ROLLUP_WATCHis used to determine mode inrollup.config.js, I wanted to be consistent with it. I've changed it toprocess.env.NODE_ENVin Sapper template.Thanks for sharing that. I'm having issues with responsive classes such as
sm:block. Using it throws an error saying that class doesn't exist. Are others able to use responsive classes with the approach explained in this article?Hi Mike,
Thanks for your comment. Unfortunately I couldn't reproduce this issue. But it can be caused by one of following reasons:
Purgessconfig. You can refer to tailwindcss.com/docs/controlling-f... for correct usage.responsivevariant may be disabled in your tailwind config. You can solve it using tailwindcss.com/docs/display#respo...Thanks for your response. I cloned your repo and changed
App.svelte's style to the following:This throws the same error I see in my project:
Are you able to use responsive classes such as
md:text-red-200?I'm afraid it is a limitation of
tailwindcss. It cannot@applyvariants directly. You can use it directly or find a workaround at tailwindcss.com/docs/functions-and...Thank you, that helped fix the issue.
@screenand regular Tailwind utility classes works as documented.Thanks for sharing. I ran into a small issue where the production flag in the postcss config is always false and therefore purgecss is not invoked. The NODE_ENV is "development" even when running
npm run buildin my case. I am on windows and I am resolving this by changing the build script toset NODE_ENV=production && sapper build --legacy. With that purgecss is invoked and everything is fabulous, thanks again.Same for me too. I didn't mention that because I couldn't find any official information whether it is an expected behavior or not. Thank you for mentioning.
This still happens even though I'm using
set NODE_ENV=production && sapper export --legacyI find this super weird. Not sure what to do at all.The only way for me to avoid having all the classes in production is to use
purgecssin both dev and prod. What a bummerWhat about?
Ahh, simply.
This is great! Thank you!
Note:
If anyone facing css linting warning in
@tailwindkeyword, just addtype="text/postcss".You just solve a whole day of headache. I could kiss you!
I had to add a few bolts to make it work fully for me:
/html/and/body/to thewhitelistPatterns(purgecss remove style tohtmlorbodyfor some reason)emitCss: trueto thesvelte-loaderoptions when using webpack (allow me to use postcss syntaxe in svelte component)Thank you! I've updated the template according to your comments. Issue with
htmlandbodywas happening becauseindex.htmlis inpublicfolder. Purgecss config now checks every folder under project directory.Thanks a lot for this wonderful article! Much appreciated!
I included it in Sapper by following exactly your description and then, instead of importing and including it in
src/App.svelte, I just included it insrc/routes/_layout.svelte. This way it should be included in all pages rendered by Sapper.Works like a charm and, as you mentioned, no additional workflow side-by-side with Sapper/Svelte.
Thanks man!
Thanks for making these templates. I noticed after switching the sapper template to typescript with
node scripts/setupTypeScriptRollup.js,Tailwind stopped working. The TS script creates some conflicts in rollup.config.js. They are easy to fix though.preprocess: sveltePreprocess()lines that got addedI am brand new to Svelte, so might have something wrong, but this seemed to both get me TypeScript and Tailwind.
Thank you Matt! I've noticed this issue.
scripts/setupTypeScriptRollup.jsdoes not check if there is an existingsveltePreprocess. So it results with a silent failure. Your fix is correct. I am going to examine the script deeper to find a solution.Thank you so much for sharing this!
Got my build time down from 2:20 to just 19 seconds!
One note: noticing that my dev builds are slower than my production ones, i found that (at least for me) always executing purgecss gave me even better timings: 8 seconds!
i.e. replaced
...(production ? [purgecss] : []with
purgecssThank you for sharing your results! Glad to help you out.
I think it worths to disable
Purgecssin development. I frequently use DevTools to add/remove some styles to get an instant preview. Therefore, I can sacrifice some seconds for better development experience 🙂This doesn't seem to work for adding custom utilities: tailwindcss.com/docs/adding-new-ut...
If I try to add a new utility class, e.g.:
and try to use it with
@apply, then I see something like the following:i think this is great! i made a recipe blog based on the sapper at master-tailwind method you mentioned above. i would like to try this method instead. do you have any plans on coming out with sapper-tailwind template on github? i can't figure out which goes into the server and which into client configs as you said. i'm a noob with those sort of things and more of a UI/UX guy. thank you for this! i'll try it out on a svelte project. cheers!
Thanks for your reply. I'm planning to create the sapper version soon.
Great article! I'm also using
.prettierrc.jsonand.eslintrc.jsonfrom this article.Thank you!! I found the previous Tailwind implementation a bit lackluster, I'm excited to give it a try again!
A better approach indeed!
thank you so much for this! working perfectly...
Any workaround to make it work with svelte-vscode?
It's just a minor annoyance, but since I've made those changes, the bundle.js.map file gets send twice on every request. Don't know why really. Everything else seems to be working great tho. :)
This doesn't remove unused tailwindcss in dev mode but should remove it when building, right?
Thanks so much for sharing!! Have you done or are you aware of any public recipe which used this svelte-tw-template?
thank you! this is a lot of help!
everything works fine but important modifier like !bg-red-100 doesn't work.why and how can i fix it?
Something is wrong with your sapper-template. When exporting, builds are 2 or 3 megs because the extra Tailwind CSS is not getting purged.
Need extra config now, Tailwind added purgecss. tailwindcss.com/docs/controlling-f...