Webpack: refactor devserver env

This commit is contained in:
Alex Gleason 2021-11-03 23:41:55 -05:00
parent b516ff0b03
commit bcc55e9e85
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -7,9 +7,17 @@ const sharedConfig = require('./shared');
const watchOptions = {}; const watchOptions = {};
const backendUrl = process.env.BACKEND_URL || 'http://localhost:4000'; const {
const patronUrl = process.env.PATRON_URL || 'http://localhost:3037'; DEVSERVER_URL,
const secureProxy = !(process.env.PROXY_HTTPS_INSECURE === 'true'); BACKEND_URL,
PATRON_URL,
PROXY_HTTPS_INSECURE,
} = process.env;
const DEFAULTS = {
DEVSERVER_URL: 'http://localhost:3036',
PATRON_URL: 'http://localhost:3037',
};
const { FE_SUBDIRECTORY } = require(join(__dirname, '..', 'app', 'soapbox', 'build_config')); const { FE_SUBDIRECTORY } = require(join(__dirname, '..', 'app', 'soapbox', 'build_config'));
@ -28,15 +36,17 @@ const backendEndpoints = [
]; ];
const makeProxyConfig = () => { const makeProxyConfig = () => {
const secureProxy = PROXY_HTTPS_INSECURE !== 'true';
const proxyConfig = {}; const proxyConfig = {};
proxyConfig['/api/patron'] = { proxyConfig['/api/patron'] = {
target: patronUrl, target: PATRON_URL || DEFAULTS.PATRON_URL,
secure: secureProxy, secure: secureProxy,
changeOrigin: true, changeOrigin: true,
}; };
backendEndpoints.map(endpoint => { backendEndpoints.map(endpoint => {
proxyConfig[endpoint] = { proxyConfig[endpoint] = {
target: backendUrl, target: BACKEND_URL || DEFAULTS.BACKEND_URL,
secure: secureProxy, secure: secureProxy,
changeOrigin: true, changeOrigin: true,
}; };
@ -51,6 +61,14 @@ if (process.env.VAGRANT) {
watchOptions.poll = 1000; watchOptions.poll = 1000;
} }
const devServerUrl = (() => {
try {
return new URL(DEVSERVER_URL);
} catch {
return new URL(DEFAULTS.DEVSERVER_URL);
}
})();
module.exports = merge(sharedConfig, { module.exports = merge(sharedConfig, {
mode: 'development', mode: 'development',
cache: true, cache: true,
@ -73,9 +91,9 @@ module.exports = merge(sharedConfig, {
devServer: { devServer: {
compress: true, compress: true,
host: 'localhost', host: devServerUrl.hostname,
port: 3036, port: devServerUrl.port,
https: false, https: devServerUrl.protocol === 'https:',
hot: false, hot: false,
allowedHosts: 'all', allowedHosts: 'all',
historyApiFallback: { historyApiFallback: {