Skip to content

Instantly share code, notes, and snippets.

@citrus
Created June 8, 2021 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save citrus/967fe769a50f30d0940c848e8553c0ea to your computer and use it in GitHub Desktop.
Save citrus/967fe769a50f30d0940c848e8553c0ea to your computer and use it in GitHub Desktop.
Netlify plugin to contextualize vite env vars.
module.exports = {
onPreBuild: () => {
const BRANCH = process.env.BRANCH.toUpperCase().replace(/-/g, '_')
console.log('Vite env plugin starting for branch:', BRANCH)
Object.keys(process.env).sort().forEach(key => {
if (!key.startsWith('VITE_') || key.endsWith(`_${BRANCH}`)) {
return
}
const contextualValue = process.env[`${key}_${BRANCH}`]
if (contextualValue) {
console.log(`Reassigning ${key} to ${contextualValue}`)
process.env[key] = contextualValue
} else {
console.log('Keeping default value for', key)
}
})
process.env.VITE_BRANCH = BRANCH
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment