|
| 1 | +# Svelte |
| 2 | + |
| 3 | +Setting up Tailwind with svelte is really simple, just install Tailwind and pocstcss-cli: |
| 4 | + |
| 5 | +```sh |
| 6 | +npm install tailwindcss postcss-cli --save-dev |
| 7 | +``` |
| 8 | + |
| 9 | +If you want to remove unused styles, add PurgeCSS as well |
| 10 | + |
| 11 | +``` |
| 12 | +npm install @fullhuman/postcss-purgecss |
| 13 | +``` |
| 14 | + |
| 15 | +Create your Tailwind config file |
| 16 | + |
| 17 | +```sh |
| 18 | +./node_modules/.bin/tailwind init tailwind.js |
| 19 | +``` |
| 20 | + |
| 21 | +Create a `postcss.config.js` file and add this to it |
| 22 | + |
| 23 | +```js |
| 24 | +const tailwindcss = require("tailwindcss"); |
| 25 | + |
| 26 | +// only needed if you want to purge |
| 27 | +const purgecss = require("@fullhuman/postcss-purgecss")({ |
| 28 | + content: ["./src/**/*.svelte", "./public/**/*.html"], |
| 29 | + defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || [] |
| 30 | +}); |
| 31 | + |
| 32 | +module.exports = { |
| 33 | + plugins: [ |
| 34 | + tailwindcss("./tailwind.js"), |
| 35 | + |
| 36 | + // only needed if you want to purge |
| 37 | + ...(process.env.NODE_ENV === "production" ? [purgecss] : []) |
| 38 | + ] |
| 39 | +}; |
| 40 | +``` |
| 41 | + |
| 42 | +Next, create a CSS file for your Tailwind styles. You have to store in it public folder `public/tailwind.css` and add this to it : |
| 43 | + |
| 44 | +```css |
| 45 | +@tailwind base; |
| 46 | +@tailwind components; |
| 47 | +@tailwind utilities; |
| 48 | +``` |
| 49 | + |
| 50 | +Update your `package.json` with the custom scripts. |
| 51 | + |
| 52 | +`build:tailwind is only needed if you want to purge` |
| 53 | + |
| 54 | +```js |
| 55 | +"scripts": { |
| 56 | + "watch:tailwind": "postcss public/tailwind.css -o public/index.css -w", |
| 57 | + "build:tailwind": "NODE_ENV=production postcss public/tailwind.css -o public/index.css", |
| 58 | + "dev": "run-p start:dev autobuild watch:build", |
| 59 | + "build": "npm run build:tailwind && rollup -c", |
| 60 | + |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Finally, add a stylesheet link to your `public/index.html` file |
| 65 | + |
| 66 | +```html |
| 67 | +<link rel="stylesheet" href="index.css" /> |
| 68 | +``` |
| 69 | + |
| 70 | +## Project setup |
| 71 | + |
| 72 | +``` |
| 73 | +npm install |
| 74 | +``` |
| 75 | + |
| 76 | +### Compiles and hot-reloads for development |
| 77 | + |
| 78 | +``` |
| 79 | +npm run dev |
| 80 | +``` |
| 81 | + |
| 82 | +### Compiles and minifies for production |
| 83 | + |
| 84 | +``` |
| 85 | +npm run build |
| 86 | +``` |
0 commit comments