Skip to content

Instantly share code, notes, and snippets.

@antony
Last active August 15, 2019 15:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antony/aed7cfbceed89c9f6e55d3a2f72341bb to your computer and use it in GitHub Desktop.
Save antony/aed7cfbceed89c9f6e55d3a2f72341bb to your computer and use it in GitHub Desktop.
Getting config from node-config into Sapper as `process.env.variableName`
// config/default.js
module.exports = {
myConfigVariable: 'It Works!'
}
{
...
"scripts": {
"dev": "NODE_CONFIG_ENV=${NODE_ENV} PORT=1234 sapper dev",
"build": "NODE_CONFIG_ENV=${NODE_ENV} sapper build --legacy",
...
},
"devDependencies": {
"config": "^3.1.0",
"rollup-plugin-replace": "^2.0.0",
...
}
import replace from 'rollup-plugin-replace'
...
import conf from 'config'
...
const appConfig = Object.keys(conf).reduce((acc, n) => {
acc[`process.env.${n}`] = JSON.stringify(conf[n])
return acc
}, {})
export default {
client: {
...
plugins: [
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode),
// Don't do this if your config contains senstive secrets - filter it.
...appConfig
}),
...
},
server: {
...
plugins: [
replace({
'process.browser': false,
'process.env.NODE_ENV': JSON.stringify(mode),
...appConfig
}),
...
},
...
}
<h1>
{process.env.myConfigVariable}
</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment