├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitignore ├── README.md ├── example ├── index.js └── webpack.config.js ├── index.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | example/build 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "airbnb/legacy" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | example/build 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | inline-environment-variables-webpack-plugin 2 | =========================================== 3 | 4 | Plugin which replaces all instances of `process.env.***` with the environment variable value. 5 | 6 | Install 7 | ------- 8 | 9 | ``` 10 | npm install --save-dev inline-environment-variables-webpack-plugin 11 | ``` 12 | 13 | Usage 14 | ----- 15 | 16 | ### Inline all environment variables 17 | 18 | If you would like to inline **all** environment variables then you can just instantiate the plugin without passing any config, as in the following example. 19 | 20 | 21 | ```javascript 22 | var InlineEnvironmentVariablesPlugin = require('inline-environment-variables-webpack-plugin'); 23 | 24 | var webpackConfig = { 25 | plugins: [ 26 | new InlineEnvironmentVariablesPlugin() 27 | ], 28 | // other webpack config ... 29 | }; 30 | ``` 31 | 32 | ### Inline only selected environment variables 33 | 34 | You can optionally pass a config to select which environment variables you would like to inline. 35 | 36 | A config can be either a string an object or an array. 37 | 38 | ###### string config 39 | A string config is defined like 40 | ```js 41 | new InlineEnvironmentVariablesPlugin('NODE_ENV') 42 | ``` 43 | The above config will inline `process.env.NODE_ENV` with the value of `process.env.NODE_ENV` at build time 44 | 45 | ###### object config 46 | an object config is defined like 47 | ```js 48 | new InlineEnvironmentVariablesPlugin({ NODE_ENV: 'production' }); 49 | ``` 50 | 51 | The above config will inline `process.env.NODE_ENV` with the value specified in the config. So in the above example it would change it to `'production'`; 52 | 53 | ###### array config 54 | 55 | and array config is just a list of string and object configs. It would be defined like 56 | 57 | ```js 58 | new InlineEnvironmentVariablesPlugin([ 59 | 'SOME_VAR', 60 | 'ANOTHER_ONE', 61 | { 62 | NODE_ENV: 'production', 63 | ONE_MORE: true 64 | } 65 | ]); 66 | ``` 67 | 68 | ### Disable warnings 69 | 70 | Pass in an object with `warnings` set to `false`. 71 | 72 | `new InlineEnvironmentVariablesPlugin({ NODE_ENV: 'production' }, { warnings: false })` 73 | -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | var a = process.env.NODE_ENV; 2 | var b = process.env.SHELL; 3 | 4 | console.log(a); 5 | console.log(b); 6 | -------------------------------------------------------------------------------- /example/webpack.config.js: -------------------------------------------------------------------------------- 1 | // var InlineEnvironmentVariablesPlugin = require('inline-environment-variables-webpack-plugin'); 2 | var InlineEnvironmentVariablesPlugin = require('..'); 3 | 4 | module.exports = { 5 | entry: './index.js', 6 | output: { 7 | path: './build', 8 | filename: 'index.js', 9 | }, 10 | plugins: [ 11 | new InlineEnvironmentVariablesPlugin(), 12 | ], 13 | }; 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack'); 2 | var compose = require('fn-compose'); 3 | 4 | // make sure replacements is an array 5 | function ensureReplacementsIsArray(replacements) { 6 | if (replacements.constructor !== Array) { 7 | return [replacements]; 8 | } 9 | 10 | return replacements; 11 | } 12 | 13 | // convert string configs of type ['NODE_ENV'] 14 | // to an object of [{NODE_ENV: process.env.NODE_ENV}] 15 | function convertAllReplacementsToObject(replacements) { 16 | return replacements.map(function (replacement) { 17 | var config = {}; 18 | if (typeof replacement === 'string') { 19 | config[replacement] = process.env[replacement]; 20 | return config; 21 | } 22 | return replacement; 23 | }); 24 | } 25 | 26 | // now that all configs are objects merge em into a single object 27 | function mergeReplacements(replacements) { 28 | return Object.assign.apply(null, replacements); 29 | } 30 | 31 | // compose all above actions into a single function 32 | function normalizeReplacements(replacements) { 33 | var finalReplacements = replacements; 34 | if (typeof replacements === 'undefined') { 35 | finalReplacements = [process.env]; 36 | } 37 | 38 | return compose( 39 | mergeReplacements, 40 | convertAllReplacementsToObject, 41 | ensureReplacementsIsArray 42 | )(finalReplacements); 43 | } 44 | 45 | // warn if you are trying to inline a env var that is not defined 46 | function warnOnMissingEnvVars(config, variable) { 47 | if (typeof config[variable] === 'undefined') { 48 | console.warn('You tried to inline "' + variable + '", but it is not defined.'); 49 | } 50 | } 51 | 52 | function InlineEnvironmentVariablesPlugin(replacements, userOptions) { 53 | var defaultOptions = { warnings: true }; 54 | var normalizedConfig = normalizeReplacements(replacements); 55 | var options = Object.assign(defaultOptions, userOptions); 56 | 57 | var finalConfig = Object.keys(normalizedConfig).reduce(function (config, variable) { 58 | if (options.warnings) warnOnMissingEnvVars(normalizedConfig, variable); 59 | config['process.env.' + variable] = JSON.stringify(normalizedConfig[variable]); 60 | return config; 61 | }, {}); 62 | 63 | return new webpack.DefinePlugin(finalConfig); 64 | } 65 | 66 | module.exports = InlineEnvironmentVariablesPlugin; 67 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inline-environment-variables-webpack-plugin", 3 | "version": "1.2.1", 4 | "description": "webpack plugin to inline all environment variables", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "npm run lint", 8 | "lint": "eslint ." 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/tikotzky/inline-environment-variables-webpack-plugin.git" 13 | }, 14 | "keywords": [ 15 | "webpack", 16 | "plugin", 17 | "env", 18 | "variables" 19 | ], 20 | "author": "Mordy Tikotzky ", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/tikotzky/inline-environment-variables-webpack-plugin/issues" 24 | }, 25 | "homepage": "https://github.com/tikotzky/inline-environment-variables-webpack-plugin", 26 | "devDependencies": { 27 | "eslint": "^1.10.3", 28 | "eslint-config-airbnb": "^2.1.1", 29 | "webpack": "^1.12.9" 30 | }, 31 | "dependencies": { 32 | "fn-compose": "^1.1.0" 33 | } 34 | } 35 | --------------------------------------------------------------------------------