add dotenv browser stub

This commit is contained in:
martin-mfg
2025-10-04 18:26:57 +00:00
parent 46a9b085ed
commit 35a9303ad0
2 changed files with 23 additions and 19 deletions
+18 -19
View File
@@ -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;
},
},
@@ -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: {} };
}