summaryrefslogtreecommitdiffstats
path: root/internal/builder/importmap.go
diff options
context:
space:
mode:
authorIvar Løvlie <38570165+ivarlovlie@users.noreply.github.com>2026-03-31 12:27:46 +0200
committerGitHub <noreply@github.com>2026-03-31 12:27:46 +0200
commit8d7cda6e578e684483c0b5c7391c48e5b9ac5192 (patch)
treed2b6506db2de72b3a6982cfbe69925b88936de90 /internal/builder/importmap.go
parent33f214f6cd9729473bb55fd7b3b923d5d960bb98 (diff)
parent3cb7c82cf7c4e050148f69be23590a7fbe587a27 (diff)
downloadnebbet.no-8d7cda6e578e684483c0b5c7391c48e5b9ac5192.tar.xz
nebbet.no-8d7cda6e578e684483c0b5c7391c48e5b9ac5192.zip
Merge pull request #1 from ivarlovlie/claude/static-site-sqlite-setup-mrcAr
Diffstat (limited to 'internal/builder/importmap.go')
-rw-r--r--internal/builder/importmap.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/internal/builder/importmap.go b/internal/builder/importmap.go
new file mode 100644
index 0000000..8445411
--- /dev/null
+++ b/internal/builder/importmap.go
@@ -0,0 +1,58 @@
+package builder
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+// ImportMap represents a browser importmap.
+type ImportMap struct {
+ Imports map[string]string `json:"imports"`
+}
+
+// GenerateImportMap scans libDir for .js files and produces an importmap JSON string.
+//
+// Naming rules:
+// - lib/chart.js → "chart"
+// - lib/icons/index.js → "icons"
+// - lib/utils/helpers.js → "utils/helpers"
+func GenerateImportMap(libDir string) (string, error) {
+ imports := make(map[string]string)
+
+ if _, err := os.Stat(libDir); os.IsNotExist(err) {
+ b, _ := json.MarshalIndent(ImportMap{Imports: imports}, "", " ")
+ return string(b), nil
+ }
+
+ err := filepath.WalkDir(libDir, func(path string, d os.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(path, ".js") {
+ return err
+ }
+ rel, _ := filepath.Rel(libDir, path)
+ rel = filepath.ToSlash(rel)
+
+ dir := filepath.ToSlash(filepath.Dir(rel))
+ base := strings.TrimSuffix(filepath.Base(rel), ".js")
+
+ var importName string
+ switch {
+ case dir == ".":
+ importName = base
+ case base == "index":
+ importName = dir
+ default:
+ importName = dir + "/" + base
+ }
+
+ imports[importName] = "/lib/" + rel
+ return nil
+ })
+ if err != nil {
+ return "", err
+ }
+
+ b, err := json.MarshalIndent(ImportMap{Imports: imports}, "", " ")
+ return string(b), err
+}