Repository navigation
refactor(http): build the web UI with Vite and embed it gzipped in the firmware #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3eb141e
feat(http): embed the gzipped web UI in the firmware
Mechazawa b345f6c
refactor(http): tie UI state to its views
Mechazawa 87e9754
fix(http): rebuild the embedded UI when ui/package.json changes
Mechazawa 75f1163
fix(scripts): drop stale directories from the LittleFS data tree
Mechazawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| .DS_Store | ||
| __pycache__/ | ||
| data/*.* | ||
| data/ui/ | ||
| data/*/ | ||
| artifacts/ | ||
|
|
||
| # Cursor hook event log | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| Import("env") | ||
|
|
||
| import gzip | ||
| import shutil | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| project_dir = Path(env.subst("$PROJECT_DIR")) | ||
| ui_dir = project_dir / "ui" | ||
| generated_dir = Path(env.subst("$BUILD_DIR")) / "generated" | ||
| header = generated_dir / "web_ui.h" | ||
|
|
||
|
|
||
| def npm(*args): | ||
| executable = shutil.which("npm") | ||
| if executable is None: | ||
| raise RuntimeError("npm not found; building the web UI needs Node 20.19+ on 20.x, or 22.12+") | ||
| subprocess.run([executable, *args], cwd=ui_dir, check=True) | ||
|
|
||
|
|
||
| def newest_source_mtime(): | ||
| sources = [ui_dir / "index.html", ui_dir / "vite.config.js", ui_dir / "package.json", ui_dir / "package-lock.json"] | ||
| sources += [path for path in (ui_dir / "src").rglob("*") if path.is_file()] | ||
| return max(path.stat().st_mtime for path in sources) | ||
|
|
||
|
|
||
| env.Append(CPPPATH=[str(generated_dir)]) | ||
|
|
||
| # pio runs pre-scripts for every target (uploadfs, otafs, ...), so skip Node when ui/ is unchanged. | ||
| if header.is_file() and header.stat().st_mtime >= newest_source_mtime(): | ||
| Return() | ||
|
|
||
| # npm writes this marker on install; an older one means the lockfile changed since. | ||
| installed = ui_dir / "node_modules" / ".package-lock.json" | ||
| if not installed.is_file() or installed.stat().st_mtime < (ui_dir / "package-lock.json").stat().st_mtime: | ||
| npm("ci") | ||
| npm("run", "build") | ||
|
|
||
| page = (ui_dir / "dist" / "index.html").read_bytes() | ||
| # mtime=0 keeps the bytes identical for identical sources, so an unchanged UI does not rebuild index_page.cpp. | ||
| packed = gzip.compress(page, compresslevel=9, mtime=0) | ||
| rows = ",\n".join( | ||
| " " + ", ".join(f"0x{byte:02x}" for byte in packed[offset:offset + 16]) | ||
| for offset in range(0, len(packed), 16) | ||
| ) | ||
| source = ( | ||
| "// Generated by scripts/embed_ui.py from ui/. Do not edit.\n" | ||
| "#pragma once\n\n" | ||
| "#include <Arduino.h>\n\n" | ||
| f"const uint8_t kWebUiGzip[] PROGMEM = {{\n{rows}\n}};\n" | ||
| ) | ||
|
|
||
| generated_dir.mkdir(parents=True, exist_ok=True) | ||
| if header.is_file() and header.read_text() == source: | ||
| header.touch() | ||
| else: | ||
| header.write_text(source) | ||
| print(f"Embedded web UI: {len(page)} -> {len(packed)} bytes gzipped") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.