package/package.json000644 001750 001750 0000001334 12607452027013025 0ustar00000000 000000 { "name": "webpack-env", "version": "0.8.0", "description": "read variables from a .env.js file and make them available to webpack as globals", "main": "index.js", "scripts": { "test": "./node_modules/mocha/bin/mocha test" }, "repository": { "type": "git", "url": "git+https://github.com/toastynerd/webpack_env.git" }, "keywords": [ "weback", "env" ], "author": "Tyler Morgan ", "license": "MIT", "bugs": { "url": "https://github.com/toastynerd/webpack_env/issues" }, "homepage": "https://github.com/toastynerd/webpack_env#readme", "dependencies": { "webpack": "^1.12.2" }, "devDependencies": { "chai": "^3.3.0", "mocha": "^2.3.3" } } package/.npmignore000644 001750 001750 0000000026 12600522604012523 0ustar00000000 000000 node_modules **/*.sw? package/README.md000644 001750 001750 0000002335 12607450767012031 0ustar00000000 000000 Webpack Env ================ [![Build Status](https://travis-ci.org/toastynerd/webpack_env.svg)](https://travis-ci.org/toastynerd/webpack_env) Webpack ENV is a webpack plug-in for creating ENV-variable-like globals in webpack. ## Setup First add Webpack Env as a webpack plug-in, using gulp this would look something like this: ```js //gulpfile.js var gulp = require('gulp'); var webpack = require('webpack'); var webpackEnv = require('webpack-env'); gulp.task('webpack', function() { return gulp.src('./entry.js') .pipe(webpack({ output: { filename: 'bundle.js' }, plugins: [webpackEnv] })) .pipe(gulp.dest('build/')); }); ``` Create a `.env.js` file in the same directory as your gulpfile.js. This file should export an object that contains the eventual globals you want your webpack code to contain: ```js module.exports = { SOME_VAR: 'some val' } ``` ## Multiple Environments Weback Env also supports having multiple files for multiple environments. To create a set of Production environment globals, just create a `.production_env.js` file and run gulp with NODE_ENV set to 'production'. ## Keep your secrets out of the repo Be sure you've added all your `.env` files to your `.gitignore`. package/index.js000644 001750 001750 0000000074 12600531060012170 0ustar00000000 000000 module.exports = require(__dirname + '/lib/webpack_env')(); package/.travis.yml000644 001750 001750 0000000105 12600532207012633 0ustar00000000 000000 language: node_js node_js: - "0.10" - "0.12" - "4.0" - "4.1" package/lib/webpack_env.js000644 001750 001750 0000001002 12607452012014111 0ustar00000000 000000 var webpack = require('webpack'); var fs = require('fs'); module.exports = exports = function() { var dir = process.cwd(); var env = require(dir + '/\.' + (process.env.NODE_ENV? process.env.NODE_ENV + '_' : '') + 'env.js'); if (!(typeof env === 'object')) { console.log('WARNING: .env.js did not return an object!'); env = {}; } var keys = Object.keys(env); for (var i = 0; i < keys.length; i++) { env[keys[i]] = '\"' + env[keys[i]] + '\"'; } return new webpack.DefinePlugin(env); } package/test/.env.js000644 001750 001750 0000000053 12600537654012722 0ustar00000000 000000 module.exports = { TEST: 'test value' }; package/test/.production_env.js000644 001750 001750 0000000053 12600537625015166 0ustar00000000 000000 module.exports = { TEST: 'prod value' }; package/test/env_test.js000644 001750 001750 0000002075 12607451677013720 0ustar00000000 000000 var nodenvDirBackup = process.env.NODENV_DIR; process.env.NODENV_DIR = __dirname; process.cwd = function() { return __dirname; }; var expect = require('chai').expect; describe('webpack_env default', function() { before(function() { this.webpackEnv = require(__dirname + '/../index'); }); after(function() { process.env.NODENV_DIR = nodenvDirBackup; }); it('should be able to grab basic .env.json file', function() { expect(this.webpackEnv.definitions.TEST).to.eql('"test value"'); }); describe('not dev environment', function() { before(function() { this.cwdBackup = process.cwd; this.nodeEnvBackup = process.env.NODE_ENV; process.env.NODE_ENV = 'production'; delete require.cache[require.resolve(__dirname + '/../index')]; this.webpackEnv = require(__dirname + '/../index'); }); after(function() { process.env.NODE_ENV = this.nodeEnvBackup; }); it('should be able to read other env files', function() { expect(this.webpackEnv.definitions.TEST).to.eql('"prod value"'); }); }); });