Skip to content

Commit b48cd3a

Browse files
committed
Refactor environment variable handling in webpack configuration to return missing variables and log warnings, improving build resilience.
1 parent 17b1bff commit b48cd3a

File tree

4 files changed

+27
-36
lines changed

4 files changed

+27
-36
lines changed

common/scripts/env-webpack-helper.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* 3. Only define variables that are explicitly declared in .env file
2424
*
2525
* @param {Object} env - Parsed environment variables from .env file (from dotenv.config().parsed)
26-
* @returns {Object} Environment variables object ready for webpack.DefinePlugin
26+
* @returns {Object} Object containing envKeys for webpack.DefinePlugin and missingVars array
2727
*/
2828
function createEnvDefinePlugin(env) {
2929

@@ -44,14 +44,7 @@ function createEnvDefinePlugin(env) {
4444
});
4545
}
4646

47-
if (missingVars.length > 0) {
48-
throw new Error(
49-
`Missing required environment variables: ${missingVars.join(', ')}\n` +
50-
`Please provide values in either .env file or runtime environment.\n`
51-
);
52-
}
53-
54-
return envKeys;
47+
return { envKeys, missingVars };
5548
}
5649

5750
module.exports = {

workspaces/ballerina/ballerina-extension/webpack.config.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ const { createEnvDefinePlugin } = require('../../../common/scripts/env-webpack-h
1010

1111
const envPath = path.resolve(__dirname, '.env');
1212
const env = dotenv.config({ path: envPath }).parsed;
13-
14-
let envKeys;
15-
try {
16-
envKeys = createEnvDefinePlugin(env);
17-
} catch (error) {
18-
console.warn('\n⚠️ Environment Variable Configuration Warning:');
19-
console.warn(error.message);
20-
console.warn('Continuing build with empty environment variables...');
21-
envKeys = {};
13+
console.log("Fetching values for environment variables...");
14+
const { envKeys, missingVars } = createEnvDefinePlugin(env);
15+
if (missingVars.length > 0) {
16+
console.warn(
17+
'\n⚠️ Environment Variable Configuration Warning:\n' +
18+
`Missing required environment variables: ${missingVars.join(', ')}\n` +
19+
`Please provide values in either .env file or runtime environment.\n`
20+
);
2221
}
2322

2423
/** @type {import('webpack').Configuration} */

workspaces/mi/mi-extension/webpack.config.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ const { createEnvDefinePlugin } = require('../../../common/scripts/env-webpack-h
2727

2828
const envPath = path.resolve(__dirname, '.env');
2929
const env = dotenv.config({ path: envPath }).parsed;
30-
31-
let envKeys;
32-
try {
33-
envKeys = createEnvDefinePlugin(env);
34-
} catch (error) {
35-
console.warn('\n⚠️ Environment Variable Configuration Warning:');
36-
console.warn(error.message);
37-
console.warn('Continuing build with empty environment variables...');
38-
envKeys = {};
30+
console.log("Fetching values for environment variables...");
31+
const { envKeys, missingVars } = createEnvDefinePlugin(env);
32+
if (missingVars.length > 0) {
33+
console.warn(
34+
'\n⚠️ Environment Variable Configuration Warning:\n' +
35+
`Missing required environment variables: ${missingVars.join(', ')}\n` +
36+
`Please provide values in either .env file or runtime environment.\n`
37+
);
3938
}
4039

4140
/** @type {import('webpack').Configuration} */

workspaces/wso2-platform/wso2-platform-extension/webpack.config.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ const { createEnvDefinePlugin } = require('../../../common/scripts/env-webpack-h
2626

2727
const envPath = path.resolve(__dirname, ".env");
2828
const env = dotenv.config({ path: envPath }).parsed;
29-
30-
let envKeys;
31-
try {
32-
envKeys = createEnvDefinePlugin(env);
33-
} catch (error) {
34-
console.warn('\n⚠️ Environment Variable Configuration Warning:');
35-
console.warn(error.message);
36-
console.warn('Continuing build with empty environment variables...');
37-
envKeys = {};
29+
console.log("Fetching values for environment variables...");
30+
const { envKeys, missingVars } = createEnvDefinePlugin(env);
31+
if (missingVars.length > 0) {
32+
console.warn(
33+
'\n⚠️ Environment Variable Configuration Warning:\n' +
34+
`Missing required environment variables: ${missingVars.join(', ')}\n` +
35+
`Please provide values in either .env file or runtime environment.\n`
36+
);
3837
}
38+
3939
//@ts-check
4040
/** @typedef {import('webpack').Configuration} WebpackConfig **/
4141

0 commit comments

Comments
 (0)