Skip to content

Commit

Permalink
'npx serve dist' works, before 'dist' rebuild attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanbluesky committed Dec 14, 2024
1 parent b1e3aac commit 61386d5
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 99 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

# Cargo.toml

[package]
name = "github_bio"
version = "0.1.0"
Expand All @@ -14,9 +17,6 @@ stylist = "^0.13"
# Allow wasm-bindgen to update dynamically while staying within major version 0.2
wasm-bindgen = "^0.2.99"

[web.watcher]
index_on_404 = true

[profile]

[profile.wasm-dev]
Expand Down
3 changes: 3 additions & 0 deletions Dioxus.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

# Dioxus.toml

[application]
output = "dist"
serve_dir = "dist"
71 changes: 19 additions & 52 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Stage 1: Build the Dioxus application and Tailwind CSS
FROM rustlang/rust:nightly-bookworm-slim AS builder
# Use Rust nightly since Dioxus often requires latest features
FROM rustlang/rust:nightly-bookworm-slim

# Prevent interactive prompts during build
ENV DEBIAN_FRONTEND=noninteractive

# Install Python 3, system dependencies, and other required packages
# Install system dependencies
RUN apt-get update && apt-get install -y \
python3 \
python3-venv \
python3-pip \
build-essential \
pkg-config \
libssl-dev \
Expand All @@ -18,7 +15,8 @@ RUN apt-get update && apt-get install -y \
curl \
git \
jq \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
Expand All @@ -39,58 +37,27 @@ RUN cargo metadata --format-version 1 \
| jq -r '.packages[] | select(.name == "wasm-bindgen") | .version' \
| xargs -I {} cargo install -f wasm-bindgen-cli --version {}

# Copy package.json and install Node dependencies
# Copy package files first to leverage Docker cache
COPY package.json ./
RUN npm install

# Copy the rest of the application
COPY . .

# Build Tailwind CSS and the Dioxus application
RUN npm run build:css
RUN dx build --release --platform web



# SECOND IMAGE


# Stage 2: Create a lightweight image for serving
FROM debian:bullseye-slim

# Prevent interactive prompts during runtime
ENV DEBIAN_FRONTEND=noninteractive
# Create dummy src to build dependencies
RUN mkdir src && \
echo "fn main() {println!(\"dummy\")}" > src/main.rs && \
cargo build --release && \
rm -rf src

# Set working directory
WORKDIR /app
# Create required directories for Tailwind
RUN mkdir -p src/styles dist/assets/styles

# Copy build output from the builder stage
COPY --from=builder /app/dist /app/dist
# Copy the rest of the application
COPY . .

# Install Python3 and a lightweight HTTP server
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
curl \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Ensure the output directory for Tailwind exists
RUN mkdir -p dist/assets/styles

# Expose port
EXPOSE 8080

# Start the server to serve static files
CMD ["sh", "-c", "cd dist && python3 -m http.server 8080"]


# Explanation
# Multi-Stage Build:
# Stage 1 (Builder):
# Includes all build tools (Rust, Node.js, Dioxus CLI).
# Builds Tailwind CSS (npm run build:css) and the Dioxus application (dx build).
# Stage 2 (Final Image):
# Copies the built dist folder from the builder stage.
# Uses a lightweight base image (debian:bullseye-slim).
# Avoids including unnecessary tools (Rust, Node.js) in the final image.
# Static File Serving:
# The dist directory is served using Python's built-in HTTP server.
# Smaller Final Image:
# The final image is lightweight since it only includes the static files and a minimal HTTP server.
# CMD
CMD ["npm", "run", "serve"]
10 changes: 10 additions & 0 deletions __about_these_files.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@


# New Approach to running (without Docker)
Generates a static build (dist/index.html) for production deployment:

dx build --release
npx serve dist



# Old Approach to running (without Docker)
# 1st:
npm run serve
# 2nd:
Expand Down
2 changes: 1 addition & 1 deletion dist/assets/styles/tailwind.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Tailwind base styles */
/* src/styles/input.css Tailwind base styles */

*, ::before, ::after {
--tw-border-spacing-x: 0;
Expand Down
42 changes: 4 additions & 38 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

<!-- dist/index.html -->

<!DOCTYPE html>
<html>
<head>
Expand All @@ -18,42 +21,5 @@
}
});
</script>

</body>
</html><script>// Dioxus-CLI
// https://github.com/DioxusLabs/dioxus/tree/master/packages/cli
// NOTE: This is also used in the fullstack package at ../packages/fullstack/src/render.rs, if you make changes here, make sure to update the version in there as well
// TODO: Extract hot reloading with axum into a separate crate or use the fullstack hot reloading Axum extension trait here

(function () {
var protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
var url = protocol + "//" + window.location.host + "/_dioxus/ws";
var poll_interval = 8080;

var reload_upon_connect = (event) => {
// Firefox will send a 1001 code when the connection is closed because the page is reloaded
// Only firefox will trigger the onclose event when the page is reloaded manually: https://stackoverflow.com/questions/10965720/should-websocket-onclose-be-triggered-by-user-navigation-or-refresh
// We should not reload the page in this case
if (event.code === 1001) {
return;
}
window.setTimeout(() => {
var ws = new WebSocket(url);
ws.onopen = () => window.location.reload();
ws.onclose = reload_upon_connect;
}, poll_interval);
};

var ws = new WebSocket(url);

ws.onmessage = (ev) => {
console.log("Received message: ", ev, ev.data);

if (ev.data == "reload") {
window.location.reload();
}
};

ws.onclose = reload_upon_connect;
})();
</script>
</html>
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"tailwindcss": "^3.4.16"
},
"scripts": {
"build:css": "npx tailwindcss -i src/styles/input.css -o dist/assets/styles/tailwind.css",
"build": "dx build --release",
"build:css": "npx tailwindcss -i src/styles/input.css -o dist/assets/styles/tailwind.css",
"build": "npm run build:css && build --release",
"serve": "serve dist",
"dev": "concurrently \"npm run build:css --watch\" \"dx serve\""
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pub enum Route {
}

fn main() {
dioxus::launch(App);
dioxus::launch(app);
}

pub fn App() -> Element {
pub fn app() -> Element {
rsx! { Router::<Route> {} }
}
2 changes: 1 addition & 1 deletion src/styles/input.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Tailwind base styles */
/* src/styles/input.css Tailwind base styles */
@tailwind base;
@tailwind components;
@tailwind utilities;

0 comments on commit 61386d5

Please sign in to comment.