fix: embed env var EnvrionmentPlugin config (#14225)

* fix: embed env var EnvrionmentPlugin config

* fixup! fix: embed env var EnvrionmentPlugin config

* fixup! fixup! fix: embed env var EnvrionmentPlugin config

* fix: auto switch to VERCEL_URL if WEBAPP_URL is undefined
This commit is contained in:
Morgan authored and GitHub committed 2024-03-28 08:05:13 +00:00
1 parent 6ffe7005dd
commit 22212e7edb
2 files changed
+21 -6

No files matched your search

+8 -6
View File
@@ -1,6 +1,6 @@
import EnvironmentPlugin from "vite-plugin-environment";
import viteBaseConfig from "../vite.config";
import viteBaseConfig, { embedCoreEnvVars } from "../vite.config";
const path = require("path");
const { defineConfig } = require("vite");
@@ -9,6 +9,13 @@ module.exports = defineConfig((configEnv) => {
const config = {
...viteBaseConfig,
base: "/embed/",
plugins: [
EnvironmentPlugin({
EMBED_PUBLIC_EMBED_FINGER_PRINT: embedCoreEnvVars.EMBED_PUBLIC_EMBED_FINGER_PRINT,
EMBED_PUBLIC_VERCEL_URL: embedCoreEnvVars.EMBED_PUBLIC_VERCEL_URL,
EMBED_PUBLIC_WEBAPP_URL: embedCoreEnvVars.EMBED_PUBLIC_WEBAPP_URL,
}),
],
build: {
emptyOutDir: true,
rollupOptions: {
@@ -17,11 +24,6 @@ module.exports = defineConfig((configEnv) => {
embed: path.resolve(__dirname, "src/embed.ts"),
},
plugins: [
EnvironmentPlugin([
"EMBED_PUBLIC_EMBED_FINGER_PRINT",
"EMBED_PUBLIC_VERCEL_URL",
"EMBED_PUBLIC_WEBAPP_URL",
]),
{
generateBundle: (code, bundle) => {
// Note: banner/footer doesn't work because it doesn't enclose the entire library code, some variables are still left out.
+13
View File
@@ -2,13 +2,17 @@ const path = require("path");
require("dotenv").config({ path: path.join(__dirname, "..", "..", ".env") });
process.env.EMBED_PUBLIC_VERCEL_URL = process.env.VERCEL_URL;
process.env.EMBED_PUBLIC_WEBAPP_URL = process.env.NEXT_PUBLIC_WEBAPP_URL;
// eslint-disable-next-line turbo/no-undeclared-env-vars
process.env.EMBED_PUBLIC_EMBED_LIB_URL = process.env.NEXT_PUBLIC_EMBED_LIB_URL;
// eslint-disable-next-line turbo/no-undeclared-env-vars
process.env.EMBED_PUBLIC_EMBED_FINGER_PRINT = process.env.NEXT_PUBLIC_EMBED_FINGER_PRINT;
// Problem: typeof process.env.EMBED_PUBLIC_EMBED_LIB_URL is "undefined"(truthy) if process.env.NEXT_PUBLIC_EMBED_LIB_URL is undefined(falsy)
// This is probably because environment variables are always string, so this weird automatic conversion to string happens
// HACKY Solution
// eslint-disable-next-line turbo/no-undeclared-env-vars
if (process.env.EMBED_PUBLIC_EMBED_LIB_URL === "undefined") {
// eslint-disable-next-line turbo/no-undeclared-env-vars
delete process.env.EMBED_PUBLIC_EMBED_LIB_URL;
}
@@ -24,3 +28,12 @@ const viteBaseConfig = {
envPrefix: "EMBED_PUBLIC_",
};
export default viteBaseConfig;
export const embedCoreEnvVars = {
EMBED_PUBLIC_EMBED_FINGER_PRINT: process.env.EMBED_PUBLIC_EMBED_FINGER_PRINT ?? "",
EMBED_PUBLIC_VERCEL_URL: process.env.EMBED_PUBLIC_VERCEL_URL ?? "http://localhost:3000",
EMBED_PUBLIC_WEBAPP_URL:
process.env.EMBED_PUBLIC_WEBAPP_URL ??
(process.env.EMBED_PUBLIC_VERCEL_URL
? `https://${process.env.EMBED_PUBLIC_VERCEL_URL}`
: "http://localhost:3000"),
};