Skip to content

Commit

Permalink
[webpack-integration] Expose getSanityEnvVars()
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Dec 6, 2019
1 parent b2dea43 commit 8e71dad
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/@sanity/webpack-integration/src/v3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ function getPartResolverPlugin(options) {
return new PartResolverPlugin(options)
}

function tryReadDotEnv(studioRootPath) {
const configEnv = process.env.NODE_ENV || 'development'
function tryReadDotEnv(studioRootPath, configEnv) {
const envFile = path.join(studioRootPath, `.env.${configEnv}`)

let parsed = {}
Expand All @@ -31,15 +30,24 @@ function tryReadDotEnv(studioRootPath) {
return parsed
}

function getEnvVars({isProd, basePath}) {
const dotEnvVars = tryReadDotEnv(basePath)
function getSanityEnvVars({env, basePath}) {
const configEnv = process.env.BUNDLE_ENV || env || process.env.NODE_ENV || 'development'
const dotEnvVars = tryReadDotEnv(basePath, configEnv)
const allEnvVars = {...dotEnvVars, ...process.env}
return Object.keys(allEnvVars).reduce((acc, key) => {
if (key.startsWith('SANITY_STUDIO_')) {
acc[key] = allEnvVars[key]
}
return acc
}, {})
}

function getEnvVars({isProd, env, basePath}) {
const envVars = getSanityEnvVars({env, basePath})

return Object.keys(allEnvVars).reduce(
return Object.keys(envVars).reduce(
(acc, key) => {
if (key.startsWith('SANITY_STUDIO_')) {
acc[`process.env.${key}`] = JSON.stringify(process.env[key])
}
acc[`process.env.${key}`] = JSON.stringify(envVars[key])
return acc
},
{
Expand All @@ -56,7 +64,7 @@ function getEnvPlugin(options) {
const webpack = options.webpack || require('webpack')
return new webpack.DefinePlugin({
__DEV__: !isProd && bundleEnv === 'development',
...getEnvVars({isProd, basePath: options.basePath})
...getEnvVars({isProd, env, basePath: options.basePath})
})
}

Expand Down Expand Up @@ -107,6 +115,7 @@ module.exports = {
getEnvPlugin: getEnvPlugin,
getPartLoader: getPartLoader,
getStyleResolver: getStyleResolver,
getSanityEnvVars: getSanityEnvVars,
getPartResolverPlugin: getPartResolverPlugin,
getPostcssImportPlugin: getPostcssImportPlugin,
getPostcssPlugins: getPostcssPlugins,
Expand Down

0 comments on commit 8e71dad

Please sign in to comment.