@@ -33,26 +33,11 @@ Then install the bundle using Composer and Symfony Flex:
3333 $ yarn install --force
3434 $ yarn watch
3535
36- You also need to add the following lines at the end to your ``assets/app.js `` file:
36+ The Flex recipe will automatically set things up for you, like adding
37+ ``.enableSvelte() `` to your ``webpack.config.js `` file and adding code
38+ to load your Vue components inside ``assets/app.js ``.
3739
38- .. code-block :: javascript
39-
40- // assets/app.js
41- import { registerSvelteControllerComponents } from ' @symfony/ux-svelte' ;
42-
43- // Registers Svelte controller components to allow loading them from Twig
44- //
45- // Svelte controller components are components that are meant to be rendered
46- // from Twig. These component can then rely on other components that won't be
47- // called directly from Twig.
48- //
49- // By putting only controller components in `svelte/controllers`, you ensure that
50- // internal components won't be automatically included in your JS built file if
51- // they are not necessary.
52- registerSvelteControllerComponents (require .context (' ./svelte/controllers' , true , / \. svelte$ / ));
53-
54- To make sure Svelte components can be loaded by Webpack Encore, you need to add and configure
55- the `svelte-loader `_ library in your project :
40+ Next, install a package to help Svelte:
5641
5742.. code-block :: terminal
5843
@@ -61,32 +46,33 @@ the `svelte-loader`_ library in your project :
6146 # or use yarn
6247 $ yarn add svelte svelte-loader --dev
6348
64- Enable it in your ``webpack.config.js `` file :
49+ That's it! Any files inside ``assets/svelte/controllers/ `` can now be rendered as
50+ Svelte components.
51+
52+ Usage
53+ -----
54+
55+ The Flex recipe will have already added the ``registerSvelteControllerComponents() ``
56+ code to your ``assets/app.js `` file:
6557
6658.. code-block :: javascript
6759
68- // webpack.config.js
69- Encore
70- // ...
71- .enableSvelte ()
72- ;
60+ // assets/app.js
61+ import { registerSvelteControllerComponents } from ' @symfony/ux-svelte' ;
7362
74- Usage
75- -----
63+ registerSvelteControllerComponents (require .context (' ./svelte/controllers' , true , / \\ . (j| t)sx? $ / ));
7664
77- UX Svelte works by using a system of ** Svelte controller components **: Svelte components that
78- are registered using `` registerSvelteControllerComponents() `` and that are meant to be rendered
79- from Twig.
65+ This will load all Svelte components located in the `` assets/svelte/controllers ``
66+ directory. These are known as ** Svelte controller components **: top-level
67+ components that are meant to be rendered from Twig.
8068
81- When using the ``registerSvelteControllerComponents() `` configuration shown previously, all
82- Svelte components located in the directory ``assets/svelte/controllers `` are registered as
83- Svelte controller components.
69+ You can render any Svelte controller component in Twig using the ``svelte_component() ``.
8470
85- You can then render any Svelte controller component in Twig using the `` svelte_component() `` function :
71+ For example :
8672
8773.. code-block :: javascript
8874
89- // assets/svelte/controllers/MyComponent .svelte
75+ // assets/svelte/controllers/Hello .svelte
9076 < script>
9177 export let name = " Svelte" ;
9278 < / script>
@@ -97,7 +83,7 @@ You can then render any Svelte controller component in Twig using the ``svelte_c
9783 .. code-block :: html+twig
9884
9985 {# templates/home.html.twig #}
100- <div {{ svelte_component('MyComponent ', { 'name': app.user.fullName }) }}></div>
86+ <div {{ svelte_component('Hello ', { 'name': app.user.fullName }) }}></div>
10187
10288If your Svelte component has a transition that you want to play on initial render, you can use
10389the third argument ``intro `` of the ``svelte_component() `` function like you would do with the
@@ -156,6 +142,5 @@ the Symfony framework:
156142https://symfony.com/doc/current/contributing/code/bc.html
157143
158144.. _`Svelte` : https://svelte.dev/
159- .. _`svelte-loader` : https://github.com/sveltejs/svelte-loader/blob/master/README.md
160145.. _`the Symfony UX initiative` : https://symfony.com/ux
161146.. _StimulusBundle configured in your app : https://symfony.com/bundles/StimulusBundle/current/index.html
0 commit comments