Skip to content

Commit

Permalink
feat: Added vite-plugin-env-compatible to use VUE_APP env prefix and …
Browse files Browse the repository at this point in the history
…process.env
  • Loading branch information
Aldemylla committed Apr 2, 2024
1 parent 27e271a commit 4aae31a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@weni/unnnic-system": "^2.0.0-beta.0",
"apexcharts": "^3.45.2",
"axios": "^1.6.8",
"vite-plugin-env-compatible": "^2.0.1",
"vue": "^3.3.11",
"vue-router": "^4.2.5",
"vue3-apexcharts": "^1.4.4",
Expand Down
9 changes: 5 additions & 4 deletions src/services/api/resources/GPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import env from '@/utils/env';

const http = axios.create({
headers: {
Authorization: env('VITE_GPT_AUTH'),
Authorization: env('VUE_APP_GPT_AUTH'),
},
});

export default {
async getInsights(prompt) {
console.log(import.meta.env);
const URL = import.meta.env.VITE_GPT_URL;
const URL = env('VUE_APP_GPT_URL');

if (!URL) {
throw new Error(`The VITE_GPT_URL environment variable is empty: ${URL}`);
throw new Error(
`The VUE_APP_GPT_URL environment variable is empty: ${URL}`,
);
}

const response = await http.post(URL, {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/env.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default function env(name) {
return (
import.meta.env?.[name] ||
window?.configs?.[name] ||
window?.configs?.[`VITE_${name}`]
process.env[name] ||
window?.configs?.[`VUE_APP_${name}`] ||
process.env[`VUE_APP_${name}`]
);
}
34 changes: 14 additions & 20 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import { fileURLToPath, URL } from 'node:url';

import { defineConfig, loadEnv } from 'vite';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import env from 'vite-plugin-env-compatible';

// https://vitejs.dev/config/
export default ({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');

return defineConfig({
define: {
'process.env': env,
},
plugins: [vue(), vueJsx()],
css: {
preprocessorOptions: {
scss: {
additionalData: `
export default defineConfig({
plugins: [vue(), vueJsx(), env({ prefix: 'VUE_APP' })],
css: {
preprocessorOptions: {
scss: {
additionalData: `
@import '@weni/unnnic-system/src/assets/scss/unnnic.scss';
`,
},
},
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
});
};
},
});
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,16 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dotenv-expand@5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0"
integrity sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==

dotenv@8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

electron-to-chromium@^1.4.601:
version "1.4.626"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.626.tgz#c20e1706354a31721b65e81496800534dd04b222"
Expand Down Expand Up @@ -3201,6 +3211,14 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"

vite-plugin-env-compatible@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/vite-plugin-env-compatible/-/vite-plugin-env-compatible-2.0.1.tgz#9f9f3bc973d6d49b5ad41c14f9b66a691f96eac3"
integrity sha512-DRrOZTg/W44ojVQQfGSMPEgYQGzp5TeIpt9cpaK35hTOC/b2D7Ffl8/RIgK8vQ0mlnDIUgETcA173bnMEkyzdw==
dependencies:
dotenv "8.2.0"
dotenv-expand "5.1.0"

vite@^5.0.10:
version "5.0.11"
resolved "https://registry.yarnpkg.com/vite/-/vite-5.0.11.tgz#31562e41e004cb68e1d51f5d2c641ab313b289e4"
Expand Down

0 comments on commit 4aae31a

Please sign in to comment.