Skip to content

Commit

Permalink
minify all views and public files
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtheman committed Sep 18, 2024
1 parent 290d98b commit bac7143
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ RUN chmod +x ./z
COPY ./db/migrations ./db/migrations
COPY ./db/schema.sql ./db/

COPY src/views ./src/views
COPY dist/views ./src/views
COPY src/public ./src/public
COPY src/instrumentation.js ./src/
COPY dist/public/output.min.css ./src/public/output.min.css
COPY src/instrumentation.js ./
COPY .env.production ./

EXPOSE 3000
Expand Down
Binary file modified bun.lockb
Binary file not shown.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"test": "echo \"Error: no test specified\" && exit 1",
"db:migrate": "dotenvx run -f .env.production -- bun run dbmate up",
"prepare": "cp .hooks/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit",
"minify": "html-minifier-terser --input-dir src/views --output-dir dist/views --file-ext eta --collapse-whitespace --remove-comments --minify-css true --minify-js true && cleancss -o dist/public/output.min.css src/public/output.css",
"build-linux": "dotenvx run -f .env.production -- bun build --compile --minify --sourcemap --target=bun-linux-x64-modern src/index.js --outfile ./dist/z",
"deploy": "bun run build-linux && fly deploy && bun run clean",
"deploy": "bun run build-linux && bun run minify && fly deploy && bun run clean",
"clean": "rm -rf dist/*"
},
"author": "max caldwell",
Expand All @@ -32,8 +33,10 @@
"pino": "^9.4.0"
},
"devDependencies": {
"clean-css-cli": "^5.6.3",
"dbmate": "^2.20.0",
"eslint": "^9.10.0",
"html-minifier-terser": "^7.2.0",
"pino-pretty": "^11.2.2",
"prettier": "^3.3.3",
"prettier-plugin-ejs": "^1.0.3",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Mental model for picking this or any stack:
- https://litestream.io/guides/docker/ This would be a good first step for backups, and a stepping stone on the journey to litefs.
- Testing framework? Didn't bother setting up tests for this. Bun comes with bun:test, probably use that.
- Typescript
- Caching implementation -- didn't get to this yet

## Going faster

Expand Down
2 changes: 2 additions & 0 deletions setupDb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ MIGRATIONS_DIR="./db/migrations"
# Create the data directory if it doesn't exist
mkdir -p /data

# Could probably move all of this into bun in the future

if [ ! -f /data/mydb.sqlite ]; then
echo "Database not found, creating and running migrations..."
touch /data/mydb.sqlite
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ app.get("/", requiresAuth, async (c) => {
logger.info({
user: c.get("user")?.sub,
});
const html = views.render("index", { isAuthenticated: true });
const html = views.render("index", {
isAuthenticated: true,
isProduction: process.env.NODE_ENV === "production",
});
return c.html(html, 200);
});

Expand Down
4 changes: 2 additions & 2 deletions src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ db.run("PRAGMA journal_mode = WAL");
// You can also try OFF which is faster but less safe.
db.run("PRAGMA synchronous = NORMAL");
// https://www.sqlite.org/pragma.html#pragma_optimize
// Run PRAGMA OPTIMIZE every 6 hours using cron
if (process.env.NODE_ENV === "production") {
// Run PRAGMA OPTIMIZE every 6 hours using cron - set to disabled for now, rethinking.
if (process.env.NODE_ENV === "production" && false) {
const checkCronJob = $`crontab -l | grep -q "PRAGMA optimize"`;
checkCronJob.then(() => {
logger.info("Cron job for database optimization already exists");
Expand Down
16 changes: 10 additions & 6 deletions src/views/index.eta
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Todo Management</title>
<link href="/public/output.css"
rel="stylesheet" />
<script src="/public/htmx.min.js"></script>
<script type="module">
<% if (it.isProduction) { %>
<link href="/public/output.min.css" rel="stylesheet" />
<script src="/public/htmx.min.js"></script>
<script src="/public/auto-animate.min.js"></script>
<% } else { %>
<script src="/public/htmx.min.js"></script>
<link href="/public/output.min.css" rel="stylesheet" />
<% } %>
<script type="module">
import autoAnimate from "/public/auto-animate.min.js";
document.addEventListener("DOMContentLoaded", () => {
const todoList = document.querySelector("#todoList");
Expand Down

0 comments on commit bac7143

Please sign in to comment.