A Laravel frontend preset for initial Svelte scaffolding.
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.
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.
<!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>
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:
<my-test-component></my-test-component>
If you wish to register a custom component and use it within your blade.php
files, you can do it like so:
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>
webpack.mix.js
fileModifiy 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.
+ });
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;
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;
blade.php
file<!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" />
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email gal@wewowweb.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.