From 35a9303ad02761034f31ad02fbfe62bcfaa12f4a Mon Sep 17 00:00:00 2001 From: martin-mfg <2026226+martin-mfg@users.noreply.github.com> Date: Sat, 4 Oct 2025 18:26:57 +0000 Subject: [PATCH] add dotenv browser stub --- frontend/frontend/craco.config.js | 37 ++++++++++---------- frontend/frontend/src/dotenv-browser-stub.js | 5 +++ 2 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 frontend/frontend/src/dotenv-browser-stub.js diff --git a/frontend/frontend/craco.config.js b/frontend/frontend/craco.config.js index ccc64e54..23444e5b 100644 --- a/frontend/frontend/craco.config.js +++ b/frontend/frontend/craco.config.js @@ -1,27 +1,26 @@ -import webpack from 'webpack'; +const path = require('path'); +const webpack = require('webpack'); -export default { +module.exports = { webpack: { + alias: { + dotenv: path.resolve(__dirname, 'src/dotenv-browser-stub.js'), + }, + configure: (webpackConfig) => { - /* - // Add fallback for Node.js core modules - webpackConfig.resolve = { - ...(webpackConfig.resolve || {}), - fallback: { - ...(webpackConfig.resolve?.fallback || {}), - querystring: require.resolve('querystring-es3'), - }, + webpackConfig.resolve.fallback = { + ...webpackConfig.resolve.fallback, + pg: false, }; - */ + + // Turn { KEY: "value" } into { "process.env.KEY": JSON.stringify("value") } + const env = {}; + const envKeys = Object.keys(env).reduce((prev, next) => { + prev[`process.env.${next}`] = JSON.stringify(env[next]); + return prev; + }, {}); + webpackConfig.plugins.push(new webpack.DefinePlugin(envKeys)); - // Inject process.env so server code can access PAT_1 - webpackConfig.plugins.push( - new webpack.DefinePlugin({ - 'process.env': { - PAT_1: JSON.stringify('BrowserPAT'), // <-- your env var - }, - }), - ); return webpackConfig; }, }, diff --git a/frontend/frontend/src/dotenv-browser-stub.js b/frontend/frontend/src/dotenv-browser-stub.js new file mode 100644 index 00000000..c8a3772b --- /dev/null +++ b/frontend/frontend/src/dotenv-browser-stub.js @@ -0,0 +1,5 @@ +// Safe browser stub for dotenv +export function config() { + console.warn('dotenv.config() is a no-op in the browser.'); + return { parsed: {} }; +}