From 99d578d0e8420b83a457abf2e4ae5ce4e9b76ac1 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Tue, 13 Sep 2016 18:31:40 +0530 Subject: [PATCH] Add support to get custom env variables. User can pass env. variables prefixed with STORYBOOK_ and it'll be passed to the client. If our env variable is STORYBOOK_SCORE=100, then you can get it via 'process.env.STORYBOOK_SCORE' from the client. --- dist/server/config/utils.js | 31 ++++++++++++++++++++++- dist/server/config/webpack.config.js | 2 +- dist/server/config/webpack.config.prod.js | 2 +- src/server/config/utils.js | 16 ++++++++++++ src/server/config/webpack.config.js | 3 ++- src/server/config/webpack.config.prod.js | 4 +-- 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/dist/server/config/utils.js b/dist/server/config/utils.js index d03f7bc571f1..46d9a9aba843 100644 --- a/dist/server/config/utils.js +++ b/dist/server/config/utils.js @@ -5,6 +5,16 @@ Object.defineProperty(exports, "__esModule", { }); exports.excludePaths = exports.includePaths = exports.OccurenceOrderPlugin = undefined; +var _keys = require('babel-runtime/core-js/object/keys'); + +var _keys2 = _interopRequireDefault(_keys); + +var _stringify = require('babel-runtime/core-js/json/stringify'); + +var _stringify2 = _interopRequireDefault(_stringify); + +exports.loadEnv = loadEnv; + var _webpack = require('webpack'); var _webpack2 = _interopRequireDefault(_webpack); @@ -23,4 +33,23 @@ _webpack2.default.optimize.OccurenceOrderPlugin; var includePaths = exports.includePaths = [_path2.default.resolve('./')]; -var excludePaths = exports.excludePaths = [_path2.default.resolve('./node_modules')]; \ No newline at end of file +var excludePaths = exports.excludePaths = [_path2.default.resolve('./node_modules')]; + +// Load environment variables starts with STORYBOOK_ to the client side. +function loadEnv() { + var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + var defaultNodeEnv = options.production ? 'production' : 'development'; + var env = { + 'process.env.NODE_ENV': (0, _stringify2.default)(process.env.NODE_ENV || defaultNodeEnv) + }; + + (0, _keys2.default)(process.env).filter(function (name) { + return (/^STORYBOOK_/.test(name) + ); + }).forEach(function (name) { + env['process.env.' + name] = (0, _stringify2.default)(process.env[name]); + }); + + return env; +} \ No newline at end of file diff --git a/dist/server/config/webpack.config.js b/dist/server/config/webpack.config.js index 2c8951854eaa..2fccc23589df 100644 --- a/dist/server/config/webpack.config.js +++ b/dist/server/config/webpack.config.js @@ -35,7 +35,7 @@ var config = { filename: 'static/[name].bundle.js', publicPath: '/' }, - plugins: [new _utils.OccurenceOrderPlugin(), new _webpack2.default.HotModuleReplacementPlugin(), new _caseSensitivePathsWebpackPlugin2.default()], + plugins: [new _webpack2.default.DefinePlugin((0, _utils.loadEnv)()), new _utils.OccurenceOrderPlugin(), new _webpack2.default.HotModuleReplacementPlugin(), new _caseSensitivePathsWebpackPlugin2.default()], module: { loaders: [{ test: /\.jsx?$/, diff --git a/dist/server/config/webpack.config.prod.js b/dist/server/config/webpack.config.prod.js index 42e3126ff5f9..fb26b847a630 100644 --- a/dist/server/config/webpack.config.prod.js +++ b/dist/server/config/webpack.config.prod.js @@ -38,7 +38,7 @@ var config = { // relative URLs works always. publicPath: '' }, - plugins: [new _webpack2.default.DefinePlugin({ 'process.env.NODE_ENV': '"production"' }), new _webpack2.default.optimize.DedupePlugin(), new _webpack2.default.optimize.UglifyJsPlugin({ + plugins: [new _webpack2.default.DefinePlugin((0, _utils.loadEnv)({ production: true })), new _webpack2.default.optimize.DedupePlugin(), new _webpack2.default.optimize.UglifyJsPlugin({ compress: { screw_ie8: true, warnings: false diff --git a/src/server/config/utils.js b/src/server/config/utils.js index e430b3ebefd2..77dee4b8785e 100644 --- a/src/server/config/utils.js +++ b/src/server/config/utils.js @@ -14,3 +14,19 @@ export const includePaths = [ export const excludePaths = [ path.resolve('./node_modules'), ]; + +// Load environment variables starts with STORYBOOK_ to the client side. +export function loadEnv(options = {}) { + const defaultNodeEnv = options.production ? 'production' : 'development'; + const env = { + 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || defaultNodeEnv), + }; + + Object.keys(process.env) + .filter((name) => /^STORYBOOK_/.test(name)) + .forEach((name) => { + env[`process.env.${name}`] = JSON.stringify(process.env[name]); + }); + + return env; +} diff --git a/src/server/config/webpack.config.js b/src/server/config/webpack.config.js index dea819c3d988..428fb456f3e5 100644 --- a/src/server/config/webpack.config.js +++ b/src/server/config/webpack.config.js @@ -1,7 +1,7 @@ import path from 'path'; import webpack from 'webpack'; import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin'; -import { OccurenceOrderPlugin, includePaths, excludePaths } from './utils'; +import { OccurenceOrderPlugin, includePaths, excludePaths, loadEnv } from './utils'; import babalLoaderConfig from './babel.js'; const config = { @@ -23,6 +23,7 @@ const config = { publicPath: '/', }, plugins: [ + new webpack.DefinePlugin(loadEnv()), new OccurenceOrderPlugin(), new webpack.HotModuleReplacementPlugin(), new CaseSensitivePathsPlugin(), diff --git a/src/server/config/webpack.config.prod.js b/src/server/config/webpack.config.prod.js index f153fd11922a..63982e1ddf4f 100644 --- a/src/server/config/webpack.config.prod.js +++ b/src/server/config/webpack.config.prod.js @@ -1,6 +1,6 @@ import path from 'path'; import webpack from 'webpack'; -import { OccurenceOrderPlugin, includePaths, excludePaths } from './utils'; +import { OccurenceOrderPlugin, includePaths, excludePaths, loadEnv } from './utils'; import babalLoaderConfig from './babel.prod.js'; const entries = { @@ -27,7 +27,7 @@ const config = { publicPath: '', }, plugins: [ - new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"production"' }), + new webpack.DefinePlugin(loadEnv({ production: true })), new webpack.optimize.DedupePlugin(), new webpack.optimize.UglifyJsPlugin({ compress: {