From 509c0bf7586c5d6dc0455dde024d4c9ef27f2cdb Mon Sep 17 00:00:00 2001 From: WardPearce Date: Mon, 1 Apr 2024 12:26:08 +1300 Subject: [PATCH] Fixed sveltekit build --- materialious/Dockerfile | 34 ++++++++++++++++++++++------------ materialious/nginx.conf | 17 +++++++++++++++++ materialious/package-lock.json | 10 ++++++++++ materialious/package.json | 1 + materialious/svelte.config.js | 6 ++++-- 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 materialious/nginx.conf diff --git a/materialious/Dockerfile b/materialious/Dockerfile index 4de3277c..552b7d6c 100644 --- a/materialious/Dockerfile +++ b/materialious/Dockerfile @@ -1,30 +1,40 @@ -FROM node as build +FROM node AS builder +# Set the working directory WORKDIR /app -COPY ./package*.json ./ - -RUN npm install +# Copy package.json and package-lock.json to the working directory +COPY package*.json ./ +# Copy the rest of the project files to the working directory COPY . . +# Create placeholder env vars RUN echo "VITE_DEFAULT_INVIDIOUS_INSTANCE=VITE_DEFAULT_INVIDIOUS_INSTANCE_PLACEHOLDER" > .env && \ echo "VITE_DEFAULT_FRONTEND_URL=VITE_DEFAULT_FRONTEND_URL_PLACEHOLDER" > .env && \ echo "VITE_DEFAULT_SPONSERBLOCK_INSTANCE=VITE_DEFAULT_SPONSERBLOCK_INSTANCE_PLACEHOLDER" > .env && \ echo "VITE_DEFAULT_RETURNYTDISLIKES_INSTANCE=VITE_DEFAULT_RETURNYTDISLIKES_INSTANCE_PLACEHOLDER" >> .env -RUN npm run build +# Install dependencies and build the project +RUN npm install && npm run build -FROM node AS production +FROM nginx:alpine -COPY --from=build /app/build . +# Remove the default nginx configuration +RUN rm /etc/nginx/conf.d/default.conf -COPY --from=build /app/package.json . +# Copy the nginx.conf file to the container +COPY nginx.conf /etc/nginx/nginx.conf -COPY --from=build /app/package-lock.json . +# Copy the built Vite project from the builder stage +COPY --from=builder /app/dist /usr/share/nginx/html -RUN npm ci --omit dev +# Expose the desired port +EXPOSE 80 -EXPOSE 3000 +# Copy the replace_env_vars.sh script +COPY replace_env_vars.sh /replace_env_vars.sh +RUN chmod +x /replace_env_vars.sh -CMD ["node", "."] \ No newline at end of file +# Replace env vars & Start nginx +CMD ["/bin/sh", "-c", "/replace_env_vars.sh && nginx -g \"daemon off;\""] \ No newline at end of file diff --git a/materialious/nginx.conf b/materialious/nginx.conf new file mode 100644 index 00000000..2984783a --- /dev/null +++ b/materialious/nginx.conf @@ -0,0 +1,17 @@ +events { + worker_connections 1024; +} + +http { + default_type application/octet-stream; + include /etc/nginx/mime.types; + + server { + listen 80; + + location / { + root /usr/share/nginx/html; + try_files $uri /index.html; + } + } +} \ No newline at end of file diff --git a/materialious/package-lock.json b/materialious/package-lock.json index a23aee76..06fc7743 100644 --- a/materialious/package-lock.json +++ b/materialious/package-lock.json @@ -17,6 +17,7 @@ }, "devDependencies": { "@sveltejs/adapter-auto": "^3.0.0", + "@sveltejs/adapter-static": "^3.0.1", "@sveltejs/kit": "^2.0.0", "@sveltejs/vite-plugin-svelte": "^3.0.0", "@types/eslint": "^8.56.0", @@ -2688,6 +2689,15 @@ "@sveltejs/kit": "^2.0.0" } }, + "node_modules/@sveltejs/adapter-static": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@sveltejs/adapter-static/-/adapter-static-3.0.1.tgz", + "integrity": "sha512-6lMvf7xYEJ+oGeR5L8DFJJrowkefTK6ZgA4JiMqoClMkKq0s6yvsd3FZfCFvX1fQ0tpCD7fkuRVHsnUVgsHyNg==", + "dev": true, + "peerDependencies": { + "@sveltejs/kit": "^2.0.0" + } + }, "node_modules/@sveltejs/kit": { "version": "2.5.5", "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.5.5.tgz", diff --git a/materialious/package.json b/materialious/package.json index accd644c..283bd062 100644 --- a/materialious/package.json +++ b/materialious/package.json @@ -13,6 +13,7 @@ }, "devDependencies": { "@sveltejs/adapter-auto": "^3.0.0", + "@sveltejs/adapter-static": "^3.0.1", "@sveltejs/kit": "^2.0.0", "@sveltejs/vite-plugin-svelte": "^3.0.0", "@types/eslint": "^8.56.0", diff --git a/materialious/svelte.config.js b/materialious/svelte.config.js index 2b35fe1b..0227345f 100644 --- a/materialious/svelte.config.js +++ b/materialious/svelte.config.js @@ -1,4 +1,4 @@ -import adapter from '@sveltejs/adapter-auto'; +import adapter from '@sveltejs/adapter-static'; import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; /** @type {import('@sveltejs/kit').Config} */ @@ -11,7 +11,9 @@ const config = { // adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list. // If your environment is not supported or you settled on a specific environment, switch out the adapter. // See https://kit.svelte.dev/docs/adapters for more information about adapters. - adapter: adapter() + adapter: adapter({ + fallback: 'index.html' + }) } };