Last active
August 15, 2019 15:07
-
-
Save antony/aed7cfbceed89c9f6e55d3a2f72341bb to your computer and use it in GitHub Desktop.
Getting config from node-config into Sapper as `process.env.variableName`
This file contains 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
// config/default.js | |
module.exports = { | |
myConfigVariable: 'It Works!' | |
} |
This file contains 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
{ | |
... | |
"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", | |
... | |
} |
This file contains 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
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 | |
}), | |
... | |
}, | |
... | |
} |
This file contains 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
<h1> | |
{process.env.myConfigVariable} | |
</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment