<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paul.42</title>
    <description>The latest articles on DEV Community by Paul.42 (@paul42).</description>
    <link>https://dev.to/paul42</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F465902%2F51e55e37-2a26-4fbb-b91e-7bafe1705751.jpeg</url>
      <title>DEV Community: Paul.42</title>
      <link>https://dev.to/paul42</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/paul42"/>
    <language>en</language>
    <item>
      <title>Svelte &amp; Tailwind Css, minimal install</title>
      <dc:creator>Paul.42</dc:creator>
      <pubDate>Sun, 13 Sep 2020 21:04:47 +0000</pubDate>
      <link>https://dev.to/paul42/svelte-tailwind-css-minimal-install-ia2</link>
      <guid>https://dev.to/paul42/svelte-tailwind-css-minimal-install-ia2</guid>
      <description>&lt;h1&gt;
  
  
  Purpose
&lt;/h1&gt;

&lt;p&gt;Trying to get a base line install of tailwind css and svelte working together with a minimal amount of steps. If I've made any mistakes or false assumptions, please reach out! if I've missed any steps let me know and I'll fix it up, my goal here is to help the community use a tool that I really enjoy (TailwindCss) but I'm still learning myself, so please bear with me!&lt;/p&gt;

&lt;h2&gt;
  
  
  Process
&lt;/h2&gt;

&lt;p&gt;1.) clone project from base &amp;amp; install dependencies: &lt;br&gt;
(you can replace svelte-tailwind-app with your own project name)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx degit sveltejs/template svelte-tailwind-app
cd svelte-tailwind-app
npm install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2.) Install tailwind&lt;br&gt;
&lt;code&gt;npm install tailwindcss --save-dev&lt;/code&gt;&lt;br&gt;
3.) Create Tailwind Config File&lt;br&gt;
&lt;code&gt;npx tailwindcss init&lt;/code&gt;&lt;br&gt;
this creates tailwind.config.js in project root with some defaults.&lt;br&gt;
4.) install svelte-preprocess: &lt;code&gt;npm install --save-dev svelte-preprocess&lt;/code&gt;&lt;br&gt;
5.) Add imports for tailwind and svelte-preprocess to &lt;code&gt;rollup.config.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
&lt;/span&gt;&lt;span class="gi"&gt;+import sveltePreprocess from 'svelte-preprocess';
+import tailwind from 'tailwindcss';
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;6.) Add plugins to bottom of svelte section, adding it just under the css object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;preprocess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;sveltePreprocess&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="na"&gt;postcss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                        &lt;span class="nx"&gt;tailwind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tailwind.config.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;7.) Create a new component under &lt;code&gt;src&lt;/code&gt;: &lt;code&gt;TailwindCss.svelte&lt;/code&gt; and put this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;global&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"postcss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;components&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;utilities&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(&lt;em&gt;footnote&lt;/em&gt; wondering why no 'ignore purgecss directives'? see below)&lt;br&gt;
and import and add this to your App.svelte:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;&amp;lt;script&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+import TailwindCss from './TailwindCss.svelte'
&lt;/span&gt;&lt;span class="gd"&gt;&amp;lt;/script&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+&amp;lt;TailwindCss/&amp;gt;
&lt;/span&gt;&lt;span class="err"&gt;//...rest&lt;/span&gt; of App.svelte
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;8.) If you want to test it, run &lt;code&gt;npm run dev&lt;/code&gt; and then edit 'App.svelte' to add a new class to the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-&amp;lt;h1&amp;gt;Hello {name}!&amp;lt;/h1&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+&amp;lt;h1 class="bg-green-500"&amp;gt;Hello {name}!&amp;lt;/h1&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 you should see the background color change to green in your browser.&lt;br&gt;
9.) The problem being that Tailwind carries around a large amount of classes to give you such a great experience, we don't want to ship that on production sites. if you look in the inspect area and network tab you can see that &lt;code&gt;bundle.css&lt;/code&gt; is up to 1.7 MB, that's a bit much for a background, so lets add some purgecss&lt;br&gt;
10.) since about tailwind v1.4.0 you don't need to set it up manually, you can follow &lt;a href="https://tailwindcss.com/docs/controlling-file-size#removing-unused-css"&gt;directions here to edit your tailwind.config.js&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+const production = !process.env.ROLLUP_WATCH;
&lt;/span&gt;&lt;span class="gd"&gt;-purge: [],
&lt;/span&gt;&lt;span class="gi"&gt;+purge: {
+content: [
+"./src/**/*.svelte",
+"./public/**/*.html"
+],
+css: ["./public/**/*.css"],
+enabled: production // disable purge in dev
+}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you'll notice that if you run &lt;code&gt;npm run dev&lt;/code&gt; you don't see any change in &lt;code&gt;bundle.css&lt;/code&gt; but running &lt;code&gt;npm run build&lt;/code&gt; then &lt;code&gt;npm run start&lt;/code&gt; you can see the bundle.css size drop (for me it's down to 3.4kB, previously I had a typo here)&lt;/p&gt;

&lt;p&gt;I HIGHLY recommend the &lt;a href="https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss"&gt;vs-code plugin for tailwind css intellisense it helps me a ton&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Optional: if you don't want sirv to 'clear the console' during &lt;code&gt;npm run dev&lt;/code&gt; you can edit the rollup.config.js and add --no-clear to the params passed in like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
&lt;/span&gt;&lt;span class="gi"&gt;+server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev', '--no-clear'], {
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;if you don't want sirv to 'clear the console' during &lt;code&gt;npm run start&lt;/code&gt; edit your package.json:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-"start": "sirv public"
&lt;/span&gt;&lt;span class="gi"&gt;+"start": "sirv public --no-clear"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;if you wanted to change the port for sirv, this is where you'd put the &lt;code&gt;--host 6000&lt;/code&gt; or other port number&lt;/p&gt;

&lt;h2&gt;
  
  
  Acknowledgements:
&lt;/h2&gt;

&lt;p&gt;I couldn't have gotten nearly this good without the great articles by others: &lt;br&gt;
&lt;a href="https://dev.to/swyx/how-to-set-up-svelte-with-tailwind-css-4fg5"&gt;How to Set Up Svelte with Tailwind CSS&lt;/a&gt; by &lt;a href="https://dev.to/swyx"&gt;Shawn swyx Wang&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/sarioglu/using-svelte-with-tailwindcss-a-better-approach-47ph"&gt;Using Svelte with Tailwindcss - A better approach&lt;/a&gt; by &lt;a href="https://dev.to/sarioglu"&gt;sarioglu&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Footnotes
&lt;/h3&gt;

&lt;p&gt;*. I didn't put in the purgecss ignore directives because we know exactly where our index.html is - it's in public, not hidden in node_modules  - towards the bottom of the docs on &lt;a href="https://tailwindcss.com/docs/controlling-file-size#setting-up-purge-css-manually"&gt;Setting up purgecss manually&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* purgecss start ignore */
@tailwind base;
@tailwind components;
/* purgecss end ignore */

@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This will ensure you don't accidentally purge important base styles when working with frameworks like Next.js, Nuxt, vue-cli, create-react-app, and others that hide their base HTML template somewhere in your node_modules directory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;*. I didn't put the &lt;code&gt;@tailwind base;&lt;/code&gt; and other at rules into the &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; section of the App.svelte because it appeared to really slow down my builds - I'm far from an expert in rollup, so it could be just me.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>tailwindcss</category>
      <category>tech</category>
    </item>
  </channel>
</rss>
