|
1 |
| -# svelte |
2 |
| -Guide for Svelte + TailwindCSS + Vite + GitHub Pages |
| 1 | +<!-- <img|title here> --> |
| 2 | +<!-- shield.io, 'project built with' --> |
| 3 | + |
| 4 | +"[Svelte](https://svelte.dev/) is a tool for building web applications. Like other user interface frameworks, it allows you to build your app _declaratively_ out of components that combine markup, styles, and behaviours." |
| 5 | + |
| 6 | +"[SvelteKit](https://kit.svelte.dev/) is a framework for rapidly developing robust, performant web applications using Svelte. If you're coming from React, SvelteKit is similar to Next. If you're coming from Vue, SvelteKit is similar to Nuxt." |
| 7 | + |
| 8 | +This guide provides a first-hand experience on building a Svelte project using [SvelteKit](https://kit.svelte.dev/docs/introduction) + [Tailwind CSS](https://tailwindcss.com/docs/guides/sveltekit) and deploying it on [GitHub Pages](https://pages.github.com/). |
| 9 | + |
| 10 | +## 🛠️ Installation |
| 11 | + |
| 12 | +**1. Create your project.** |
| 13 | + |
| 14 | +```bash |
| 15 | + # terminal |
| 16 | + npm create svelte@latest project_name |
| 17 | + cd project_name |
| 18 | +``` |
| 19 | + |
| 20 | +**2. Install Tailwind CSS.** |
| 21 | + |
| 22 | +```bash |
| 23 | + # terminal |
| 24 | + npm install -D tailwindcss postcss autoprefixer |
| 25 | + npx tailwindcss init -p |
| 26 | +``` |
| 27 | + |
| 28 | +**3. Enable use of PostCSS in \<style> blocks.** <small>[reference here](https://tailwindcss.com/docs/guides/sveltekit)</small> |
| 29 | + |
| 30 | +```js |
| 31 | +// svelte.config.js |
| 32 | +import adapter from '@sveltejs/adapter-auto'; |
| 33 | +import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; |
| 34 | + |
| 35 | +/** @type {import('@sveltejs/kit').Config} */ |
| 36 | +const config = { |
| 37 | + kit: { |
| 38 | + adapter: adapter(), |
| 39 | + }, |
| 40 | + preprocess: vitePreprocess(), |
| 41 | +}; |
| 42 | + |
| 43 | +export default config; |
| 44 | +``` |
| 45 | + |
| 46 | +**4. Configure your template paths.** |
| 47 | + |
| 48 | +```js |
| 49 | +// tailwind.config.js |
| 50 | +/** @type {import('tailwindcss').Config} */ |
| 51 | +export default { |
| 52 | + content: ['./src/**/*.{html,js,svelte,ts}'], |
| 53 | + theme: { |
| 54 | + extend: {}, |
| 55 | + }, |
| 56 | + plugins: [], |
| 57 | +}; |
| 58 | +``` |
| 59 | + |
| 60 | +**5. Add the Tailwind directives to your CSS.** `./src/app.css` |
| 61 | + |
| 62 | +```css |
| 63 | +/* app.css */ |
| 64 | +@tailwind base; |
| 65 | +@tailwind components; |
| 66 | +@tailwind utilities; |
| 67 | +``` |
| 68 | + |
| 69 | +**6. Import the CSS file.** `./src/routes/+layout.svelte` |
| 70 | + |
| 71 | +```html |
| 72 | +<!-- +layout.svelte --> |
| 73 | +<script> |
| 74 | + import '../app.css'; |
| 75 | +</script> |
| 76 | + |
| 77 | +<slot /> |
| 78 | +``` |
| 79 | + |
| 80 | +**7. Start your build process.** |
| 81 | + |
| 82 | +```bash |
| 83 | + npm run dev |
| 84 | +``` |
| 85 | + |
| 86 | +**8. Start coding.** |
| 87 | + |
| 88 | +```html |
| 89 | +<h1 class="text-3xl font-bold underline">Hello world!</h1> |
| 90 | + |
| 91 | +<style lang="postcss"> |
| 92 | + :global(html) { |
| 93 | + background-color: theme(colors.gray.100); |
| 94 | + } |
| 95 | +</style> |
| 96 | +``` |
| 97 | + |
| 98 | +## 🗂️ File Structure |
| 99 | + |
| 100 | +Typical file structure. |
| 101 | + |
| 102 | +``` |
| 103 | +my-project/ |
| 104 | +├─ src/ |
| 105 | +│ ├─ lib/ |
| 106 | +│ │ ├─ server/ |
| 107 | +│ │ │ └─ [your server-only lib files] |
| 108 | +│ │ └─ [your lib files] |
| 109 | +│ ├─ params/ |
| 110 | +│ │ └── [your param matchers] |
| 111 | +│ ├─ routes/ |
| 112 | +│ │ ├─ +layout.svelte |
| 113 | +│ │ ├─ +page.svelte |
| 114 | +│ │ └─ [your routes] |
| 115 | +│ ├─ app.html |
| 116 | +│ ├─ app.css |
| 117 | +│ ├─ error.html |
| 118 | +│ ├─ hooks.client.js |
| 119 | +│ ├─ hooks.server.js |
| 120 | +│ └─ service-worker.js |
| 121 | +├─ static/ |
| 122 | +│ └─ [your static assets] |
| 123 | +├─ tests/ |
| 124 | +│ └─ [your tests] |
| 125 | +├─ package.json |
| 126 | +├─ svelte.config.js |
| 127 | +├─ tsconfig.json |
| 128 | +└─ vite.config.js |
| 129 | +``` |
| 130 | + |
| 131 | +## 🛫 How to deploy to GitHub Pages |
| 132 | + |
| 133 | +Deploying to github pages is totally up to you, be it through **[GitHub Actions](https://docs.github.com/en/actions/deployment/about-deployments/deploying-with-github-actions)**, or via **[gh-pages](https://www.npmjs.com/package/gh-pages)** without jekyll, or manually. |
| 134 | + |
| 135 | +> [!NOTE] |
| 136 | +> |
| 137 | +> - Take note of the specific configurations for your project before deploying it, otherwise, it won't work properly on production. Refer to the documentations for [Svelte](https://svelte.dev/docs/introduction) or [SvelteKit](https://kit.svelte.dev/docs/introduction). |
| 138 | +> - Also take note that [GitHub Pages](https://pages.github.com/) have limitations, it's free, yes, but it has a limit. |
| 139 | +
|
| 140 | +### ❗ via package without the use of Jekyll SSG ❗ |
| 141 | + |
| 142 | +**1. Install `gh-pages` package.** |
| 143 | + |
| 144 | +```bash |
| 145 | +npm install gh-pages --save-dev |
| 146 | +``` |
| 147 | + |
| 148 | +**2. Add `.nojekyll` file to your `static/` directory.** |
| 149 | + |
| 150 | +> [!IMPORTANT] |
| 151 | +> It is important that the **.nojekyll file** is present in the _gh-pages_ branch for the project deployment to work properly. |
| 152 | +
|
| 153 | +**3. Add `deploy` to your scripts.** |
| 154 | + |
| 155 | +```json |
| 156 | +{ |
| 157 | + "scripts": { |
| 158 | + "deploy": "npm run build && gh-pages -d build -t true" |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +`-t true` flag addition is based on this [discussion](https://github.com/tschaub/gh-pages/issues/315) regarding the the dotfiles (.nojekyll) not being pushed in the repo for deployment. |
| 164 | + |
| 165 | +**4. Create and configure a new branch for `gh-pages`.** |
| 166 | +I like to do this manually. If there is some automated way, feel free to let me know by any means. |
| 167 | + |
| 168 | +```bash |
| 169 | +git checkout --orphan gh-pages |
| 170 | +git reset --hard |
| 171 | +git commit --allow-empty -m 'commit_message' |
| 172 | +git push origin gh-pages |
| 173 | +``` |
| 174 | + |
| 175 | +**5. Publish the production build.** |
| 176 | + |
| 177 | +```bash |
| 178 | +npm run deploy |
| 179 | +``` |
| 180 | + |
| 181 | +### ❗ via manually configuring github pages settings ❗ |
| 182 | + |
| 183 | +**1. Create your project.** |
| 184 | +Start coding your project, either use a framework like React, Vue, or not. |
| 185 | + |
| 186 | +**2. Publish production build to GitHub.** |
| 187 | +Push your _production build_ to your github repo. After that, check if your `index.html` file is uploaded, since it is one of the core files needed for your website to work. |
| 188 | + |
| 189 | +**3. Configure your GitHub Pages on repo Settings.** |
| 190 | +Navigate to `Settings > Pages > Build and deployment`. Make sure the _Source_ says 'Deploy from a branch', and then configure the _Branch_ settings and change it to your branch with the files. |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +🌎 [kerbethecoder](https://kerbethecoder.com/) |
| 195 | +📫 krby.cnts@gmail.com |
| 196 | +📌 July 26, 2024 |
0 commit comments