Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a helper function in frontend-build to retrieve relative path from webpack to env.config.js #283

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions patches/@edx+frontend-build+13.0.14.patch
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,23 @@ index 5ce7716..fe9888e 100644
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..4cd1e42 100644
index 2879dd9..555be8f 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
@@ -12,17 +12,39 @@ 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,25 @@ const HtmlWebpackNewRelicPlugin = require('../lib/plugins/html-webpack-new-relic

// Reduce CSS file size by ~70%
const purgecss = require('@fullhuman/postcss-purgecss');
-
const HtmlWebpackNewRelicPlugin = require('../lib/plugins/html-webpack-new-relic-plugin');
+const resolveRelativePath = require('../lib/resolveRelativePath')
const commonConfig = require('./webpack.common.config');
const presets = require('../lib/presets');

Expand All @@ -88,7 +93,9 @@ index 2879dd9..4cd1e42 100644
+
+if (envConfigPath) {
+ const envConfigFilename = envConfigPath.slice(envConfigPath.indexOf('env.config'));
+ fs.copyFileSync(envConfigPath, envConfigFilename);
+ const relEnvConfigFilePath = resolveRelativePath(process.cwd(), envConfigPath)
+
+ fs.copyFileSync(relEnvConfigFilePath, envConfigFilename);
+
+ let newConfigFilepath = path.resolve(process.cwd(), envConfigFilename);
+ envConfig = require(newConfigFilepath);
Expand All @@ -97,7 +104,7 @@ index 2879dd9..4cd1e42 100644
// Add process env vars. Currently used only for setting the PUBLIC_PATH.
dotenv.config({
path: path.resolve(process.cwd(), '.env'),
@@ -45,12 +65,12 @@ if (process.env.ENABLE_NEW_RELIC !== 'false') {
@@ -45,12 +67,12 @@ if (process.env.ENABLE_NEW_RELIC !== 'false') {
agentID: process.env.NEW_RELIC_AGENT_ID || 'undefined_agent_id',
trustKey: process.env.NEW_RELIC_TRUST_KEY || 'undefined_trust_key',
licenseKey: process.env.NEW_RELIC_LICENSE_KEY || 'undefined_license_key',
Expand All @@ -113,3 +120,40 @@ index 2879dd9..4cd1e42 100644
// upload source maps in prod builds only
noop: typeof process.env.NEW_RELIC_ADMIN_KEY === 'undefined',
}));
diff --git a/node_modules/@edx/frontend-build/lib/resolveRelativePath.js b/node_modules/@edx/frontend-build/lib/resolveRelativePath.js
new file mode 100644
index 0000000..7fdbcf5
--- /dev/null
+++ b/node_modules/@edx/frontend-build/lib/resolveRelativePath.js
@@ -0,0 +1,31 @@
+/**
+ * Returns the relative filepath given the directory to start from (FROM) and the target filepath (TO).
+ * Useful for finding files by traversing upward from the starting directory
+ *
+ * @param {string} startDir is string path of where to begin searching for filepath
+ * @param {string} targetFilePath
+ *
+ * @returns {string} - relative path from startDir to targetFilePath
+ * */
+
+const fs = require('fs');
+const path = require('path');
+
+module.exports = (startDir = process.cwd(), targetFilePath) => {
+ let rootOfFilePath = targetFilePath.split('/')[0];
+
+ while(true) {
+ // retrieve list of all directories/files in the directory
+ let list = fs.readdirSync(startDir)
+
+ // searches for rootOfFilePath in list, and will either return
+ // a relative path or reassign startDir to its parent
+ if (list.indexOf(rootOfFilePath) != -1) {
+ return path.join(startDir, targetFilePath);
+ } else if (startDir === '/') {
+ throw new Error(`The target (${targetFilePath}) does not exist in the current working directory or its parents`)
+ } else {
+ startDir = path.normalize(path.join(startDir, '..'));
+ };
+ };
+};
Loading