Sign inSign up

georgecraggs/vite-server

By georgecraggs

•Updated almost 2 years ago

A static server (designed for Vite) that allows environment variables to be subsisted at runtime.

Image
0

512

georgecraggs/vite-server repository overview

⁠Vite Runtime Env Server

A dead simple static site server (designed for Vite projects) that allows environment variables to be subsisted at runtime.

⁠Usage

FROM node:18-alpine as build
WORKDIR /usr/app
COPY . /usr/app
RUN yarn
RUN yarn build

FROM georgecraggs/vite-server:1
COPY --from=build /usr/app/dist /dist

⁠Caveats

  • We will only replace variables that start with "VITE_". This allows you to set the variables using Vite as normal in development.
  • The environment variable cannot be set when the project is built. In this case Vite will replace the variable with the value during build, so it can't be replaced later. I like to set the variables in .env.development so I can easily test the project in development mode.

⁠How it works

During the build process, Vite will do string replacements of any uses of environment variables that start with "VITE_" to match the value at build time.

If an environment variable isn't set, it will replace "import.meta.env" with "{}".

For each environment variable beginning with "VITE_" we do a find/replace for "{}.VITE_...". If any replacements are made in the file it's cached in memory, so we don't have to perform the replacements for every request.

Tag summary

Content type

Image

Digest

sha256:f4adcf9bd…

Size

7.8 MB

Last updated

almost 2 years ago

docker pull georgecraggs/vite-server