1
0
mirror of https://github.com/bitwarden/web synced 2025-12-06 00:03:28 +00:00

New client configuration pattern (#937)

* adding in initial config files

* working config files

* updating the client config pattern to default to dev instead of prod

* updating the npm script commands and docs

* Adding a helpful debugging log for the webpack build

* adding in more supporting documentation for running against production

* updating README.md and removing the unneeded ENV var
This commit is contained in:
Joseph Flinn
2021-04-21 11:29:33 -07:00
committed by GitHub
parent ad40c38ca3
commit 6e4782784c
8 changed files with 90 additions and 40 deletions

1
.gitignore vendored
View File

@@ -11,3 +11,4 @@ dist/
*.zip *.zip
build/ build/
!dev-server.shared.pem !dev-server.shared.pem
config/development.json

View File

@@ -40,26 +40,26 @@ If you want to point the development web vault to the production APIs, you can r
``` ```
npm install npm install
npm run build:prod:watch ENV=production npm run build:watch
``` ```
You can also manually adjusting your API endpoint settings in `webpack.config.js` by altering the `targets` within `proxy`. For example: You can also manually adjusting your API endpoint settings by adding `config/development.js` overriding any of the values in `config/base.json`. For example:
```typescript ```typescript
proxy: { {
'/api': { "proxyApi": "http://your-api-url",
target: 'http://localhost:4000', "proxyIdentity": "http://your-identity-url",
pathRewrite: {'^/api' : ''} "proxyEvents": "http://your-events-url",
}, "proxyNotifications": "http://your-notifications-url",
'/identity': { "proxyPortal": "http://your-portal-url",
target: 'http://localhost:33656', "allowedHosts": ["hostnames-to-allow-in-webpack"]
pathRewrite: {'^/identity' : ''} }
}, ```
'/events': {
target: 'http://localhost:46273', To pick up the overrides in the newly created `config/development.js` file, run the app with:
pathRewrite: {'^/events' : ''}
} ```
}, npm run build:dev:watch
``` ```
## Contribute ## Contribute

29
config.js Normal file
View File

@@ -0,0 +1,29 @@
function load(envName) {
const envOverrides = {
'production': () => require('./config/production.json'),
'qa': () => require('./config/qa.json'),
'development': () => require('./config/development.json'),
};
const baseConfig = require('./config/base.json');
const overrideConfig = envOverrides.hasOwnProperty(envName) ? envOverrides[envName]() : {};
return {
...baseConfig,
...overrideConfig
};
}
function log(configObj) {
const repeatNum = 50
console.log(`${"=".repeat(repeatNum)}\nenvConfig`)
Object.entries(configObj).map(([key, value]) => {
console.log(` ${key}: ${value}`)
})
console.log(`${"=".repeat(repeatNum)}`)
}
module.exports = {
load,
log
};

8
config/base.json Normal file
View File

@@ -0,0 +1,8 @@
{
"proxyApi": "http://localhost:4000",
"proxyIdentity": "http://localhost:33656",
"proxyEvents": "http://localhost:46273",
"proxyNotifications": "http://localhost:61840",
"proxyPortal": "http://localhost:52313",
"allowedHosts": []
}

7
config/production.json Normal file
View File

@@ -0,0 +1,7 @@
{
"proxyApi": "https://api.bitwarden.com",
"proxyIdentity": "https://identity.bitwarden.com",
"proxyEvents": "https://events.bitwarden.com",
"proxyNotifications": "https://notifications.bitwarden.com",
"proxyPortal": "https://portal.bitwarden.com"
}

7
config/qa.json Normal file
View File

@@ -0,0 +1,7 @@
{
"proxyApi": "https://api.qa.bitwarden.com",
"proxyIdentity": "https://identity.qa.bitwarden.com",
"proxyEvents": "https://events.qa.bitwarden.com",
"proxyNotifications": "https://notifications.qa.bitwarden.com",
"proxyPortal": "https://portal.qa.bitwarden.com"
}

View File

@@ -13,8 +13,12 @@
"symlink:lin": "rm -rf ./jslib && ln -s ../jslib ./jslib", "symlink:lin": "rm -rf ./jslib && ln -s ../jslib ./jslib",
"build": "gulp prebuild && webpack", "build": "gulp prebuild && webpack",
"build:watch": "gulp prebuild && webpack-dev-server", "build:watch": "gulp prebuild && webpack-dev-server",
"build:prod": "gulp prebuild && cross-env NODE_ENV=production webpack", "build:dev": "gulp prebuild && cross-env ENV=development webpack",
"build:prod:watch": "gulp prebuild && cross-env NODE_ENV=production webpack-dev-server", "build:dev:watch": "gulp prebuild && cross-env ENV=development webpack-dev-server",
"build:qa": "gulp prebuild && cross-env NODE_ENV=production ENV=qa webpack",
"build:qa:watch": "gulp prebuild && cross-env NODE_ENV=production ENV=qa webpack-dev-server",
"build:prod": "gulp prebuild && cross-env NODE_ENV=production ENV=production webpack",
"build:prod:watch": "gulp prebuild && cross-env NODE_ENV=production ENV=production webpack-dev-server",
"build:selfhost": "gulp prebuild && cross-env SELF_HOST=true webpack-dev-server", "build:selfhost": "gulp prebuild && cross-env SELF_HOST=true webpack-dev-server",
"build:selfhost:watch": "gulp prebuild && cross-env SELF_HOST=true webpack-dev-server", "build:selfhost:watch": "gulp prebuild && cross-env SELF_HOST=true webpack-dev-server",
"build:selfhost:prod": "gulp prebuild && cross-env SELF_HOST=true NODE_ENV=production webpack", "build:selfhost:prod": "gulp prebuild && cross-env SELF_HOST=true NODE_ENV=production webpack",

View File

@@ -8,11 +8,15 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin'); const TerserPlugin = require('terser-webpack-plugin');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin; const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const pjson = require('./package.json'); const pjson = require('./package.json');
const config = require('./config.js')
if (process.env.NODE_ENV == null) { if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
} }
const ENV = process.env.ENV = process.env.NODE_ENV;
const NODE_ENV = process.env.NODE_ENV;
const envConfig = config.load(process.env.ENV)
config.log(envConfig)
const moduleRules = [ const moduleRules = [
{ {
@@ -123,7 +127,7 @@ const plugins = [
}), }),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.env': { 'process.env': {
'ENV': JSON.stringify(ENV), 'ENV': JSON.stringify(NODE_ENV),
'SELF_HOST': JSON.stringify(process.env.SELF_HOST === 'true' ? true : false), 'SELF_HOST': JSON.stringify(process.env.SELF_HOST === 'true' ? true : false),
'APPLICATION_VERSION': JSON.stringify(pjson.version), 'APPLICATION_VERSION': JSON.stringify(pjson.version),
'CACHE_TAG': JSON.stringify(Math.random().toString(36).substring(7)), 'CACHE_TAG': JSON.stringify(Math.random().toString(36).substring(7)),
@@ -131,7 +135,7 @@ const plugins = [
}), }),
]; ];
if (ENV === 'production') { if (NODE_ENV === 'production') {
moduleRules.push({ moduleRules.push({
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
loader: '@ngtools/webpack', loader: '@ngtools/webpack',
@@ -159,52 +163,42 @@ const devServer = {
// host: '192.168.1.9', // host: '192.168.1.9',
proxy: { proxy: {
'/api': { '/api': {
target: 'http://localhost:4000', target: envConfig['proxyApi'],
pathRewrite: {'^/api' : ''}, pathRewrite: {'^/api' : ''},
secure: false, secure: false,
changeOrigin: true changeOrigin: true
}, },
'/identity': { '/identity': {
target: 'http://localhost:33656', target: envConfig['proxyIdentity'],
pathRewrite: {'^/identity' : ''}, pathRewrite: {'^/identity' : ''},
secure: false, secure: false,
changeOrigin: true changeOrigin: true
}, },
'/events': { '/events': {
target: 'http://localhost:46273', target: envConfig['proxyEvents'],
pathRewrite: {'^/events' : ''}, pathRewrite: {'^/events' : ''},
secure: false, secure: false,
changeOrigin: true changeOrigin: true
}, },
'/notifications': { '/notifications': {
target: 'http://localhost:61840', target: envConfig['proxyNotifications'],
pathRewrite: {'^/notifications' : ''}, pathRewrite: {'^/notifications' : ''},
secure: false, secure: false,
changeOrigin: true changeOrigin: true
}, },
'/portal': { '/portal': {
target: 'http://localhost:52313', target: envConfig['proxyPortal'],
pathRewrite: {'^/portal' : ''}, pathRewrite: {'^/portal' : ''},
secure: false, secure: false,
changeOrigin: true changeOrigin: true
} }
}, },
hot: false, hot: false,
allowedHosts: [ allowedHosts: envConfig['allowedHosts']
'bitwarden.test',
],
}; };
if (ENV === "production") { const webpackConfig = {
devServer.proxy['/api'].target = 'https://api.bitwarden.com'; mode: NODE_ENV,
devServer.proxy['/identity'].target = 'https://identity.bitwarden.com';
devServer.proxy['/events'].target = 'https://events.bitwarden.com';
devServer.proxy['/notifications'].target = 'https://notifications.bitwarden.com';
devServer.proxy['/portal'].target = 'https://portal.bitwarden.com';
}
const config = {
mode: ENV,
devtool: 'source-map', devtool: 'source-map',
devServer: devServer, devServer: devServer,
entry: { entry: {
@@ -257,4 +251,4 @@ const config = {
plugins: plugins, plugins: plugins,
}; };
module.exports = config; module.exports = webpackConfig;