<?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: Alan</title>
    <description>The latest articles on DEV Community by Alan (@alancwoo).</description>
    <link>https://dev.to/alancwoo</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%2F406701%2F74b03533-296f-47dd-b186-8932635a02b3.jpeg</url>
      <title>DEV Community: Alan</title>
      <link>https://dev.to/alancwoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alancwoo"/>
    <language>en</language>
    <item>
      <title>Getting started with Sapper, Svelte, Postcss &amp; Tailwind</title>
      <dc:creator>Alan</dc:creator>
      <pubDate>Thu, 11 Jun 2020 20:32:15 +0000</pubDate>
      <link>https://dev.to/alancwoo/sapper-svelte-postcss-tailwind-473i</link>
      <guid>https://dev.to/alancwoo/sapper-svelte-postcss-tailwind-473i</guid>
      <description>&lt;p&gt;Coming from the Vue ecosystem, where &lt;a href="https://cli.vuejs.org/"&gt;vue cli&lt;/a&gt; makes it a breeze to get up and running, it was a bit of a struggle coming to Sapper/Svelte and trying to put all the pieces together.&lt;/p&gt;

&lt;h1&gt;
  
  
  Goal
&lt;/h1&gt;

&lt;p&gt;A Sapper project with Postcss, Tailwind and the ability to import css files from node_modules packages and a single &lt;code&gt;npm run dev&lt;/code&gt; to get going.&lt;/p&gt;

&lt;p&gt;Many thanks to this article: &lt;a href="https://dev.to/inalbant/a-simpler-way-to-add-tailwindcss-to-your-svelte-project-11ja"&gt;A simpler way to add TailwindCSS to your Svelte project &lt;/a&gt; for an up to date way to add tailwind to a svelte project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Clone the starter template
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx degit "sveltejs/sapper-template#rollup" my-app
cd my-app
npm i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Install requirements
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --D svelte-preprocess rollup-plugin-svelte postcss-load-config postcss-import postcss-nested postcss-cli tailwindcss npm-run-all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Configure tailwind
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tailwind init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit the resulting &lt;code&gt;tailwind.config.js&lt;/code&gt; and add the required rules to the purgecss block to ensure svelte's styles are not removed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  purge: ["./src/**/*.svelte"],
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit &lt;code&gt;/static/global.css&lt;/code&gt; with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Configure PostCSS and svelte-preprocess
&lt;/h2&gt;

&lt;p&gt;In the root of your project, create and edit &lt;code&gt;postcss.config.js&lt;/code&gt; as follows (this will be used by the postcss-cli to compile the above global.css to index.css):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  plugins: [
    require("postcss-import"),
    require("postcss-nested"),
    require("tailwindcss")
  ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit &lt;code&gt;rollup.config.js&lt;/code&gt; by adding &lt;code&gt;import sveltePreprocess from 'svelte-preprocess';&lt;/code&gt; at the top of the file and instantiate like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const preprocess = sveltePreprocess({
  postcss: {
    plugins: [
      require('postcss-import')(),
      require('postcss-nested')()
    ]
  }
});

// Add the above

export default { ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;preprocess&lt;/code&gt; to the &lt;code&gt;svelte&lt;/code&gt; object in both the client and server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client: {
  ...
  plugins: [
    ...
    svelte({
      preprocess,
      dev,
      ...
},
server: {
  ...
  plugins: [
    ...
    svelte({
      preprocess,
      generate: 'ssr',
      ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Adjust run scripts
&lt;/h2&gt;

&lt;p&gt;In &lt;code&gt;/my-app/package.json&lt;/code&gt;, adjust the &lt;code&gt;scripts&lt;/code&gt; block as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "dev": "run-p start:dev watch:css",
  "build": "npm run build:css &amp;amp;&amp;amp; sapper build --legacy",
  "watch:css": "postcss static/global.css -o static/index.css -w",
  "build:css": "NODE_ENV=production postcss static/global.css -o static/index.css",
  "export": "sapper export --legacy",
  "start": "node __sapper__/build",
  "start:dev": "sapper dev",
  "cy:run": "cypress run",
  "cy:open": "cypress open",
  "test": "run-p --race dev cy:run"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, the adjusted &lt;code&gt;dev&lt;/code&gt; script uses &lt;code&gt;npm-run-all&lt;/code&gt; to run the tasks to compile tailwind and run sapper from the new &lt;code&gt;start:dev&lt;/code&gt; script. If you need to run sapper on another port, you can adjust it so: &lt;code&gt;"start:dev": "sapper dev --port 8080"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Adjust the css import in the template
&lt;/h2&gt;

&lt;p&gt;Edit &lt;code&gt;/src/template.html&lt;/code&gt; ahd replace &lt;code&gt;&amp;lt;link rel='stylesheet' href='global.css'&amp;gt;&lt;/code&gt; with &lt;code&gt;&amp;lt;link rel='stylesheet' href='index.css'&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Import css in svelte
&lt;/h2&gt;

&lt;p&gt;So this is where things get a bit complicated.&lt;/p&gt;

&lt;p&gt;Let's take &lt;a href="https://atomiks.github.io/tippyjs/"&gt;Tippy.js&lt;/a&gt; as an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D tippy.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add a tooltip to the default &lt;code&gt;index.svelte&lt;/code&gt; like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1 data-tippy-content="Hello World"&amp;gt;Great success!&amp;lt;/h1&amp;gt;
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To instantiate tippy, you have to do so inside an &lt;code&gt;onMount&lt;/code&gt; to prevent a &lt;code&gt;document is not defined&lt;/code&gt; error caused on the server-side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
  import { onMount } from "svelte";
  onMount(async () =&amp;gt; {
    const { default: tippy } = await import("tippy.js");
    tippy('[data-tippy-content]');
  });
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to import the relevant css, you would think to simply add &lt;code&gt;@import 'tippy.js/dist/tippy.css';&lt;/code&gt; to the top of your style block.&lt;/p&gt;

&lt;p&gt;However, this will result in endless &lt;code&gt;Unused CSS selector&lt;/code&gt; errors. The way around this is to set the style block to global like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style global&amp;gt;
  @import 'tippy.js/dist/tippy.css';
  ...
&amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, this now has the caveat that all styles within the block are now global, and you can have only one style block per component.&lt;/p&gt;

&lt;p&gt;Seeing as this css needs to be applied globally to avoid the compiler errors and being stripped, I think it makes the most sense to simply place the import in &lt;code&gt;global.css&lt;/code&gt; and allow the external &lt;code&gt;postcss-cli&lt;/code&gt; to take care of things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* /static/global.css */
@import 'tippy.js/dist/tippy.css';
@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, the svelte compiler doesn't need to worry about these css imports on each save, and from our setup, we have the functionality of postcss within our components (though we lose the hot reload functionality).&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Run &amp;amp; build
&lt;/h2&gt;

&lt;p&gt;A simple &lt;code&gt;npm run dev&lt;/code&gt; and you are ready to work. To build for production, &lt;code&gt;npm run build&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I'm completely new to svelte, so can't help but feel like this is being hacky – if anyone knows of a better way to handle things, please let me know.&lt;/p&gt;




&lt;p&gt;Versions used in this writeup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"postcss-cli": "^7.1.1",
"postcss-import": "^12.0.1",
"postcss-load-config": "^2.1.0",
"rollup-plugin-svelte": "^5.2.2",
"sapper": "^0.27.0",
"svelte": "^3.0.0",
"svelte-preprocess": "^3.9.7",
"tailwindcss": "^1.4.6",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Going Further
&lt;/h1&gt;

&lt;p&gt;I'd recommend &lt;a href="https://codechips.me/snowpack-svelte-typescript-tailwindcss/"&gt;Snowpack with Svelte, Typescript and Tailwind CSS is a very pleasant surprise&lt;/a&gt; and actually many other articles on &lt;a href="https://codechips.me/"&gt;Ilia Mikhailov's blog&lt;/a&gt; where he's been writing about svelte a fair bit lately.&lt;/p&gt;

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