Skip to content

Commit

Permalink
refactor: move webpack config logic to node_modules via patch-package
Browse files Browse the repository at this point in the history
* add JS/JSX compatability to jest and webpack configs
  • Loading branch information
jsnwesson committed Feb 9, 2024
1 parent 8115b51 commit f1347b9
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 28 deletions.
113 changes: 113 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"glob": "7.2.3",
"husky": "7.0.4",
"jest": "27.5.1",
"patch-package": "8.0.0",
"resize-observer-polyfill": "^1.5.1",
"rosie": "2.1.1",
"sharp": "0.32.6"
Expand Down
91 changes: 91 additions & 0 deletions patches/@edx+frontend-build+13.0.14.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
diff --git a/node_modules/@edx/frontend-build/config/jest.config.js b/node_modules/@edx/frontend-build/config/jest.config.js
index ae08b70..952ccc3 100644
--- a/node_modules/@edx/frontend-build/config/jest.config.js
+++ b/node_modules/@edx/frontend-build/config/jest.config.js
@@ -3,12 +3,12 @@ const fs = require('fs');

const presets = require('../lib/presets');

-let envConfigPath = path.resolve(__dirname, './jest/fallback.env.config.js');
-const appEnvConfigPath = path.resolve(process.cwd(), './env.config.js');
+const appEnvConfigPathJs = path.resolve(process.cwd(), './env.config.js');
+const appEnvConfigPathJsx = path.resolve(process.cwd(), './env.config.jsx');

-if (fs.existsSync(appEnvConfigPath)) {
- envConfigPath = appEnvConfigPath;
-}
+const envConfigPath = fs.existsSync(appEnvConfigPathJs) ? appEnvConfigPathJs
+ : fs.existsSync(appEnvConfigPathJsx) ? appEnvConfigPathJsx
+ : path.resolve(__dirname, './jest/fallback.env.config.js');

module.exports = {
testURL: 'http://localhost/',
diff --git a/node_modules/@edx/frontend-build/config/webpack.dev.config.js b/node_modules/@edx/frontend-build/config/webpack.dev.config.js
index 5ce7716..932a853 100644
--- a/node_modules/@edx/frontend-build/config/webpack.dev.config.js
+++ b/node_modules/@edx/frontend-build/config/webpack.dev.config.js
@@ -7,6 +7,7 @@ const Dotenv = require('dotenv-webpack');
const dotenv = require('dotenv');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
+const fs = require('fs');
const PostCssAutoprefixerPlugin = require('autoprefixer');
const PostCssRTLCSS = require('postcss-rtlcss');
const PostCssCustomMediaCSS = require('postcss-custom-media');
@@ -17,6 +18,12 @@ const presets = require('../lib/presets');
const resolvePrivateEnvConfig = require('../lib/resolvePrivateEnvConfig');
const getLocalAliases = require('./getLocalAliases');

+const envConfigPathJs = path.resolve(process.cwd(),'./env.config.js');
+const envConfigPathJsx = path.resolve(process.cwd(), './env.config.jsx');
+const envConfig = fs.existsSync(envConfigPathJs) ? require(envConfigPathJs)
+ : fs.existsSync(envConfigPathJsx) ? require(envConfigPathJsx)
+ : {};
+
// Add process env vars. Currently used only for setting the
// server port and the publicPath
dotenv.config({
@@ -174,7 +181,7 @@ module.exports = merge(commonConfig, {
// reloading.
devServer: {
host: '0.0.0.0',
- port: process.env.PORT || 8080,
+ port: envConfig.PORT || process.env.PORT || 8080,
historyApiFallback: {
index: path.join(PUBLIC_PATH, 'index.html'),
disableDotRule: true,
diff --git a/node_modules/@edx/frontend-build/config/webpack.prod.config.js b/node_modules/@edx/frontend-build/config/webpack.prod.config.js
index 2879dd9..64c2e7b 100644
--- a/node_modules/@edx/frontend-build/config/webpack.prod.config.js
+++ b/node_modules/@edx/frontend-build/config/webpack.prod.config.js
@@ -12,6 +12,7 @@ const NewRelicSourceMapPlugin = require('@edx/new-relic-source-map-webpack-plugi
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
+const fs = require('fs');
const PostCssAutoprefixerPlugin = require('autoprefixer');
const PostCssRTLCSS = require('postcss-rtlcss');
const PostCssCustomMediaCSS = require('postcss-custom-media');
@@ -23,6 +24,22 @@ const HtmlWebpackNewRelicPlugin = require('../lib/plugins/html-webpack-new-relic
const commonConfig = require('./webpack.common.config');
const presets = require('../lib/presets');

+/** This condition confirms whether the configuration for the MFE has switched to a JS-based configuration
+ * as previously implemented in frontend-build and frontend-platform. If the environment variable exists, then
+ * an env.config.js file will be created at the root directory and its env variables can be accessed with getConfig().
+ *
+ * https://github.com/openedx/frontend-build/blob/master/docs/0002-js-environment-config.md
+ * https://github.com/openedx/frontend-platform/blob/master/docs/decisions/0007-javascript-file-configuration.rst
+ */
+const envConfigPath = process.env.JS_CONFIG_FILEPATH;
+if (envConfigPath) {
+ const envConfigFilename = envConfigPath.slice(envConfigPath.indexOf('env.config'));
+
+ fs.copyFile(process.env.JS_CONFIG_FILEPATH, envConfigFilename, (err) => {
+ if (err) { throw err; }
+ });
+}
+
// Add process env vars. Currently used only for setting the PUBLIC_PATH.
dotenv.config({
path: path.resolve(process.cwd(), '.env'),
9 changes: 0 additions & 9 deletions webpack.dev.config.js

This file was deleted.

19 changes: 0 additions & 19 deletions webpack.prod.config.js

This file was deleted.

0 comments on commit f1347b9

Please sign in to comment.