From 236012dea684050bea013d134b282ce7c631a733 Mon Sep 17 00:00:00 2001 From: sthzg Date: Tue, 20 Oct 2015 00:34:36 +0200 Subject: [PATCH] #1 -- added support to import app configuration based on --env --- README.md | 2 ++ cfg/base.js | 3 ++- cfg/test.js | 3 ++- src/config/README.md | 17 +++++++++++++++++ src/config/dev.js | 7 +++++++ src/config/dist.js | 7 +++++++ src/config/test.js | 7 +++++++ test/config/ConfigTest.js | 12 ++++++++++++ webpack.config.js | 1 + 9 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/config/README.md create mode 100644 src/config/dev.js create mode 100644 src/config/dist.js create mode 100644 src/config/test.js create mode 100644 test/config/ConfigTest.js diff --git a/README.md b/README.md index 6a9ddf7..f8d6c26 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ The following features are planned to be included in the final version: - [x] No dependency on grunt, gulp or the next hot taskrunner! - [x] The dynamic webpack configuration is clunky, there must be a better way to do that! - [x] Karmas webpack configuration is not included in the global webpack.config.js file, but it really should +- [x] Support for environment-specific configuration files ## Todo - [ ] Better performance for webpack builds @@ -71,3 +72,4 @@ react-webpack-template is available under MIT-License and can therefore be used ## Contributors - Weblogixx (cs@weblogixx.de) - Martin Jul (martin@mjul.com) +- Stephan Herzog (sthzgvie@gmail.com) diff --git a/cfg/base.js b/cfg/base.js index b24bbbb..d6a0684 100644 --- a/cfg/base.js +++ b/cfg/base.js @@ -27,7 +27,8 @@ module.exports = { components: srcPath + '/components/', sources: srcPath + '/sources/', stores: srcPath + '/stores/', - styles: srcPath + '/styles/' + styles: srcPath + '/styles/', + config: srcPath + '/config/' + process.env.REACT_WEBPACK_ENV } }, module: { diff --git a/cfg/test.js b/cfg/test.js index e4682e8..f2e3e4e 100644 --- a/cfg/test.js +++ b/cfg/test.js @@ -27,7 +27,8 @@ module.exports = { components: srcPath + 'components/', sources: srcPath + 'sources/', stores: srcPath + 'stores/', - styles: srcPath + 'styles/' + styles: srcPath + 'styles/', + config: srcPath + 'config/' + process.env.REACT_WEBPACK_ENV } } }; diff --git a/src/config/README.md b/src/config/README.md new file mode 100644 index 0000000..099c59b --- /dev/null +++ b/src/config/README.md @@ -0,0 +1,17 @@ +# About this folder +This folder holds configuration files for different environments. +You can use it to provide your app with different settings based on the +current environment, e.g. to configure different API base urls depending on +whether your setup runs in dev mode or is built for distribution. +You can include the configuration into your code like this: + +```javascript +let react = require('react/addons'); +let config = require('config'); +class MyComponent extends React.Component { + constructor(props) { + super(props); + let currentAppEnv = config.appEnv; + } +} +``` diff --git a/src/config/dev.js b/src/config/dev.js new file mode 100644 index 0000000..96009da --- /dev/null +++ b/src/config/dev.js @@ -0,0 +1,7 @@ +'use strict'; + +const config = { + appEnv: 'dev' // feel free to remove the appEnv property here +}; + +export default config; diff --git a/src/config/dist.js b/src/config/dist.js new file mode 100644 index 0000000..10661f6 --- /dev/null +++ b/src/config/dist.js @@ -0,0 +1,7 @@ +'use strict'; + +const config = { + appEnv: 'dist' // feel free to remove the appEnv property here +}; + +export default config; diff --git a/src/config/test.js b/src/config/test.js new file mode 100644 index 0000000..d76ca29 --- /dev/null +++ b/src/config/test.js @@ -0,0 +1,7 @@ +'use strict'; + +const config = { + appEnv: 'test' // don't remove the appEnv property here +}; + +export default config; diff --git a/test/config/ConfigTest.js b/test/config/ConfigTest.js new file mode 100644 index 0000000..7c9d35b --- /dev/null +++ b/test/config/ConfigTest.js @@ -0,0 +1,12 @@ +/*eslint-env node, mocha */ +/*global expect */ +/*eslint no-console: 0*/ +'use strict'; + +import config from 'config'; + +describe('appEnvConfigTests', () => { + it('should load app config file depending on current --env', () => { + expect(config.appEnv).to.equal('test'); + }); +}); diff --git a/webpack.config.js b/webpack.config.js index b7882b0..220f09e 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -15,6 +15,7 @@ if(args._.length > 0 && args._.indexOf('start') !== -1) { } else { env = 'dev'; } +process.env.REACT_WEBPACK_ENV = env; // Get available configurations var configs = {