Skip to content

Commit 7de0f8b

Browse files
committed
watch frontend assets during development
1 parent 0d74a86 commit 7de0f8b

5 files changed

Lines changed: 31 additions & 7 deletions

File tree

.air.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ pre_cmd = ["go tool templ generate"]
33
cmd = "go build -tags production -o ./tmp/pkgstatsd ."
44
entrypoint = ["./tmp/pkgstatsd"]
55
include_ext = ["go", "templ"]
6+
include_file = [".assets-rebuilt"]
67
exclude_dir = [".idea", "dist", "node_modules", "tmp"]
78
exclude_regex = ["_templ\\.go$"]

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/tmp/*
1414
!/tmp/.gitkeep
1515
/pkgstatsd
16+
/.assets-rebuilt
1617

1718
# development
1819
/fixtures

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ the Arch Linux package statistics website.
1717

1818
# Contributing
1919
## Dev Mode
20-
Run `just dev` to watch for template and Go changes and rebuild automatically (requires [air](https://github.com/air-verse/air)).
21-
The automatic rebuild does not include assets (images, JS, CSS) and all unused classes are stripped during the build process.
22-
Thus, when changing classes for example, you might need to stop the dev server and run `just build-assets` and then start
23-
the project again.
20+
Run `just dev` to watch for Go, template and frontend changes and rebuild automatically (requires [air](https://github.com/air-verse/air)).
2421

2522
## Tests
2623
For contributing you'll probably want to test your changes at least once

justfile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,20 @@ build: build-assets build-templates
3636
run:
3737
go run -tags production .
3838

39+
# open the local dev server in the default browser
40+
open:
41+
xdg-open 'http://localhost:{{ PORT }}'
42+
3943
# watch for template and Go changes and rebuild automatically
40-
dev:
44+
[parallel]
45+
dev: dev-assets dev-server
46+
47+
[private]
48+
dev-assets:
49+
pnpm exec vite build --watch
50+
51+
[private]
52+
dev-server:
4153
air
4254

4355
# run all tests

vite.config.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import { writeFileSync } from "fs";
12
import { fileURLToPath } from "url";
23
import { dirname, resolve } from "path";
3-
import type { UserConfig } from "vite";
4+
import type { Plugin, UserConfig } from "vite";
5+
6+
const isWatch = process.argv.includes("--watch");
47

58
const __dirname = dirname(fileURLToPath(import.meta.url));
69

10+
function notifyAir(): Plugin {
11+
return {
12+
name: "notify-air",
13+
writeBundle() {
14+
writeFileSync(".assets-rebuilt", String(Date.now()));
15+
},
16+
};
17+
}
18+
719
export default {
820
resolve: {
921
alias: [
@@ -49,10 +61,11 @@ export default {
4961
},
5062
},
5163
},
64+
plugins: isWatch ? [notifyAir()] : [],
5265
publicDir: false,
5366
build: {
5467
manifest: "manifest.json",
55-
minify: "terser",
68+
minify: isWatch ? false : "terser",
5669
rollupOptions: {
5770
input: "src/main.ts",
5871
},

0 commit comments

Comments
 (0)