Created
February 4, 2021 22:21
-
-
Save AWolf81/057edca9b861b0a438fc75998d7dae27 to your computer and use it in GitHub Desktop.
Snowpack plugin-dotenv with path option
This file contains hidden or 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
const fs = require('fs'); | |
const path = require('path'); | |
module.exports = function plugin(_, {path:envPath}) { | |
const NODE_ENV = process.env.NODE_ENV; | |
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use | |
const dotenvFiles = [ | |
NODE_ENV && `.env.${NODE_ENV}.local`, | |
// Don't include `.env.local` for `test` environment | |
// since normally you expect tests to produce the same | |
// results for everyone | |
NODE_ENV !== 'test' && `.env.local`, | |
NODE_ENV && `.env.${NODE_ENV}`, | |
'.env', | |
].filter(Boolean); | |
// Load environment variables from .env* files. Suppress warnings using silent | |
// if this file is missing. dotenv will never modify any environment variables | |
// that have already been set. Variable expansion is supported in .env files. | |
// https://github.com/motdotla/dotenv | |
// https://github.com/motdotla/dotenv-expand | |
dotenvFiles.forEach((dotenvFile) => { | |
const envFile = envPath ? path.join(envPath, dotenvFile) : dotenvFile | |
if (fs.existsSync(envFile)) { | |
require('dotenv-expand')( | |
require('dotenv').config({ | |
path: envFile, | |
}), | |
); | |
} | |
}); | |
return { | |
name: 'plugin-dotenv', | |
}; | |
}; |
This file contains hidden or 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
/** @type {import("snowpack").SnowpackUserConfig } */ | |
module.exports = { | |
mount: { | |
// directory name: 'build directory' | |
public: '/', | |
src: '/dist', | |
}, | |
plugins: [ | |
["./plugins/plugin-dotenv", {path: "../"}] | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment