Comments (6)
This is would be my proposed solution. Instead of using dotenv, use the vite function loadEnv()
inside the default function. This way you can access the correct mode and also use vite's env loading priorities. You would have to pass the two correct env prefixes in order to access the both the KIRBY_
and VITE_
prefixed variables. You could rename also CONTENT_API_SLUG
as VITE_BACKEND_API_SLUG
directly in the env file and change the reference in your kirby plugins, or also pass CONTENT_
as a prefix. I've tested this setup and it works fine.
import { resolve } from "path";
import { defineConfig, loadEnv } from "vite";
import Vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
const root = "src";
export default ({ mode }) => {
process.env = {
...process.env,
...loadEnv(mode, process.cwd(), ["VITE_", "KIRBY_"]),
};
process.env.VITE_BACKEND_URL = `${process.env.KIRBY_DEV_PROTOCOL}://${process.env.KIRBY_DEV_HOSTNAME}:${process.env.KIRBY_DEV_PORT}`;
process.env.VITE_MULTILANG = process.env.KIRBY_MULTILANG;
return defineConfig({
root,
...
from kirby-vue3-starterkit.
You wil have to follow the official .env
file naming conventions supported by Vite.
The file .env.development.example
is intended to be renamed to .env
, not .env.development
. Make sure to follow the instructions on setting up the project.
from kirby-vue3-starterkit.
Ok, I think I had misunderstood how you had this setup. I was thinking I could use multiple .env files, for example have a .env.staging file and run vite build --mode staging
to have it load that file.
from kirby-vue3-starterkit.
I think you got it right, the staging file should be loaded by Vite. 🤔
from kirby-vue3-starterkit.
The Vite way is to use the mode argument from the defineConfig function and load the correct env file based on that, see here: https://vitejs.dev/config/#environment-variables.
It seems that dotenv does not automatically load different env files. And it needs to be done like this:
const whichenv = process.env.NODE_ENV === "production" ? `.${process.env.NODE_ENV}` : ""; require("dotenv").config({ path: `.env${whichenv}` });
Which theoretically means you could use a .env and a .env.production file, but only these two as node itself only has two modes. In order to leverage vite's custom "mode" parameter i.e: vite build --mode custommode
then we need to read the mode argument and load the .env accordingly: const env = loadEnv(mode, process.cwd(), '')
.
This would be fine, except then we can only read variables starting with VITE_, which breaks your process.env.VITE_BACKEND_URL, process.env.VITE_BACKEND_API_SLUG, process.env.VITE_MULTILANG
variables.
I think it's not too hard to find a solution, it would just mean eith: using the vite way of loading env files and renaming the three non "VITE_" vars used. Or alternatively having all vars in one .env file and using a naming convention like:
KIRBY_DEV_HOSTNAME
KIRBY_STAGE_HOSTNAME
CONTENT_DEV_API_SLUG
CONTENT_STAGE_API_SLUG
What do you think
from kirby-vue3-starterkit.
Thanks for your input and feedback. Looks interesting! Could you open a PR, please?
from kirby-vue3-starterkit.
Related Issues (20)
- Working with layouts and blocks HOT 9
- Deployed vite scripts refers to localhost HOT 4
- (?) Best Practice: Using Tags for filtered Collections HOT 1
- Kirby caches path to assets? HOT 1
- router-view transition won't fade in on router.replace HOT 1
- PHP / Kirby Routing HOT 4
- Sitemap.xml HOT 1
- Build Issue: Page not found HOT 1
- Deployment: What and how? HOT 17
- Vue.js devtools HOT 2
- Replacing default image block snippet HOT 4
- Meta-tags kirby-extended HOT 1
- Storing and accessing development/production variables in env HOT 1
- Alias HOT 1
- Queries not working HOT 1
- Draft preview not working HOT 3
- create pages from frontend HOT 1
- Outsource internal `kirby-vue-kit` into own repository HOT 1
- Custom language variables best practice HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kirby-vue3-starterkit.