Skip to content

Commit

Permalink
Added check for Vite app, opt to use Nginx instead of Node
Browse files Browse the repository at this point in the history
- Checks if its a Vite app based on whether the "dev" script is defined as "vite"
- Takes result of `vite build` from /dist and puts into Nginx's html directory
  • Loading branch information
Annie Sexton authored and Annie Sexton committed Nov 21, 2023
1 parent e888291 commit ce1dd43
Show file tree
Hide file tree
Showing 6 changed files with 3,807 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export class GDF {
// previous answer to conflict prompt
#answer = ''

get vite() {
return !!(this.#pj.scripts?.dev === 'vite')
}

// Does this application use remix.run?
get remix() {
return !!(this.#pj.dependencies?.remix ||
Expand Down Expand Up @@ -573,6 +577,7 @@ export class GDF {
if (this.nestjs) runtime = 'NestJS'
if (this.gatsby) runtime = 'Gatsby'
if (this.adonisjs) runtime = 'AdonisJS'
if (this.vite) runtime = 'Vite'

if (this.prisma) runtime += '/Prisma'

Expand Down Expand Up @@ -607,6 +612,8 @@ export class GDF {

if (this.gatsby) {
return [this.npx, 'gatsby', 'serve', '-H', '0.0.0.0']
} else if (this.vite) {
return ['/usr/sbin/nginx', '-g', 'daemon off;']
} else if (this.runtime === 'Node.js' && this.#pj.scripts?.start?.includes('fastify')) {
let start = this.#pj.scripts.start
if (!start.includes('-a') && !start.includes('--address')) {
Expand Down Expand Up @@ -682,6 +689,7 @@ export class GDF {
let port = 3000

if (this.gatsby) port = 8080
if (this.vite) port = 80

return port
}
Expand Down
6 changes: 6 additions & 0 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ WORKDIR /app
# Set production environment
<%- emitEnv(baseEnv) %>
<% } else if (vite) { -%>
FROM nginx
<% } else { -%>
FROM base
<% } -%>
Expand All @@ -127,7 +129,11 @@ RUN apt-get update -qq && \
<% } -%>
# Copy built application
<% if (vite) { -%>
COPY --from=build /app/dist /usr/share/nginx/html
<% } else { -%>
COPY --from=build /app /app
<% } -%>

<% if (false && !options.root) /* needs more testing */ { -%>
# Run as a non-root user for security
Expand Down
1 change: 1 addition & 0 deletions test/frameworks/vite/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
45 changes: 45 additions & 0 deletions test/frameworks/vite/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=21.2.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Vite"

# Vite app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci --include=dev

# Copy application code
COPY --link . .

# Build application
RUN npm run build

# Remove development dependencies
RUN npm prune --omit=dev


# Final stage for app image
FROM nginx

# Copy built application
COPY --from=build /app/dist /usr/share/nginx/html

# Start the server by default, this can be overwritten at runtime
EXPOSE 80
CMD [ "/usr/sbin/nginx", "-g", "daemon off;" ]
Loading

0 comments on commit ce1dd43

Please sign in to comment.