Skip to content

Commit e6d0a16

Browse files
committed
add alpine loading
1 parent 8054cd6 commit e6d0a16

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

public/index.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77

88
$base = dirname(__DIR__);
99

10-
require $base . '/vendor/getkirby/cms/bootstrap.php';
11-
require $base . '/vendor/autoload.php';
10+
require "{$base}/vendor/getkirby/cms/bootstrap.php";
11+
require "{$base}/vendor/autoload.php";
1212

1313
$kirby = new App([
1414
'roots' => [
1515
'index' => __DIR__,
1616
'base' => $base,
17-
'site' => $base . '/site',
18-
'data' => $data = $base . '/data',
19-
'content' => $data . '/storage/content',
20-
'accounts' => $data . '/storage/accounts',
21-
'license' => $data . '/storage/.license',
22-
'cache' => $data . '/runtime/cache',
23-
'logs' => $data . '/runtime/logs',
24-
'sessions' => $data . '/runtime/sessions',
17+
'site' => "{$base}/site",
18+
'data' => $data = "{$base}/data",
19+
'content' => "{$data}/storage/content",
20+
'accounts' => "{$data}/storage/accounts",
21+
'license' => "{$data}/storage/.license",
22+
'cache' => "{$data}/runtime/cache",
23+
'logs' => "{$data}/runtime/logs",
24+
'sessions' => "{$data}/runtime/sessions",
2525
]
2626
]);
2727

src/components/hello-world.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineComponent } from "@/utils/alpine"
2+
3+
export default defineComponent(() => ({
4+
message: "Hello World!",
5+
init() {
6+
console.log(this.message)
7+
}
8+
}))

src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ declare global {
2121
window.Alpine = Alpine
2222

2323
Alpine.plugin(focus)
24-
Alpine.start()
2524

26-
document.addEventListener("alpine:init", () => {
27-
lazyLoad()
28-
})
25+
for (const [path, module] of Object.entries(import.meta.glob("./components/*.ts", { eager: true }))) {
26+
const name = path.slice(13, -3)
27+
// convert kebab-case to camelCase
28+
const camelName = name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())
29+
Alpine.data(camelName, (module as { default: any }).default)
30+
}
31+
32+
Alpine.start()
33+
lazyLoad('[loading="lazy"][data-src]')

src/utils/alpine.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { AlpineComponent } from "alpinejs"
2+
3+
export const defineComponent = <T extends Record<string, unknown>, A extends unknown[]>(
4+
callback: (...args: A) => AlpineComponent<T>
5+
) => callback

0 commit comments

Comments
 (0)