├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Dependency directory 11 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 12 | node_modules 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Arik Maor 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 all 13 | 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 THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # extended-define-webpack-plugin 2 | Extended version of webpack.DefinePlugin 3 | 4 | ## Installation 5 | ``` 6 | npm install extended-define-webpack-plugin --save-dev 7 | ``` 8 | 9 | ## Example Usage 10 | We want to inject a configuration object and use it in our code. 11 | ```js 12 | // app.config.js 13 | module.exports = { 14 | api_key: '1234567890ABCDEFG' 15 | fb_conf: { 16 | use_social: true, 17 | api_key: '123456790' 18 | } 19 | }; 20 | ``` 21 | 22 | We use the `ExtendedDefinePlugin` in our webpack configuration like this: 23 | ```js 24 | // webpack.config.js 25 | 26 | var ExtendedDefinePlugin = require('extended-define-webpack-plugin'); 27 | var appConfig = require('./app.config.js'); 28 | 29 | module.exports = { 30 | // ... 31 | plugins: [ 32 | /* ..., */ 33 | new ExtendedDefinePlugin({ 34 | APP_CONFIG: appConfig, 35 | }) 36 | ] 37 | }; 38 | ``` 39 | 40 | In our client code we can just use it! 41 | ```js 42 | // main.js 43 | 44 | // ... 45 | 46 | var server_api_key = APP_CONFIG.api_key; 47 | 48 | // ... 49 | 50 | if (APP_CONFIG.fb_conf.use_social) { 51 | var fb_api_key = APP_CONFIG.fb_conf.api_key; 52 | } 53 | 54 | // ... 55 | ``` 56 | 57 | ## Output 58 | The example above will produce this output: 59 | ```js 60 | // main.js 61 | 62 | // ... 63 | 64 | var server_api_key = ('1234567890ABCDEFG'); 65 | 66 | // ... 67 | 68 | if ((true)) { 69 | var fb_api_key = ('123456790'); 70 | } 71 | 72 | // ... 73 | ``` 74 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License http://www.opensource.org/licenses/mit-license.php 3 | Author Arik Maor 4 | */ 5 | var _ = require('lodash'); 6 | var webpack = require('webpack'); 7 | 8 | module.exports = ExtendedDefinePlugin; 9 | 10 | function ExtendedDefinePlugin(definitions) { 11 | var clonedDefinitions = _.cloneDeep(definitions); 12 | return new webpack.DefinePlugin(deepJsonStringify(clonedDefinitions)); 13 | } 14 | 15 | function deepJsonStringify(definitions) { 16 | return _.each(definitions, function (val, key) { 17 | definitions[key] = _.isString(val) ? 18 | JSON.stringify(val) : 19 | deepJsonStringify(definitions[key]); 20 | }); 21 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "extended-define-webpack-plugin", 3 | "version": "0.1.3", 4 | "description": "Extended version of webpack.DefinePlugin", 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/ArikMaor/extended-define-webpack-plugin.git" 12 | }, 13 | "keywords": [ 14 | "webpack", 15 | "plugin", 16 | "webpack-plugin", 17 | "define-webpack-plugin", 18 | "json", 19 | "javascript" 20 | ], 21 | "author": "Arik Maor", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/ArikMaor/extended-define-webpack-plugin/issues" 25 | }, 26 | "homepage": "https://github.com/ArikMaor/extended-define-webpack-plugin#readme", 27 | "dependencies": { 28 | "lodash": "^4.15.0" 29 | }, 30 | "peerDependencies": { 31 | "webpack": "*" 32 | } 33 | } 34 | --------------------------------------------------------------------------------