Created
June 8, 2021 18:55
-
-
Save citrus/967fe769a50f30d0940c848e8553c0ea to your computer and use it in GitHub Desktop.
Netlify plugin to contextualize vite env vars.
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
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