Skip to content

Instantly share code, notes, and snippets.

@AWolf81
Created February 4, 2021 22:21
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 AWolf81/057edca9b861b0a438fc75998d7dae27 to your computer and use it in GitHub Desktop.
Save AWolf81/057edca9b861b0a438fc75998d7dae27 to your computer and use it in GitHub Desktop.
Snowpack plugin-dotenv with path option
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',
};
};
/** @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