Created
September 25, 2017 13:28
-
-
Save mpontus/6f5fe46711e190213d8fcb395b98dc6c to your computer and use it in GitHub Desktop.
Storybook + CRA dotenv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// .storybook/webpack.config.js | |
const path = require('path'); | |
const fs = require('fs'); | |
const webpack = require('webpack'); | |
const dotenv = path.resolve(__dirname, '../.env'); | |
const NODE_ENV = process.env.NODE_ENV || 'development'; | |
const dotenvFiles = [ | |
`${dotenv}.${NODE_ENV}.local`, | |
`${dotenv}.${NODE_ENV}`, | |
// Don't include `.env.local` for `test` environment | |
// since normally you expect tests to produce the same | |
// results for everyone | |
NODE_ENV !== 'test' && `${dotenv}.local`, | |
dotenv, | |
].filter(Boolean); | |
dotenvFiles.forEach(dotenvFile => { | |
if (fs.existsSync(dotenvFile)) { | |
require('dotenv').config({ | |
path: dotenvFile, | |
}); | |
} | |
}); | |
const REACT_APP = /^REACT_APP_/i; | |
const clientEnv = Object.keys(process.env) | |
.filter(key => REACT_APP.test(key)) | |
.reduce( | |
(env, key) => { | |
env[key] = process.env[key]; | |
return env; | |
}, | |
{ NODE_ENV }, | |
); | |
const stringifyEnv = raw => | |
Object.keys(raw).reduce((env, key) => { | |
env[key] = JSON.stringify(raw[key]); | |
return env; | |
}, {}); | |
module.exports = { | |
plugins: [ | |
new webpack.DefinePlugin({ | |
'process.env': stringifyEnv(clientEnv), | |
}), | |
], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment