From e139f746bc78a9a0576eef8c1a5f53411e2b06d0 Mon Sep 17 00:00:00 2001
From: paku <paku@skyizwhite.dev>
Date: Sat, 24 Feb 2024 18:18:24 +0900
Subject: [PATCH] Improve routing for public files

---
 .gitignore                   |  2 --
 src/app.lisp                 | 14 ++++++++++----
 src/{js => assets}/main.js   |  0
 src/{js => assets}/style.css |  0
 src/components/layout.lisp   |  2 +-
 src/routes/index.lisp        |  2 +-
 vite.config.js               |  6 ++++--
 7 files changed, 16 insertions(+), 10 deletions(-)
 rename src/{js => assets}/main.js (100%)
 rename src/{js => assets}/style.css (100%)

diff --git a/.gitignore b/.gitignore
index a40b420..8a1717f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,3 @@
 .qlot
 node_modules
-static/style/tailwind.css
-.vscode/alive
 dist
diff --git a/src/app.lisp b/src/app.lisp
index 03abd3d..3985415 100644
--- a/src/app.lisp
+++ b/src/app.lisp
@@ -17,13 +17,19 @@
 
 (update-routes)
 
+(defun exist-public-file-p (path)
+  (and (not (string= path "/"))
+       (let ((pathname (probe-file (concatenate 'string "public" path))))
+         (and pathname
+              (pathname-name pathname)))))
+
 (defparameter *app*
   (lack:builder :accesslog
                 (:static
-                 :path "/assets/"
-                 :root (asdf:system-relative-pathname :hp "dist/assets/"))
-                (:static
-                 :path "/public/"
+                 :path (lambda (path)
+                         (if (exist-public-file-p path) 
+                             path
+                             nil))
                  :root (asdf:system-relative-pathname :hp "public/"))
                 *raw-app*))
 
diff --git a/src/js/main.js b/src/assets/main.js
similarity index 100%
rename from src/js/main.js
rename to src/assets/main.js
diff --git a/src/js/style.css b/src/assets/style.css
similarity index 100%
rename from src/js/style.css
rename to src/assets/style.css
diff --git a/src/components/layout.lisp b/src/components/layout.lisp
index 09460cc..9140bf1 100644
--- a/src/components/layout.lisp
+++ b/src/components/layout.lisp
@@ -10,7 +10,7 @@
       (head
         (title "skyizwhite.dev")
         (script :type "module" :src "http://localhost:5173/@vite/client")
-        (script :type "module" :src "http://localhost:5173/src/js/main.js"))
+        (script :type "module" :src "http://localhost:5173/src/assets/main.js"))
       (body :class "h-[100svh] w-screen"
         (main :class "h-full"
           pi:children)))))
diff --git a/src/routes/index.lisp b/src/routes/index.lisp
index 64aa68b..dc48176 100644
--- a/src/routes/index.lisp
+++ b/src/routes/index.lisp
@@ -15,7 +15,7 @@
         (div :class "flex flex-col items-center gap-2"
           (div :class "avatar"
             (div :class "w-32 mask mask-squircle"
-              (img :src "/public/img/me.jpg")))
+              (img :src "/img/me.jpg")))
           (p :class "text-primary text-3xl text-center"
             "Hello, World!"))))))
 
diff --git a/vite.config.js b/vite.config.js
index 136f460..4077840 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -2,9 +2,11 @@ import { defineConfig } from 'vite'
 
 export default defineConfig({
   build: {
-    manifest: true,
+    outDir: 'public',
+    emptyOutDir: false,
+    copyPublicDir: false,
     rollupOptions: {
-      input: 'src/js/main.js',
+      input: 'src/assets/main.js',
     },
   },
 })