From 1b760329ef9fbcc64f6ec3186ddcb52cc13ef30a Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Mon, 24 Jun 2024 20:53:48 -0400 Subject: [PATCH] Build frontends with the expected base url --- .gitignore | 2 ++ Makefile | 64 +++++++++++++++++------------------- index.html | 13 -------- index.tmpl | 10 ++++++ reactNextJS/next.config.js | 1 + server.go | 66 +++++++++++++++++++++++++++++++++++--- svelte/vite.config.js | 1 + vuejs/vite.config.js | 3 +- 8 files changed, 107 insertions(+), 53 deletions(-) delete mode 100644 index.html create mode 100644 index.tmpl diff --git a/.gitignore b/.gitignore index b313bd4..e0461c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ tmp server +dist +index.html diff --git a/Makefile b/Makefile index f7e9754..aa3e5e2 100644 --- a/Makefile +++ b/Makefile @@ -20,47 +20,43 @@ svelte: svelte/dist/* cleanSvelte: rm -rf ./svelte/dist -cleanbuild: - rm -rf tmp/build -tmp/build: cleanbuild - mkdir -p tmp/build -tmp/build/vanillaJS: tmp/build vanillaJS/index.html - mkdir tmp/build/vanillaJS && \ - cp ./vanillaJS/index.html ./tmp/build/vanillaJS -tmp/build/react: tmp/build react/index.html - mkdir tmp/build/react && \ - cp ./react/index.html ./tmp/build/react/ -tmp/build/alpinejs: alpinejs/index.html - mkdir tmp/build/alpinejs && \ - cp ./alpinejs/index.html ./tmp/build/alpinejs -tmp/build/_next: tmp/build reactNextJS/out/* - mkdir ./tmp/build/_next && \ - cp -r ./reactNextJS/out/_next ./tmp/build/ -tmp/build/reactNextJS: tmp/build reactNextJS/out/* - mkdir ./tmp/build/reactNextJS && \ - cp ./reactNextJS/out/*.* ./tmp/build/reactNextJS/ -tmp/build/assets: tmp/build svelte/dist/* vuejs/dist/* - mkdir tmp/build/assets && \ - cp svelte/dist/assets/* tmp/build/assets/ && \ - cp vuejs/dist/assets/* tmp/build/assets/ -tmp/build/vuejs: tmp/build vuejs/dist/* - mkdir ./tmp/build/vuejs && \ - cp ./vuejs/dist/*.* ./tmp/build/vuejs/ -tmp/build/svelte: tmp/build svelte/dist/* - mkdir ./tmp/build/svelte && \ - cp -r ./svelte/dist/* ./tmp/build/svelte/ -tmp/build/index.html: tmp/build index.html - cp index.html tmp/build/ +cleandist: + rm -rf dist +dist: + mkdir -p dist +dist/vanillaJS: dist vanillaJS/index.html + rm -rf dist/vanillaJS && \ + cp -r ./vanillaJS ./dist/vanillaJS +dist/react: dist react/index.html + rm -rf dist/react && \ + cp -r ./react ./dist/react +dist/alpinejs: dist alpinejs/index.html + rm -rf dist/alpinejs && \ + cp -r ./alpinejs ./dist/alpinejs +dist/reactNextJS: dist reactNextJS/out/* + rm -rf ./dist/reactNextJS && \ + cp -r ./reactNextJS/out ./dist/reactNextJS +dist/vuejs: dist vuejs/dist/* + rm -rf ./dist/vuejs && \ + cp -r ./vuejs/dist ./dist/vuejs +dist/svelte: dist svelte/dist/* + rm -rf ./dist/svelte && \ + cp -r ./svelte/dist ./dist/svelte +dist/index.html: dist index.tmpl + go run server.go generate + mv index.html dist/ -web: tmp/build/assets tmp/build/vanillaJS tmp/build/react tmp/build/alpinejs tmp/build/_next tmp/build/reactNextJS tmp/build/assets tmp/build/vuejs tmp/build/svelte tmp/build/index.html +export: dist/vanillaJS dist/react dist/alpinejs dist/reactNextJS dist/assets dist/vuejs dist/svelte dist/index.html +web: ./svelte/dist/* ./vuejs/dist/* ./reactNextJS/out/* vanillaJS/index.html react/index.html alpinejs/index.html build: web server.go go build server.go run: web server.go go run server.go air: nil web server.go air server.go -clean: cleanNext cleanVue cleanSvelte +clean: cleanNext cleanVue cleanSvelte cleandist rm -rf tmp rm -f server + rm -f index.html -.PHONY: all build clean cleanbuild nextjs vuejs svelte run air nil web +.PHONY: all build clean cleanbuild nextjs vuejs svelte run air nil web export diff --git a/index.html b/index.html deleted file mode 100644 index a91f3d4..0000000 --- a/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - -

Frontends

- - - diff --git a/index.tmpl b/index.tmpl new file mode 100644 index 0000000..666bcc4 --- /dev/null +++ b/index.tmpl @@ -0,0 +1,10 @@ + + +

Frontends

+ + + diff --git a/reactNextJS/next.config.js b/reactNextJS/next.config.js index 770f839..3aca643 100644 --- a/reactNextJS/next.config.js +++ b/reactNextJS/next.config.js @@ -3,6 +3,7 @@ */ const nextConfig = { output: 'export', + basePath: '/reactNextJS', // Optional: Change links `/me` -> `/me/` and emit `/me.html` -> `/me/index.html` // trailingSlash: true, diff --git a/server.go b/server.go index 652b8bc..ce1263f 100644 --- a/server.go +++ b/server.go @@ -3,21 +3,77 @@ package main import ( "embed" "fmt" + "html/template" "io/fs" "net/http" + "os" ) const PORT = 3003 -//go:embed "all:tmp/build" -var webfs embed.FS +type frontendInfo struct { + Name, Mountpoint string +} + +//go:embed "all:vuejs/dist" +var vueFiles embed.FS + +//go:embed "all:reactNextJS/out" +var nextFiles embed.FS + +//go:embed "all:svelte/dist" +var svelteFiles embed.FS + +//go:embed "vanillaJS" +var vanillaFiles embed.FS + +//go:embed "alpinejs" +var alpineFiles embed.FS + +//go:embed "react" +var reactFiles embed.FS + +func addFrontend(files embed.FS, mountpoint string, subfs string, name string) frontendInfo { + subFiles, _ := fs.Sub(files, subfs) + fileServer := http.FileServerFS(subFiles) + http.Handle(mountpoint, http.StripPrefix(mountpoint, fileServer)) + return frontendInfo{name, mountpoint} +} + +func outputIndexAndExit(indexTemplate template.Template, frontends []frontendInfo) { + file, err := os.OpenFile("index.html", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + panic(err) // Handle error + } + defer file.Close() + indexTemplate.Execute(file, frontends) + os.Exit(0) +} func main() { - webfs, err := fs.Sub(webfs, "tmp/build") + args := os.Args + + home_template, err := template.ParseFiles("index.tmpl") if err != nil { - panic(fmt.Sprintf("Error embeding web files: %s\nTry running \"make web\"", err)) + panic(fmt.Sprintf("Error parsing homepage template\n%s", err)) } - http.Handle("/", http.FileServerFS(webfs)) + + frontends := []frontendInfo{ + addFrontend(vanillaFiles, "/vanillaJS/", "vanillaJS", "JS Only"), + addFrontend(alpineFiles, "/alpinejs/", "alpinejs", "Alpine.js"), + addFrontend(reactFiles, "/react/", "react", "React"), + addFrontend(nextFiles, "/reactNextJS/", "reactNextJS/out", "React (Next.js)"), + addFrontend(vueFiles, "/vuejs/", "vuejs/dist", "Vue.js"), + addFrontend(svelteFiles, "/svelte/", "svelte/dist", "Svelte"), + } + + if len(args) == 2 && args[1] == "generate" { + outputIndexAndExit(*home_template, frontends) + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + err = home_template.Execute(w, frontends) + }) err = http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil) if err != nil { diff --git a/svelte/vite.config.js b/svelte/vite.config.js index d701969..70b4cc8 100644 --- a/svelte/vite.config.js +++ b/svelte/vite.config.js @@ -4,4 +4,5 @@ import { svelte } from '@sveltejs/vite-plugin-svelte' // https://vitejs.dev/config/ export default defineConfig({ plugins: [svelte()], + base: '/svelte/' }) diff --git a/vuejs/vite.config.js b/vuejs/vite.config.js index 36c6187..2a7dcf1 100644 --- a/vuejs/vite.config.js +++ b/vuejs/vite.config.js @@ -14,5 +14,6 @@ export default defineConfig({ alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } - } + }, + base: '/vuejs/' })