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

Add support to get custom env variables. #450

Merged
merged 2 commits into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions dist/server/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ Object.defineProperty(exports, "__esModule", {
});
exports.nodeModulesPaths = 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);
Expand All @@ -25,4 +35,23 @@ var includePaths = exports.includePaths = [_path2.default.resolve('./')];

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;
}

var nodeModulesPaths = exports.nodeModulesPaths = _path2.default.resolve('./node_modules');
2 changes: 1 addition & 1 deletion dist/server/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var config = {
filename: 'static/[name].bundle.js',
publicPath: '/'
},
plugins: [new _utils.OccurenceOrderPlugin(), new _webpack2.default.HotModuleReplacementPlugin(), new _caseSensitivePathsWebpackPlugin2.default(), new _WatchMissingNodeModulesPlugin2.default(_utils.nodeModulesPaths)],
plugins: [new _webpack2.default.DefinePlugin((0, _utils.loadEnv)()), new _utils.OccurenceOrderPlugin(), new _webpack2.default.HotModuleReplacementPlugin(), new _caseSensitivePathsWebpackPlugin2.default(), new _WatchMissingNodeModulesPlugin2.default(_utils.nodeModulesPaths)],
module: {
loaders: [{
test: /\.jsx?$/,
Expand Down
2 changes: 1 addition & 1 deletion dist/server/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/server/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@ 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;
}

export const nodeModulesPaths = path.resolve('./node_modules');
2 changes: 2 additions & 0 deletions src/server/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
includePaths,
excludePaths,
nodeModulesPaths,
loadEnv,
} from './utils';
import babalLoaderConfig from './babel.js';

Expand All @@ -29,6 +30,7 @@ const config = {
publicPath: '/',
},
plugins: [
new webpack.DefinePlugin(loadEnv()),
new OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new CaseSensitivePathsPlugin(),
Expand Down
4 changes: 2 additions & 2 deletions src/server/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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: {
Expand Down