wewowweb/laravel-svelte-preset

Svelte frontend preset for Laravel.

v0.2.1 2022-06-02 11:53 UTC

This package is auto-updated.

Last update: 2024-03-30 00:37:29 UTC


README

A Laravel frontend preset for initial Svelte scaffolding.

Why?

Svelte is an interesting new approach in the JavaScript space, created by @Rich_Harris. While traditional frontend frameworks do the bulk of their work in the browser, Svelte does this in compilation step. They provide a fluid syntax for writing expressive code, but compile it down to small, framework-less vanilla JavaScript.

If you don't know what Svelte is, we highly recommend starting with Rich Harris' talk Rethinking Reactivity from YGLF Code Camp 2019, his introductory blog post or - if you're more of a hands-on type - Svelte's interactive tutorial.

This package is still in active development, so you might want to watch the repository to be notified of future changes.

Installation

You can install the package via composer:

composer require wewowweb/laravel-svelte-preset

After that, run the following command, which will provide you with the initial scaffolding of the project:

php artisan ui svelte

To install the JavaScript dependencies, run:

npm install && npm run dev

The package will provide you with the initial set of files:

  • /js/app.js
  • /js/components/App.svelte
  • webpack.mix.js

needed to start developing with Laravel & Svelte.

Usage

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  <head>
    ...
    <!-- Include the app.js file -->
    <script src="{{ mix('js/app.js') }}" defer></script>
  </head>
  <body>
    <!-- Include your App Component -->
    <App />
  </body>
</html>

Registering Custom Svelte Components

If you wish to use custom components, note you cannot use regular svelte components. Doing so will result in an invalid constructor error for the svelte component.

Please follow these general conventions when creating your custom components:

  • Component name must be two or more words joined by the '-' character e.g. 'my-test-component'.
  • Components can be accessed in blade file like a regular html tag e.g. <my-test-component></my-test-component>
  • Closing tag is necessary because its a web component.

If you wish to register a custom component and use it within your blade.php files, you can do it like so:

Step 1: Create a New Custom Component

Let's create a new Svelte Component (e.g. MyTestComponent.svelte)

<script></script>

<main>
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-md-8">
        <div class="card">
          <div class="card-header">My Test Component</div>
          <div class="card-body">
            I'm a test component.
          </div>
        </div>
      </div>
    </div>
  </div>
</main>

Step 2: Modify The webpack.mix.jsfile

Modifiy the webpack.mix.js file like so:

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css')
-   .svelte();
+   .svelte({
+       customElement: true,
+       tag: null // to get rid of "no custom element tag name" warning because we are defining components tag name it in app.js file. otherwise you would have to put "<svelte:options tag={null} />" in all of your custom elements.
+   });

Step 3: Import the component to your app.js

Then within your àpp.js file, import the MyTestComponent like so:

require('./bootstrap');

import App from "./components/App.svelte";
+ import MyTestComponent from "./components/MyTestComponent.svelte";

const app = new App({
  target: document.body
});

window.app = app;

+ customElements.define('my-test-component', MyTestComponent);
export default app;

Step 4: Convert your App component to a custom component

require('./bootstrap');

import App from './components/App.svelte';
import MyTestComponent from './components/MyTestComponent.svelte';

+ customElements.define('my-app', App);
customElements.define('my-test-component', MyTestComponent);

- const app = new App({
-     target: document.body,
- });

- window.app = app;
- export default app;

Step 5: Use the new component in your blade.phpfile

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        ...
        <!-- Include the app.js file -->
        <script src="{{ mix('js/app.js') }}" defer></script>
    </head>
    <body>
        <!-- Include your App Component -->
-       <App />
+       <my-app></my-app>
+       <my-test-component></my-test-component>
    </body>
</html>

Additionally, you may also define the tag within your svelte component instead of with customElement.define as so:

<svelte:options tag="my-test-component" />

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email gal@wewowweb.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.