Tutorial Creating Way-Too-Mainstream with SvelteKit

· updated

Hello! I want to share how I created this blog - Way Too Mainstream, with SvelteKit and Svelte.

Create a SvelteKit app

npm create svelte@latest my-app

You will answer a few questions for the tools to be included. I chose to not install the type script thing and the test thing because I personally don't use them.

Follow the messages after the create command

Let's change the current directory to our newly created app and install the app:

cd my-app
npm install

And that's it. You now have a functional web app. Run this command to start the local server if you want to test it out.

npm run dev

Update: starting from July 2022, Svelte switched Vite CLI. Therefore the default port becomes 5173. You can change the default dev port by setting the port value in the Vite config file.

vite.config.js

...
const config = {
  plugins: [sveltekit()],
  server: {
    port: 3000
  }
};
...

Open the browser and go to: http://localhost:3000 to see your website.

It is also a good idea to start a Git repository now.

git init
git add -A
git commit -m "Initial commit"

Install TailwindCSS

We will follow the official guide to install TailwindCSS as a PostCSS plugin, because we will be using Vite as our package manager (this is the default for SvelteKit as of writing).

Install PostCSS and TailwindCSS:

npm install -D tailwindcss postcss autoprefixer svelte-preprocess

Create the config file:

npx tailwindcss init tailwind.config.cjs -p

Update svelte.config.js

Add these config: svelte.config.js

...
import preprocess from "svelte-preprocess";
...
const config = {
  ...
  kit: {...},
  preprocess: [
    preprocess({
      postcss: true,
    }),
  ],
}

Configure template paths

In tailwind.conifg.cjs

module.exports = {
  content: ['./src/**/*.{html,js,svelte,ts}'],
  theme: {
    extend: {}
  },
  plugins: []
};

Add the Tailwind directives to your CSS

Create a file src/app.css, then add @tailwind directives for each of Tailwind’s layers.

@tailwind base;
@tailwind components;
@tailwind utilities;

Import the CSS file

Create a file src/routes/__layout.svelte, then import the app.css file we just created.

Update: since August 21 2022, this file is now src/routes/+layout.svelte

<script>
  import "../app.css";
</script>

<slot />

Start using TailwindCSS

Update src/routes/index.svelte

Update: since August 21 2022, this file is now src/routes/+page.svelte

<h1 class="text-3xl font-bold underline">
  Hello world!
</h1>

You should see the styles being applied!

Install Tailwind plugins (optional)

Run this command to install the official TailwindCSS plugins:

npm install -D @tailwindcss/typography @tailwindcss/forms @tailwindcss/line-clamp @tailwindcss/aspect-ratio

then add the plugins to tailwind.config.js

plugins: [
    require('@tailwindcss/typography'),
    require('@tailwindcss/forms'),
    require('@tailwindcss/line-clamp'),
    require('@tailwindcss/aspect-ratio'),
]

Now you should be able to use Tailwind plugins like prose and line-clamp in your project!

Developed with ❤️ by Aska