Skip to content

Commit 1c2afcd

Browse files
committed
Decouple Go build/test/lint from frontend asset compilation
Add a production build tag to assets.go so the dist/ embed directives only apply when building with -tags production. A new assets_dev.go provides stub values for development, tests, and linting — no Vite build required. CI lint and test jobs no longer need Node.js/pnpm; build-assets is moved exclusively into the build job. Also use -tags production for `just run` so local dev has real assets in the browser, and add manifest_test.go to cover NewManifest with a realistic Vite manifest fixture.
1 parent d82b3c1 commit 1c2afcd

6 files changed

Lines changed: 103 additions & 5 deletions

File tree

.github/actions/setup/action.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ runs:
2727
run: just install
2828
shell: bash
2929

30-
- name: Build Assets
31-
run: just build-assets
32-
shell: bash
3330
- name: Build Templates
3431
run: just build-templates
3532
shell: bash

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ jobs:
3333
uses: actions/checkout@v5
3434
- name: Run Common Setup
3535
uses: ./.github/actions/setup
36+
- name: Build Assets
37+
run: just build-assets
3638
- name: Build Application
3739
run: just build

assets.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build production
2+
13
package main
24

35
import "embed"

assets_dev.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !production
2+
3+
package main
4+
5+
import "embed"
6+
7+
var embedAssets embed.FS
8+
9+
//go:embed static
10+
var embedStatic embed.FS
11+
12+
var embedManifest = []byte(`{}`)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package layout
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// realManifest mirrors the structure produced by Vite: one entry point with
8+
// CSS, several non-entry chunks (dynamic imports, vendor splits, etc.).
9+
var realManifest = []byte(`{
10+
"__commonjsHelpers-CLPN-Npl.js": {
11+
"file": "assets/_commonjsHelpers-CLPN-Npl.js",
12+
"name": "_commonjsHelpers"
13+
},
14+
"_index-BmL2jti6.js": {
15+
"file": "assets/index-BmL2jti6.js",
16+
"name": "index"
17+
},
18+
"_index-bUj5cbfw.js": {
19+
"file": "assets/index-bUj5cbfw.js",
20+
"name": "index",
21+
"isDynamicEntry": true,
22+
"imports": ["_index-BmL2jti6.js"]
23+
},
24+
"node_modules/.pnpm/chart.js@4.5.1/node_modules/chart.js/dist/chart.js": {
25+
"file": "assets/chart-BjMzFfek.js",
26+
"name": "chart",
27+
"isDynamicEntry": true
28+
},
29+
"src/main.ts": {
30+
"file": "assets/main-CxdDuL5j.js",
31+
"name": "main",
32+
"src": "src/main.ts",
33+
"isEntry": true,
34+
"dynamicImports": ["node_modules/.pnpm/chart.js@4.5.1/node_modules/chart.js/dist/chart.js"],
35+
"css": ["assets/main-QTw0RKdw.css"]
36+
}
37+
}`)
38+
39+
func TestNewManifest_ParsesEntryPoint(t *testing.T) {
40+
manifest, err := NewManifest(realManifest)
41+
if err != nil {
42+
t.Fatalf("unexpected error: %v", err)
43+
}
44+
45+
if len(manifest.JS) != 1 || manifest.JS[0] != "/assets/main-CxdDuL5j.js" {
46+
t.Errorf("JS = %v, want [/assets/main-CxdDuL5j.js]", manifest.JS)
47+
}
48+
49+
if len(manifest.CSS) != 1 || manifest.CSS[0] != "/assets/main-QTw0RKdw.css" {
50+
t.Errorf("CSS = %v, want [/assets/main-QTw0RKdw.css]", manifest.CSS)
51+
}
52+
}
53+
54+
func TestNewManifest_SkipsNonEntryChunks(t *testing.T) {
55+
manifest, err := NewManifest(realManifest)
56+
if err != nil {
57+
t.Fatalf("unexpected error: %v", err)
58+
}
59+
60+
if len(manifest.JS) != 1 {
61+
t.Errorf("expected 1 JS entry, got %d: %v", len(manifest.JS), manifest.JS)
62+
}
63+
64+
if len(manifest.CSS) != 1 {
65+
t.Errorf("expected 1 CSS entry, got %d: %v", len(manifest.CSS), manifest.CSS)
66+
}
67+
}
68+
69+
func TestNewManifest_EmptyManifest(t *testing.T) {
70+
manifest, err := NewManifest([]byte(`{}`))
71+
if err != nil {
72+
t.Fatalf("unexpected error: %v", err)
73+
}
74+
75+
if len(manifest.JS) != 0 || len(manifest.CSS) != 0 {
76+
t.Errorf("expected empty manifest, got JS=%v CSS=%v", manifest.JS, manifest.CSS)
77+
}
78+
}
79+
80+
func TestNewManifest_InvalidJSON(t *testing.T) {
81+
_, err := NewManifest([]byte(`not json`))
82+
if err == nil {
83+
t.Error("expected error for invalid JSON, got nil")
84+
}
85+
}

justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ build-templates:
2323
go tool templ generate
2424

2525
build: build-assets build-templates
26-
go build -o pkgstatsd -ldflags="-s -w" -trimpath
26+
go build -tags production -o pkgstatsd -ldflags="-s -w" -trimpath
2727

2828
run:
29-
go run .
29+
go run -tags production .
3030

3131
test:
3232
go test ./...

0 commit comments

Comments
 (0)