├── .gitignore ├── package.json ├── LICENSE ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | .vscode 4 | *.log 5 | *.rar 6 | *.zip 7 | .svn/* 8 | obj/* 9 | *.bak 10 | config.js 11 | **/env/production.js 12 | note.txt 13 | temp 14 | docs 15 | dist/server.js -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dotenv-toml-webpack", 3 | "version": "1.0.1", 4 | "description": "A secure webpack plugin that gives the ability to access environment variables via `process.env.*` defined in your `.env[.development|production].toml` files within your web applications built with webpack.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/nasa8x/dotenv-toml-webpack.git" 12 | }, 13 | "keywords": [ 14 | "webpack dotenv", 15 | "dotenv toml", 16 | "dotenv webpack", 17 | "toml webpack", 18 | "webpack toml" 19 | ], 20 | "author": "Morioh Team", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/nasa8x/dotenv-toml-webpack/issues" 24 | }, 25 | "homepage": "https://github.com/nasa8x/dotenv-toml-webpack#readme", 26 | "peerDependencies": { 27 | "webpack": ">=5.0.0" 28 | }, 29 | "dependencies": { 30 | "toml": "^3.0.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 [Morioh Team](https://morioh.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { DefinePlugin } = require('webpack'); 2 | const { readFileSync } = require('fs'); 3 | const { parse } = require('toml'); 4 | 5 | class EnvTomlPlugin { 6 | /** 7 | * The dotenv-toml-webpack plugin. 8 | * @param {Object} options - The parameters. 9 | * @param {String} [options.path=./.env.toml] - The location of the environment variable. 10 | * @param {Boolean} [options.systemvars=false] - If true, load system environment variables. 11 | * @param {Boolean} [options.silent=false] - If true, suppress warnings, if false, display warnings. 12 | * @param {Boolean|String} [options.safe=false] - If false ignore safe-mode, if true load `'./.env.example.toml'`, if a string load that file as the sample. 13 | * @returns {webpack.DefinePlugin} 14 | */ 15 | constructor(options = {}) { 16 | this.config = Object.assign({}, { 17 | path: './.env.toml', 18 | encoding: 'utf8' 19 | }, options) 20 | } 21 | 22 | apply(compiler) { 23 | 24 | const { path, encoding, systemvars, silent, safe } = this.config; 25 | const data = this.read(path, encoding); 26 | const variables = parse(data); 27 | 28 | if (systemvars) { 29 | if (silent) { 30 | Object.assign(variables, process.env); 31 | } 32 | else { 33 | Object.keys(process.env).forEach((key) => { 34 | if (variables.hasOwnProperty(key)) { 35 | console.warn('dotenv-toml-webpack: "%s" is overwritten by the system environment variable with the same name', key); 36 | } 37 | 38 | variables[key] = process.env[key]; 39 | }); 40 | } 41 | } 42 | 43 | if (safe) { 44 | const example = typeof safe === 'string' ? safe : './.env.example.toml'; 45 | const blueprint = parse(this.read(example, encoding)); 46 | Object.keys(blueprint).forEach(key => { 47 | const value = variables[key]; 48 | 49 | if (typeof value === 'undefined' || value === null || value === '') { 50 | throw new Error(`Missing environment variable: ${key}`) 51 | } 52 | }) 53 | } 54 | 55 | const definitions = {}; 56 | Object.keys(variables).forEach((key) => { 57 | definitions[`process.env.${key}`] = JSON.stringify(variables[key]); 58 | }); 59 | 60 | new DefinePlugin(definitions).apply(compiler); 61 | } 62 | 63 | read(file, encoding) { 64 | try { 65 | return readFileSync(file, encoding); 66 | } catch (err) { 67 | console.warn(`Failed to load ${file}.`); 68 | return ''; 69 | } 70 | } 71 | 72 | } 73 | 74 | 75 | module.exports = EnvTomlPlugin; 76 | module.exports.EnvTomlPlugin = EnvTomlPlugin; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dotenv-toml-webpack 2 | 3 | A secure webpack plugin that gives the ability to access environment variables via `process.env.*` defined in your `.env.toml`, `.env.development.toml`, `.env.production.toml`, etc,. files within your web applications built with webpack. 4 | 5 | 6 | ## Installation 7 | 8 | Using NPM: 9 | 10 | ```sh 11 | npm install dotenv-toml-webpack --save-dev 12 | ``` 13 | 14 | Using Yarn: 15 | 16 | ```sh 17 | $ yarn add dotenv-toml-webpack --dev 18 | ``` 19 | 20 | ## Description 21 | 22 | `dotenv-toml-webpack` wraps `toml` and `Webpack.DefinePlugin`. As such, it does a text replace in the resulting bundle for any instances of `process.env`. 23 | 24 | Your `.env` files can include sensitive information. Because of this,`dotenv-toml-webpack` will only expose environment variables that are **explicitly referenced in your code** to your final bundle. 25 | 26 | 27 | ## Usage example 28 | 29 | Let's suppose you have the following files in your project: 30 | 31 | ```sh 32 | # .env.toml 33 | 34 | API_URL = "http://localhost:8081" 35 | BASE_URL = "http://localhost:8080" 36 | 37 | [DB] 38 | HOST = "127.0.0.1" 39 | NAME = "mydb" 40 | PASS = "1qa2ws3ed4rf5tg6yh" 41 | PORT = 27017 42 | USER = "sa" 43 | 44 | ``` 45 | 46 | ```js 47 | // webpack.config.js 48 | 49 | const EnvTomlPlugin = require('dotenv-toml-webpack'); 50 | // or 51 | // const { EnvTomlPlugin } = require('dotenv-toml-webpack'); 52 | 53 | module.exports = { 54 | // ... 55 | plugins: [ 56 | new EnvTomlPlugin() 57 | ], 58 | // ... 59 | }; 60 | ``` 61 | ### Use in your code 62 | 63 | ```js 64 | // file1.js 65 | console.log(process.env.BASE_URL); 66 | // 'http://localhost:8080' 67 | 68 | console.log(process.env.DB.HOST); 69 | // '127.0.0.1' 70 | 71 | ``` 72 | ### Resulting bundle 73 | ```js 74 | // bundle.js 75 | console.log('http://localhost:8080'); 76 | console.log('127.0.0.1'); 77 | ``` 78 | 79 | Note: the `.env.*.toml` values for `BASE_URL` and `DB` are NOT present in our bundle, as they were never referenced (as process.env.[VAR_NAME]) in the code. 80 | 81 | ## How Secure? 82 | By allowing you to define exactly where you are loading environment variables from and bundling only variables in your project that are explicitly referenced in your code, you can be sure that only what you need is included and you do not accidentally leak anything sensitive. 83 | 84 | ### Recommended 85 | Add `.env.*` to your `.gitignore` file 86 | 87 | ## Env Webpack Mode 88 | 89 | ```sh 90 | # .env.development.toml 91 | 92 | API_URL = "http://localhost:8081" 93 | BASE_URL = "http://localhost:8080" 94 | 95 | [DB] 96 | HOST = "127.0.0.1" 97 | NAME = "mydb" 98 | PASS = "123456" 99 | PORT = 27017 100 | USER = "sa" 101 | 102 | ``` 103 | 104 | ```sh 105 | # .env.production.toml 106 | 107 | API_URL = "https://api.yourdomain.com" 108 | BASE_URL = "https://yourdomain.com" 109 | 110 | [DB] 111 | HOST = "127.0.0.1" 112 | NAME = "mydb" 113 | PASS = "123456" 114 | PORT = 27017 115 | USER = "sa" 116 | ``` 117 | 118 | ```js 119 | // webpack.config.js 120 | module.exports = (env, argv) => { 121 | 122 | console.log(argv, env); 123 | const prod = argv.mode === 'production'; 124 | 125 | return { 126 | mode: 'development', 127 | target: 'web', 128 | devtool: prod ? false : 'source-map', 129 | plugins: [ 130 | new EnvTomlPlugin({ 131 | path: `./.env.${argv.mode}.toml`, 132 | }), 133 | 134 | ], 135 | 136 | }; 137 | }; 138 | ``` 139 | ## Properties 140 | 141 | Use the following properties to configure your instance. 142 | 143 | * **path** (`'./.env.toml'`) - The path to your environment variables. 144 | * **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes). 145 | * **silent** (`false`) - If true, all warnings will be suppressed. 146 | * **safe** (`false`) - If true, load '.env.example.toml' to verify the '.env' variables are all set. Can also be a string to a different file. 147 | 148 | The following example shows how to set any/all arguments. 149 | 150 | ```javascript 151 | module.exports = { 152 | ... 153 | plugins: [ 154 | new Dotenv({ 155 | path: './.env.other.toml', // load this now instead of the ones in '.env' 156 | systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs. 157 | silent: true, // hide any errors 158 | safe: true, // load '.env.example.toml' to verify the '.env' variables are all set. Can also be a string to a different file. 159 | }) 160 | ] 161 | ... 162 | }; 163 | ``` 164 | 165 | 166 | Contributing 167 | ------------ 168 | 169 | Please refer to each project's style and contribution guidelines for submitting patches and additions. In general, we follow the "fork-and-pull" Git workflow. 170 | 171 | 1. **Fork** the repo on GitHub 172 | 2. **Clone** the project to your own machine 173 | 3. **Commit** changes to your own branch 174 | 4. **Push** your work back up to your fork 175 | 5. Submit a **Pull request** so that we can review your changes 176 | 177 | NOTE: Be sure to merge the latest from "upstream" before making a pull request! 178 | 179 | Community 180 | ------------ 181 | Stay up to date on the development of Morioh UI and reach out to the community with these helpful resources. 182 | 183 | Follow [@codek_tv](https://twitter.com/codek_tv) and [@im_a_developer](https://twitter.com/im_a_developer) on Twitter. 184 | 185 | Follow [Morioh](https://www.facebook.com/moriohdotcom) and [Vue Developers](https://www.facebook.com/VueDevelopers) on FaceBook. 186 | 187 | Join the official [Discord](https://discord.gg/sqxU6un) room: [https://discord.gg/sqxU6un](https://discord.gg/sqxU6un). 188 | 189 | 190 | ## License 191 | 192 | Licensed under [MIT](LICENSE) (c) 2021 [Morioh Team](https://morioh.com) --------------------------------------------------------------------------------