Skip to content

Commit 99b815e

Browse files
authored
feat: added sveltekit-usage guide (feature-sliced#698)
1 parent da2b6e6 commit 99b815e

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
# Usage with SvelteKit
5+
6+
It is possible to implement FSD in a SvelteKit project, but conflicts arise due to the differences between the structure requirements of a SvelteKit project and the principles of FSD:
7+
8+
- Initially, SvelteKit offers a file structure inside the `src/routes` folder, while in FSD the routing must be part of the `app` layer.
9+
- SvelteKit suggests putting everything not related to routing in the `src/lib` folder.
10+
11+
12+
## Let's set up the config
13+
14+
```ts title="svelte.config.ts"
15+
import adapter from '@sveltejs/adapter-auto';
16+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
17+
18+
/** @type {import('@sveltejs/kit').Config}*/
19+
const config = {
20+
preprocess: [vitePreprocess()],
21+
kit: {
22+
adapter: adapter(),
23+
files: {
24+
routes: 'src/app/routes', // move routing inside the app layer
25+
lib: 'src',
26+
appTemplate: 'src/app/index.html', // Move the application entry point inside the app layer
27+
assets: 'public'
28+
},
29+
alias: {
30+
'@/*': 'src/*' // Create an alias for the src directory
31+
}
32+
}
33+
};
34+
export default config;
35+
```
36+
37+
## Move file routing to `src/app`.
38+
39+
Let's create an app layer, move the app's entry point `index.html` into it, and create a routes folder.
40+
Thus, your file structure should look like this:
41+
42+
```sh
43+
├── src
44+
│ ├── app
45+
│ │ ├── index.html
46+
│ │ ├── routes
47+
│ ├── pages # Папка pages, закреплённая за FSD
48+
```
49+
50+
Now, you can create roots for pages within `app` and connect pages from `pages` to them.
51+
52+
For example, to add a home page to your project, you need to do the following steps:
53+
- Add a page slice inside the `pages` layer
54+
- Add the corresponding rooute to the `routes` folder from the `app` layer
55+
- Align the page from the slice with the rooute
56+
57+
To create a page slice, let's use the [CLI](https://github.com/feature-sliced/cli):
58+
59+
```shell
60+
fsd pages home
61+
```
62+
63+
Create a ``home-page.vue`` file inside the ui segment, access it using the Public API
64+
65+
```ts title="src/pages/home/index.ts"
66+
export { default as HomePage } from './ui/home-page';
67+
```
68+
69+
Create a root for this page inside the `app` layer:
70+
71+
```sh
72+
73+
├── src
74+
│ ├── app
75+
│ │ ├── routes
76+
│ │ │ ├── +page.svelte
77+
│ │ ├── index.html
78+
│ ├── pages
79+
│ │ ├── home
80+
│ │ │ ├── ui
81+
│ │ │ │ ├── home-page.svelte
82+
│ │ │ ├── index.ts
83+
```
84+
85+
Add your page component inside the `index.svelte` file:
86+
87+
```html title="src/app/routes/+page.svelte"
88+
<script>
89+
import { HomePage } from '@/pages/home';
90+
</script>
91+
92+
93+
<HomePage/>
94+
```
95+
96+
## See also.
97+
98+
- [Documentation on changing directory config in SvelteKit](https://kit.svelte.dev/docs/configuration#files)
99+
100+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
# Использование с SvelteKit
5+
6+
В SvelteKit проекте возможно реализовать FSD, однако возникают конфликты из-за различий между требованиями к структуре проекта SvelteKit и принципами FSD:
7+
8+
- Изначально, SvelteKit предлагает файловую структуру внутри папки `src/routes`, в то время как в FSD роутинг должен быть частью слоя `app`.
9+
- SvelteKit предлагает складывать всё, что не относится к роутингу в папку `src/lib`.
10+
11+
12+
## Настроим конфиг
13+
14+
```ts title="svelte.config.ts"
15+
import adapter from '@sveltejs/adapter-auto';
16+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
17+
18+
/** @type {import('@sveltejs/kit').Config}*/
19+
const config = {
20+
preprocess: [vitePreprocess()],
21+
kit: {
22+
adapter: adapter(),
23+
files: {
24+
routes: 'src/app/routes', // перемещаем роутинг внутрь app слоя
25+
lib: 'src',
26+
appTemplate: 'src/app/index.html', // Перемещаем входную точку приложения внутрь слоя app
27+
assets: 'public'
28+
},
29+
alias: {
30+
'@/*': 'src/*' // Создаём алиас для директории src
31+
}
32+
}
33+
};
34+
export default config;
35+
```
36+
37+
## Перемещение файлового роутинга в `src/app`
38+
39+
Создадим слой app, переместим в него входную точку приложения `index.html` и создадим папку routes.
40+
Таким образом, ваша файловая структура должна выглядеть так:
41+
42+
```sh
43+
├── src
44+
│ ├── app
45+
│ │ ├── index.html
46+
│ │ ├── routes
47+
│ ├── pages # Папка pages, закреплённая за FSD
48+
```
49+
50+
Теперь, вы можете создавать роуты для страниц внутри `app` и подключать к ним страницы из `pages`.
51+
52+
Например, чтобы добавить главную страницу в проект, вам нужно сделать следующие шаги:
53+
- Добавить слайс страницы внутри слоя `pages`
54+
- Добавить соответствующий роут в папку `routes` из слоя `app`
55+
- Совместить страницу из слайса с роутом
56+
57+
Для того чтобы создать слайс страницы, воспользуемся [CLI](https://github.com/feature-sliced/cli):
58+
59+
```shell
60+
fsd pages home
61+
```
62+
63+
Создайте файл `home-page.vue` внутри сегмента ui, откройте к нему доступ с помощью Public API
64+
65+
```ts title="src/pages/home/index.ts"
66+
export { default as HomePage } from './ui/home-page';
67+
```
68+
69+
Создайте роут для этой страницы внутри слоя `app`:
70+
71+
```sh
72+
73+
├── src
74+
│ ├── app
75+
│ │ ├── routes
76+
│ │ │ ├── +page.svelte
77+
│ │ ├── index.html
78+
│ ├── pages
79+
│ │ ├── home
80+
│ │ │ ├── ui
81+
│ │ │ │ ├── home-page.svelte
82+
│ │ │ ├── index.ts
83+
```
84+
85+
Добавьте внутрь `index.svelte` файла компонент вашей страницы:
86+
87+
```html title="src/app/routes/+page.svelte"
88+
<script>
89+
import { HomePage } from '@/pages/home';
90+
</script>
91+
92+
93+
<HomePage/>
94+
```
95+
96+
## См. также
97+
98+
- [Документация по изменению конфига директорий в SvelteKit](https://kit.svelte.dev/docs/configuration#files)

0 commit comments

Comments
 (0)