Skip to content

Commit

Permalink
Allow for env files
Browse files Browse the repository at this point in the history
  • Loading branch information
okcoker committed Jul 10, 2017
1 parent 36e5fce commit eee5212
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"css-loader": "^0.26.1",
"debug": "^2.6.0",
"detect-port": "^1.2.1",
"dotenv": "^4.0.0",
"eventemitter2": "^4.1.0",
"express": "^4.14.0",
"express-graphql": "^0.6.6",
Expand Down
60 changes: 30 additions & 30 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { uniq, some } from "lodash"
import fs from "fs"
import path from "path"
import webpack from "webpack"
import dotenv from "dotenv"
import Config from "webpack-configurator"
import ExtractTextPlugin from "extract-text-webpack-plugin"
import StaticSiteGeneratorPlugin from "static-site-generator-webpack-plugin"
Expand Down Expand Up @@ -41,6 +42,30 @@ module.exports = async (
const stage = suppliedStage
const babelConfig = await genBabelConfig(program, babelStage)

function processEnv(stage, defaultNodeEnv) {
debug(`Building env for "${stage}"`)
const env = process.env.NODE_ENV ? process.env.NODE_ENV : `${defaultNodeEnv}`
const envFile = path.join(process.cwd(), `./.env.${env}`)
let parsed = {}
try {
parsed = dotenv.parse(fs.readFileSync(envFile, { encoding: `utf8` }))
} catch(e) {
if (e && e.code !== `ENOENT`) {
console.log(e)
}
}
const envObject = Object.keys(parsed).reduce((acc, key) => {
acc[key] = JSON.stringify(parsed[key])
return acc
}, {})

// Don't allow overwriting of NODE_ENV, PUBLIC_DIR as to not break gatsby things
envObject.NODE_ENV = JSON.stringify(env)
envObject.PUBLIC_DIR = JSON.stringify(`${process.cwd()}/public`)

return envObject
}

debug(`Loading webpack config for stage "${stage}"`)
function output() {
switch (stage) {
Expand Down Expand Up @@ -127,12 +152,7 @@ module.exports = async (
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(
process.env.NODE_ENV ? process.env.NODE_ENV : `development`
),
PUBLIC_DIR: JSON.stringify(`${process.cwd()}/public`),
},
"process.env": processEnv(stage, `development`),
__PREFIX_PATHS__: program.prefixPaths,
__PATH_PREFIX__: JSON.stringify(store.getState().config.pathPrefix),
}),
Expand All @@ -154,12 +174,7 @@ module.exports = async (
return [
new StaticSiteGeneratorPlugin(`render-page.js`, pages),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(
process.env.NODE_ENV ? process.env.NODE_ENV : `development`
),
PUBLIC_DIR: JSON.stringify(`${process.cwd()}/public`),
},
"process.env": processEnv(stage, `development`),
__PREFIX_PATHS__: program.prefixPaths,
__PATH_PREFIX__: JSON.stringify(store.getState().config.pathPrefix),
}),
Expand All @@ -168,12 +183,7 @@ module.exports = async (
case `build-css`:
return [
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(
process.env.NODE_ENV ? process.env.NODE_ENV : `production`
),
PUBLIC_DIR: JSON.stringify(`${process.cwd()}/public`),
},
"process.env": processEnv(stage, `production`),
__PREFIX_PATHS__: program.prefixPaths,
__PATH_PREFIX__: JSON.stringify(store.getState().config.pathPrefix),
}),
Expand All @@ -183,12 +193,7 @@ module.exports = async (
return [
new StaticSiteGeneratorPlugin(`render-page.js`, pages),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(
process.env.NODE_ENV ? process.env.NODE_ENV : `production`
),
PUBLIC_DIR: JSON.stringify(`${process.cwd()}/public`),
},
"process.env": processEnv(stage, `production`),
__PREFIX_PATHS__: program.prefixPaths,
__PATH_PREFIX__: JSON.stringify(store.getState().config.pathPrefix),
}),
Expand Down Expand Up @@ -261,12 +266,7 @@ module.exports = async (
// optimizations for React) and whether prefixing links is enabled
// (__PREFIX_PATHS__) and what the link prefix is (__PATH_PREFIX__).
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(
process.env.NODE_ENV ? process.env.NODE_ENV : `production`
),
PUBLIC_DIR: JSON.stringify(`${process.cwd()}/public`),
},
"process.env": processEnv(stage, `production`),
__PREFIX_PATHS__: program.prefixPaths,
__PATH_PREFIX__: JSON.stringify(store.getState().config.pathPrefix),
}),
Expand Down

0 comments on commit eee5212

Please sign in to comment.