Skip to content

Commit

Permalink
fec.config.js: preparation to get process.env.MSW working
Browse files Browse the repository at this point in the history
  • Loading branch information
schuellerf authored and regexowl committed Aug 19, 2024
1 parent afb4285 commit 7f1b8b7
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions fec.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ const webpack = require('webpack');

const plugins = [];

function add_define(key, value) {
const definePluginIndex = plugins.findIndex(
(plugin) => plugin instanceof webpack.DefinePlugin
);
if (definePluginIndex !== -1) {
const definePlugin = plugins[definePluginIndex];

const newDefinePlugin = new webpack.DefinePlugin({
...definePlugin.definitions,
[key]: JSON.stringify(value),
});

plugins[definePluginIndex] = newDefinePlugin;
} else {
plugins.push(
new webpack.DefinePlugin({
[key]: JSON.stringify(value),
})
);
}
}

if (process.env.MSW) {
// Copy mockServiceWorker.js to ./dist/ so it is served with the bundle
plugins.push(
Expand All @@ -25,17 +47,11 @@ if (process.env.MSW) {
Therefore, we find it in the `plugins` array based on its type, then update
it to add our new process.env.MSW variable.
*/
const definePluginIndex = plugins.findIndex(
(plugin) => plugin instanceof webpack.DefinePlugin
);
const definePlugin = plugins[definePluginIndex];

const newDefinePlugin = new webpack.DefinePlugin({
...definePlugin.definitions,
'process.env.MSW': true,
});
add_define('process.env.MSW', process.env.MSW);
}

plugins[definePluginIndex] = newDefinePlugin;
if (process.env.NODE_ENV) {
add_define('process.env.NODE_ENV', process.env.NODE_ENV);
}

module.exports = {
Expand Down

0 comments on commit 7f1b8b7

Please sign in to comment.