Skip to content

Commit a6a12aa

Browse files
authored
Merge pull request #15 from Medoo48/v2.x
Fix Inertia typos, add Svelte installation option
2 parents cda438a + af9a579 commit a6a12aa

File tree

9 files changed

+269
-3
lines changed

9 files changed

+269
-3
lines changed

src/ViewInstallCommand.php

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ protected function configure()
2121
->setDescription('Run a script in your composer.json')
2222
->addOption('blade', null, InputOption::VALUE_NONE, 'Install blade')
2323
->addOption('bare-ui', null, InputOption::VALUE_NONE, 'Install bare ui')
24-
->addOption('inerita', null, InputOption::VALUE_NONE, 'Setup inerita files')
24+
->addOption('inertia', null, InputOption::VALUE_NONE, 'Setup inertia files')
2525
->addOption('react', null, InputOption::VALUE_NONE, 'Install react')
26+
->addOption('svelte', null, InputOption::VALUE_NONE, 'Install svelte')
2627
->addOption('tailwind', null, InputOption::VALUE_NONE, 'Install tailwind')
2728
->addOption('vite', null, InputOption::VALUE_NONE, 'Setup vite files')
2829
->addOption('vue', null, InputOption::VALUE_NONE, 'Install vue')
@@ -39,14 +40,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3940
return $this->installBareUi($output);
4041
}
4142

42-
if ($input->getOption('inerita')) {
43+
if ($input->getOption('inertia')) {
4344
return $this->installInertia($input, $output);
4445
}
4546

4647
if ($input->getOption('react')) {
4748
return $this->installReact($input, $output);
4849
}
4950

51+
if ($input->getOption('svelte')) {
52+
return $this->installSvelte($input, $output);
53+
}
54+
5055
if ($input->getOption('tailwind')) {
5156
return $this->installTailwind($input, $output);
5257
}
@@ -157,7 +162,7 @@ protected function installBareUi($output)
157162
}
158163

159164
/**
160-
* Install inerita
165+
* Install inertia
161166
*/
162167
protected function installInertia($input, $output)
163168
{
@@ -320,6 +325,97 @@ protected function installReact($input, $output)
320325
return 0;
321326
}
322327

328+
/**
329+
* Install svelte
330+
*/
331+
protected function installSvelte($input, $output)
332+
{
333+
$output->writeln("📦 <info>Installing svelte...</info>\n");
334+
335+
$directory = getcwd();
336+
$npm = Utils\Core::findNpm($input->getOption('pm'));
337+
$composer = Utils\Core::findComposer();
338+
$success = Utils\Core::run("$npm add @leafphp/vite-plugin svelte @sveltejs/vite-plugin-svelte @inertiajs/svelte", $output);
339+
340+
if (!$success) {
341+
$output->writeln("❌ <error>Failed to install svelte</error>");
342+
return 1;
343+
};
344+
345+
$output->writeln("\n✅ <info>Svelte installed successfully</info>");
346+
$output->writeln("🧱 <info>Setting up Leaf Svelte server bridge...</info>\n");
347+
348+
$success = Utils\Core::run("$composer require leafs/inertia leafs/vite", $output);
349+
350+
if (!$success) {
351+
$output->writeln("❌ <error>Failed to setup Leaf Svelte server bridge</error>");
352+
return 1;
353+
};
354+
355+
$isMVCApp = $this->isMVCApp();
356+
$isBladeProject = $this->isBladeProject();
357+
$ext = $isBladeProject ? 'blade' : 'view';
358+
359+
if (!$isBladeProject) {
360+
$output->writeln("\n🎨 <info>Setting up BareUI as main view engine.</info>\n");
361+
$success = Utils\Core::run("$composer require leafs/bareui", $output);
362+
363+
if (!$success) {
364+
$output->writeln("❌ <error>Could not install BareUI, run leaf install bareui</error>\n");
365+
return 1;
366+
};
367+
}
368+
369+
\Leaf\FS::superCopy(__DIR__ . '/themes/svelte/root', $directory);
370+
371+
if ($isMVCApp) {
372+
$paths = require "$directory/config/paths.php";
373+
$viewsPath = trim($paths['views'] ?? 'app/views', '/');
374+
$routesPath = trim($paths['routes'] ?? 'app/routes', '/');
375+
376+
\Leaf\FS::superCopy(__DIR__ . '/themes/svelte/routes', "$directory/$routesPath");
377+
\Leaf\FS::superCopy(
378+
(__DIR__ . '/themes/svelte/views/' . ($isBladeProject ? 'blade' : 'bare-ui')),
379+
"$directory/$viewsPath"
380+
);
381+
} else {
382+
\Leaf\FS::superCopy(__DIR__ . '/themes/svelte/routes', $directory);
383+
\Leaf\FS::superCopy(
384+
(__DIR__ . '/themes/svelte/views/' . ($isBladeProject ? 'blade' : 'bare-ui')),
385+
$directory
386+
);
387+
388+
$viteConfig = file_get_contents("$directory/vite.config.js");
389+
$viteConfig = str_replace(
390+
"leaf({",
391+
"leaf({\nhotFile: 'hot',",
392+
$viteConfig
393+
);
394+
file_put_contents("$directory/vite.config.js", $viteConfig);
395+
396+
$inertiaView = file_get_contents("$directory/_inertia.$ext.php");
397+
$inertiaView = str_replace(
398+
'<?php echo vite([\'/js/app.jsx\', "/js/Pages/{$page[\'component\']}.jsx"]); ?>',
399+
'<?php echo vite([\'js/app.js\'], \'/\'); ?>',
400+
$inertiaView
401+
);
402+
file_put_contents("$directory/_inertia.$ext.php", $inertiaView);
403+
}
404+
405+
$package = json_decode(file_get_contents("$directory/package.json"), true);
406+
$package['type'] = 'module';
407+
$package['scripts']['dev'] = 'vite';
408+
$package['scripts']['build'] = 'vite build';
409+
file_put_contents("$directory/package.json", json_encode($package, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
410+
411+
$output->writeln("\n⚛️ <info>Svelte setup successfully</info>");
412+
$output->writeln("👉 Get started with the following commands:\n");
413+
$output->writeln(' leaf view:dev <info>- start dev server</info>');
414+
$output->writeln(" leaf view:build <info>- build for production</info>");
415+
416+
return 0;
417+
}
418+
323419
/**
324420
* Install tailwind
325421
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from "vite";
2+
import leaf from "@leafphp/vite-plugin";
3+
import { svelte } from '@sveltejs/vite-plugin-svelte';
4+
5+
export default defineConfig({
6+
plugins: [
7+
leaf({
8+
input: ['app/views/js/app.js', 'app/views/css/app.css'],
9+
refresh: true,
10+
}),
11+
svelte(),
12+
],
13+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
app()->get('/hello', function () {
4+
inertia('Hello');
5+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title inertia>Document</title>
8+
<?php echo vite(['/js/app.js']); ?>
9+
<?php
10+
if (!isset($__inertiaSsrDispatched)) {
11+
$__inertiaSsrDispatched = true;
12+
$__inertiaSsrResponse = (new \Leaf\Inertia\Ssr\Gateway())->dispatch($page);
13+
}
14+
15+
if ($__inertiaSsrResponse) {
16+
echo $__inertiaSsrResponse->head;
17+
}
18+
?>
19+
</head>
20+
21+
<body>
22+
<?php
23+
if (!isset($__inertiaSsrDispatched)) {
24+
$__inertiaSsrDispatched = true;
25+
$__inertiaSsrResponse = (new \Leaf\Inertia\Ssr\Gateway())->dispatch($page);
26+
}
27+
28+
if ($__inertiaSsrResponse) {
29+
echo $__inertiaSsrResponse->body;
30+
} else {
31+
echo '<div id="app" data-page="' . htmlspecialchars(json_encode($page), ENT_QUOTES, 'UTF-8', true) . '"></div>';
32+
}
33+
?>
34+
</body>
35+
36+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
export let auth;
3+
4+
let count = 0;
5+
6+
const increment = () => {
7+
count++;
8+
};
9+
</script>
10+
11+
<div>
12+
<h1>Hello World!</h1>
13+
<p>Current user: {auth?.user?.name ?? "No auth is active"}</p>
14+
<p>{count}</p>
15+
<button on:click={increment} class="bg-green-900">Increment</button>
16+
</div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createInertiaApp } from "@inertiajs/svelte";
2+
import { resolvePageComponent } from "@leafphp/vite-plugin/inertia-helpers";
3+
4+
const appName = import.meta.env.VITE_APP_NAME || "Leaf PHP";
5+
6+
createInertiaApp({
7+
title: (title) => `${title} - ${appName}`,
8+
resolve: (name) =>
9+
resolvePageComponent(`./Pages/${name}.svelte`,
10+
import.meta.glob('./Pages/**/*.svelte', { eager: true })
11+
),
12+
//or with persistent layouts
13+
// {// setting the default page layout
14+
// const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })
15+
// let page = pages[`./Pages/${name}.svelte`]
16+
// return { default: page.default, layout: page.layout || Layout }
17+
// },
18+
setup({ el, App, props, plugin }) {
19+
new App({ target: el, props })
20+
},
21+
progress: {
22+
color: "#4B5563",
23+
},
24+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title inertia>Document</title>
8+
<?php echo vite(['/js/app.js']); ?>
9+
<?php
10+
if (!isset($__inertiaSsrDispatched)) {
11+
$__inertiaSsrDispatched = true;
12+
$__inertiaSsrResponse = (new \Leaf\Inertia\Ssr\Gateway())->dispatch($page);
13+
}
14+
15+
if ($__inertiaSsrResponse) {
16+
echo $__inertiaSsrResponse->head;
17+
}
18+
?>
19+
</head>
20+
21+
<body>
22+
<?php
23+
if (!isset($__inertiaSsrDispatched)) {
24+
$__inertiaSsrDispatched = true;
25+
$__inertiaSsrResponse = (new \Leaf\Inertia\Ssr\Gateway())->dispatch($page);
26+
}
27+
28+
if ($__inertiaSsrResponse) {
29+
echo $__inertiaSsrResponse->body;
30+
} else {
31+
echo '<div id="app" data-page="' . htmlspecialchars(json_encode($page), ENT_QUOTES, 'UTF-8', true) . '"></div>';
32+
}
33+
?>
34+
</body>
35+
36+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
export let auth;
3+
4+
let count = 0;
5+
6+
const increment = () => {
7+
count++;
8+
};
9+
</script>
10+
11+
<div>
12+
<h1>Hello World!</h1>
13+
<p>Current user: {auth?.user?.name ?? "No auth is active"}</p>
14+
<p>{count}</p>
15+
<button on:click={increment} class="bg-green-900">Increment</button>
16+
</div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createInertiaApp } from "@inertiajs/svelte";
2+
import { resolvePageComponent } from "@leafphp/vite-plugin/inertia-helpers";
3+
4+
const appName = import.meta.env.VITE_APP_NAME || "Leaf PHP";
5+
6+
createInertiaApp({
7+
title: (title) => `${title} - ${appName}`,
8+
resolve: (name) =>
9+
resolvePageComponent(`./Pages/${name}.svelte`,
10+
import.meta.glob('./Pages/**/*.svelte', { eager: true })
11+
),
12+
//or with persistent layouts
13+
// {// setting the default page layout
14+
// const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })
15+
// let page = pages[`./Pages/${name}.svelte`]
16+
// return { default: page.default, layout: page.layout || Layout }
17+
// },
18+
setup({ el, App, props, plugin }) {
19+
new App({ target: el, props })
20+
},
21+
progress: {
22+
color: "#4B5563",
23+
},
24+
});

0 commit comments

Comments
 (0)