diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index aab8fb18..8d25f027 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-vue - layout - log - log-file diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index 9daf1825..1e090a3e 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-vue - layout - log - log-file diff --git a/composer.json b/composer.json index 0216e968..bc00825c 100644 --- a/composer.json +++ b/composer.json @@ -148,6 +148,10 @@ "type": "path", "url": "packages/inertia-react" }, + { + "type": "path", + "url": "packages/inertia-vue" + }, { "type": "path", "url": "packages/layout" @@ -346,6 +350,7 @@ "marko/health": "self.version", "marko/inertia": "self.version", "marko/inertia-react": "self.version", + "marko/inertia-vue": "self.version", "marko/http": "self.version", "marko/layout": "self.version", "marko/http-guzzle": "self.version", @@ -453,6 +458,7 @@ "Marko\\Health\\Tests\\": "packages/health/tests/", "Marko\\Inertia\\Tests\\": "packages/inertia/tests/", "Marko\\Inertia\\React\\Tests\\": "packages/inertia-react/tests/", + "Marko\\Inertia\\Vue\\Tests\\": "packages/inertia-vue/tests/", "Marko\\Http\\Tests\\": "packages/http/tests/", "Marko\\Http\\Guzzle\\Tests\\": "packages/http-guzzle/tests/", "Marko\\Layout\\Tests\\": "packages/layout/tests/", diff --git a/docs/src/content/docs/packages/inertia-vue.md b/docs/src/content/docs/packages/inertia-vue.md new file mode 100644 index 00000000..066d6b85 --- /dev/null +++ b/docs/src/content/docs/packages/inertia-vue.md @@ -0,0 +1,109 @@ +--- +title: marko/inertia-vue +description: Vue 3 companion for marko/inertia - configuration defaults and a frontend marker binding for Vue. +--- + +Vue 3 companion for [`marko/inertia`](/docs/packages/inertia/) and [`marko/vite`](/docs/packages/vite/). It overlays the parent Inertia configuration with Vue defaults and registers a frontend marker binding so installing multiple Inertia frontend companions fails loudly. + +## Installation + +```bash +composer require marko/inertia-vue +``` + +Install the matching frontend dependencies in your app: + +```bash +npm install @inertiajs/vue3 vue @vue/server-renderer @vitejs/plugin-vue 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-vue/config/inertia.php" +return [ + 'assetEntry' => env('INERTIA_VUE_CLIENT_ENTRY', 'app/vue-web/resources/js/app.js'), +]; +``` + +| Key | Purpose | +| --- | --- | +| `assetEntry` | Vite entry used by browser-rendered Inertia responses. | + +## Usage + +Render Vue-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/vue-web/resources/js/app.js`: + +```js title="app/vue-web/resources/js/app.js" +import { createInertiaApp } from '@inertiajs/vue3'; +import { createApp, h } from 'vue'; + +createInertiaApp({ + resolve: (name) => { + const pages = import.meta.glob('./Pages/**/*.vue', { eager: true }); + return pages[`./Pages/${name}.vue`]; + }, + setup({ el, App, props, plugin }) { + createApp({ render: () => h(App, props) }).use(plugin).mount(el); + }, +}); +``` + +Create the SSR entry at `app/vue-web/resources/js/ssr.js`: + +```js title="app/vue-web/resources/js/ssr.js" +import { createInertiaApp } from '@inertiajs/vue3'; +import createServer from '@inertiajs/vue3/server'; +import { renderToString } from '@vue/server-renderer'; +import { createSSRApp, h } from 'vue'; + +createServer((page) => + createInertiaApp({ + page, + render: renderToString, + resolve: (name) => { + const pages = import.meta.glob('./Pages/**/*.vue', { eager: true }); + return pages[`./Pages/${name}.vue`]; + }, + setup({ App, props, plugin }) { + return createSSRApp({ render: () => h(App, props) }).use(plugin); + }, + }), +); +``` + +## API Reference + +This package registers `Marko\Inertia\Frontend\InertiaFrontendInterface` to a Vue 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 Vue Vite entry +- [`marko/env`](/docs/packages/env/) - provides the `env()` helper used in `config/inertia.php` diff --git a/packages/inertia-vue/.gitattributes b/packages/inertia-vue/.gitattributes new file mode 100644 index 00000000..aa78278f --- /dev/null +++ b/packages/inertia-vue/.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-vue/LICENSE b/packages/inertia-vue/LICENSE new file mode 100644 index 00000000..99a76f97 --- /dev/null +++ b/packages/inertia-vue/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-vue/README.md b/packages/inertia-vue/README.md new file mode 100644 index 00000000..298c37c1 --- /dev/null +++ b/packages/inertia-vue/README.md @@ -0,0 +1,36 @@ +# marko/inertia-vue + +Vue 3 companion for `marko/inertia` - configuration defaults and a frontend marker binding for Vue. + +## Installation + +```bash +composer require marko/inertia-vue +``` + +## 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-vue](https://marko.build/docs/packages/inertia-vue/) diff --git a/packages/inertia-vue/composer.json b/packages/inertia-vue/composer.json new file mode 100644 index 00000000..c69f6b73 --- /dev/null +++ b/packages/inertia-vue/composer.json @@ -0,0 +1,28 @@ +{ + "name": "marko/inertia-vue", + "description": "Vue 3 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\\Vue\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Marko\\Inertia\\Vue\\Tests\\": "tests/" + } + }, + "extra": { + "marko": { + "module": true + } + } +} diff --git a/packages/inertia-vue/config/inertia.php b/packages/inertia-vue/config/inertia.php new file mode 100644 index 00000000..09e3cb90 --- /dev/null +++ b/packages/inertia-vue/config/inertia.php @@ -0,0 +1,7 @@ + env('INERTIA_VUE_CLIENT_ENTRY', 'app/vue-web/resources/js/app.js'), +]; diff --git a/packages/inertia-vue/module.php b/packages/inertia-vue/module.php new file mode 100644 index 00000000..ab3ba738 --- /dev/null +++ b/packages/inertia-vue/module.php @@ -0,0 +1,12 @@ + [ + InertiaFrontendInterface::class => VueInertiaFrontend::class, + ], +]; diff --git a/packages/inertia-vue/src/VueInertiaFrontend.php b/packages/inertia-vue/src/VueInertiaFrontend.php new file mode 100644 index 00000000..9a3ce3c7 --- /dev/null +++ b/packages/inertia-vue/src/VueInertiaFrontend.php @@ -0,0 +1,15 @@ +toBe('app/vue-web/resources/js/app.js'); + expect($config)->not->toHaveKey('ssrEntry'); +}); + +test('inertia-vue binds the inertia frontend marker', function () { + $module = require dirname(__DIR__).'/module.php'; + + expect($module['bindings'])->toHaveKey(InertiaFrontendInterface::class) + ->and($module['bindings'][InertiaFrontendInterface::class])->toBe(VueInertiaFrontend::class); +}); + +test('vue inertia frontend identifies itself', function () { + expect((new VueInertiaFrontend())->name())->toBe('vue'); +}); + +test('inertia-vue 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\\Vue\\'); +});