diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 8d25f027..0d891c92 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -83,6 +83,7 @@ body: - http-guzzle - inertia - inertia-react + - inertia-svelte - inertia-vue - layout - log diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index 1e090a3e..3d16ab77 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -71,6 +71,7 @@ body: - http-guzzle - inertia - inertia-react + - inertia-svelte - inertia-vue - layout - log diff --git a/composer.json b/composer.json index bc00825c..376835e5 100644 --- a/composer.json +++ b/composer.json @@ -148,6 +148,10 @@ "type": "path", "url": "packages/inertia-react" }, + { + "type": "path", + "url": "packages/inertia-svelte" + }, { "type": "path", "url": "packages/inertia-vue" @@ -350,6 +354,7 @@ "marko/health": "self.version", "marko/inertia": "self.version", "marko/inertia-react": "self.version", + "marko/inertia-svelte": "self.version", "marko/inertia-vue": "self.version", "marko/http": "self.version", "marko/layout": "self.version", @@ -458,6 +463,7 @@ "Marko\\Health\\Tests\\": "packages/health/tests/", "Marko\\Inertia\\Tests\\": "packages/inertia/tests/", "Marko\\Inertia\\React\\Tests\\": "packages/inertia-react/tests/", + "Marko\\Inertia\\Svelte\\Tests\\": "packages/inertia-svelte/tests/", "Marko\\Inertia\\Vue\\Tests\\": "packages/inertia-vue/tests/", "Marko\\Http\\Tests\\": "packages/http/tests/", "Marko\\Http\\Guzzle\\Tests\\": "packages/http-guzzle/tests/", diff --git a/docs/src/content/docs/packages/inertia-svelte.md b/docs/src/content/docs/packages/inertia-svelte.md new file mode 100644 index 00000000..4cb01581 --- /dev/null +++ b/docs/src/content/docs/packages/inertia-svelte.md @@ -0,0 +1,107 @@ +--- +title: marko/inertia-svelte +description: Svelte companion for marko/inertia - configuration defaults and a frontend marker binding for Svelte. +--- + +Svelte companion for [`marko/inertia`](/docs/packages/inertia/) and [`marko/vite`](/docs/packages/vite/). It overlays the parent Inertia configuration with Svelte defaults and registers a frontend marker binding so installing multiple Inertia frontend companions fails loudly. + +## Installation + +```bash +composer require marko/inertia-svelte +``` + +Install the matching frontend dependencies in your app: + +```bash +npm install @inertiajs/svelte svelte @sveltejs/vite-plugin-svelte vite +``` + +Refer to the [Inertia.js docs](https://inertiajs.com/) for currently supported versions of each frontend adapter. + +## Configuration + +This package contributes defaults to the parent `config/inertia.php` namespace: + +```php title="packages/inertia-svelte/config/inertia.php" +return [ + 'assetEntry' => env('INERTIA_SVELTE_CLIENT_ENTRY', 'app/svelte-web/resources/js/app.js'), +]; +``` + +| Key | Purpose | +| --- | --- | +| `assetEntry` | Vite entry used by browser-rendered Inertia responses. | + +## Usage + +Render Svelte-backed Inertia pages without passing an asset entry; `marko/inertia` reads it from configuration: + +```php +use Marko\Inertia\Inertia; +use Marko\Routing\Http\Request; +use Marko\Routing\Http\Response; + +class DashboardController +{ + public function __construct( + private readonly Inertia $inertia, + ) {} + + public function index(Request $request): Response + { + return $this->inertia->render( + request: $request, + component: 'Dashboard', + ); + } +} +``` + +Create the client entry at `app/svelte-web/resources/js/app.js`: + +```js title="app/svelte-web/resources/js/app.js" +import { createInertiaApp } from '@inertiajs/svelte'; +import { mount } from 'svelte'; + +createInertiaApp({ + resolve: (name) => { + const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true }); + return pages[`./Pages/${name}.svelte`]; + }, + setup({ el, App, props }) { + mount(App, { target: el, props }); + }, +}); +``` + +Create the SSR entry at `app/svelte-web/resources/js/ssr.js`: + +```js title="app/svelte-web/resources/js/ssr.js" +import { createInertiaApp } from '@inertiajs/svelte'; +import createServer from '@inertiajs/svelte/server'; +import { render } from 'svelte/server'; + +createServer((page) => + createInertiaApp({ + page, + resolve: (name) => { + const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true }); + return pages[`./Pages/${name}.svelte`]; + }, + setup({ App, props }) { + return render(App, { props }); + }, + }), +); +``` + +## API Reference + +This package registers `Marko\Inertia\Frontend\InertiaFrontendInterface` to a Svelte marker implementation. Installing more than one Inertia frontend companion produces the same binding conflict protection used by Marko driver siblings. + +## Related Packages + +- [`marko/inertia`](/docs/packages/inertia/) - renders Inertia responses and handles SSR fallback +- [`marko/vite`](/docs/packages/vite/) - resolves the configured Svelte Vite entry +- [`marko/env`](/docs/packages/env/) - provides the `env()` helper used in `config/inertia.php` diff --git a/packages/inertia-svelte/.gitattributes b/packages/inertia-svelte/.gitattributes new file mode 100644 index 00000000..aa78278f --- /dev/null +++ b/packages/inertia-svelte/.gitattributes @@ -0,0 +1,5 @@ +/tests export-ignore +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/phpunit.xml.dist export-ignore diff --git a/packages/inertia-svelte/LICENSE b/packages/inertia-svelte/LICENSE new file mode 100644 index 00000000..99a76f97 --- /dev/null +++ b/packages/inertia-svelte/LICENSE @@ -0,0 +1,20 @@ +MIT License + +Copyright (c) Devtomic LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/inertia-svelte/README.md b/packages/inertia-svelte/README.md new file mode 100644 index 00000000..2ca0a8f0 --- /dev/null +++ b/packages/inertia-svelte/README.md @@ -0,0 +1,36 @@ +# marko/inertia-svelte + +Svelte companion for `marko/inertia` - configuration defaults and a frontend marker binding for Svelte. + +## Installation + +```bash +composer require marko/inertia-svelte +``` + +## Quick Example + +```php +use Marko\Inertia\Inertia; +use Marko\Routing\Http\Request; +use Marko\Routing\Http\Response; + +class DashboardController +{ + public function __construct( + private readonly Inertia $inertia, + ) {} + + public function index(Request $request): Response + { + return $this->inertia->render( + request: $request, + component: 'Dashboard', + ); + } +} +``` + +## Documentation + +Full usage, API reference, and examples: [marko/inertia-svelte](https://marko.build/docs/packages/inertia-svelte/) diff --git a/packages/inertia-svelte/composer.json b/packages/inertia-svelte/composer.json new file mode 100644 index 00000000..3640b57b --- /dev/null +++ b/packages/inertia-svelte/composer.json @@ -0,0 +1,28 @@ +{ + "name": "marko/inertia-svelte", + "description": "Svelte companion for marko/inertia - configuration and frontend marker binding", + "license": "MIT", + "type": "marko-module", + "require": { + "php": "^8.5", + "marko/core": "self.version", + "marko/env": "self.version", + "marko/inertia": "self.version", + "marko/vite": "self.version" + }, + "autoload": { + "psr-4": { + "Marko\\Inertia\\Svelte\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Marko\\Inertia\\Svelte\\Tests\\": "tests/" + } + }, + "extra": { + "marko": { + "module": true + } + } +} diff --git a/packages/inertia-svelte/config/inertia.php b/packages/inertia-svelte/config/inertia.php new file mode 100644 index 00000000..ffbaf168 --- /dev/null +++ b/packages/inertia-svelte/config/inertia.php @@ -0,0 +1,7 @@ + env('INERTIA_SVELTE_CLIENT_ENTRY', 'app/svelte-web/resources/js/app.js'), +]; diff --git a/packages/inertia-svelte/module.php b/packages/inertia-svelte/module.php new file mode 100644 index 00000000..d2d12f8c --- /dev/null +++ b/packages/inertia-svelte/module.php @@ -0,0 +1,12 @@ + [ + InertiaFrontendInterface::class => SvelteInertiaFrontend::class, + ], +]; diff --git a/packages/inertia-svelte/src/SvelteInertiaFrontend.php b/packages/inertia-svelte/src/SvelteInertiaFrontend.php new file mode 100644 index 00000000..ba618b77 --- /dev/null +++ b/packages/inertia-svelte/src/SvelteInertiaFrontend.php @@ -0,0 +1,15 @@ +toBe('app/svelte-web/resources/js/app.js'); + expect($config)->not->toHaveKey('ssrEntry'); +}); + +test('inertia-svelte binds the inertia frontend marker', function () { + $module = require dirname(__DIR__).'/module.php'; + + expect($module['bindings'])->toHaveKey(InertiaFrontendInterface::class) + ->and($module['bindings'][InertiaFrontendInterface::class])->toBe(SvelteInertiaFrontend::class); +}); + +test('svelte inertia frontend identifies itself', function () { + expect((new SvelteInertiaFrontend())->name())->toBe('svelte'); +}); + +test('inertia-svelte is a marko module', function () { + $composer = json_decode( + file_get_contents(dirname(__DIR__).'/composer.json'), + true, + flags: JSON_THROW_ON_ERROR, + ); + + expect(file_exists(dirname(__DIR__).'/module.php'))->toBeTrue() + ->and($composer['extra']['marko']['module'])->toBeTrue() + ->and($composer['autoload']['psr-4'])->toHaveKey('Marko\\Inertia\\Svelte\\'); +});