UNPKG

3.95 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getIfUtils = undefined;
7
8var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
9
10var _propIf = require('./prop-if');
11
12function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
14exports.getIfUtils = getIfUtils;
15
16/**
17 * The object returned from getIfUtils
18 * @typedef {Object} IfUtils
19 * @property {function} ifProduction - a wrapper around `propIf` function that returns the given `value` if the
20 * environment is `"production"` or the `alternate` if not
21 * @property {function} ifNotProduction, - a wrapper around `propIf` function that returns the given `value` if the
22 * environment is not `"production"` or the `alternate` if it is
23 * @property {function} ifProd - a wrapper around `propIf` function that returns the given `value` if the environment is
24 * `"prod"` or the `alternate` if not
25 * @property {function} ifNotProd, - a wrapper around `propIf` function that returns the given `value` if the
26 * environment is not `"prod"` or the `alternate` if it is
27 * @property {function} ifTest - a wrapper around `propIf` function that returns the given `value` if the environment is
28 * `"test"` or the `alternate` if not
29 * @property {function} ifNotTest, - a wrapper around `propIf` function that returns the given `value` if the
30 * environment is not `"test"` or the `alternate` if it is
31 * @property {function} ifDevelopment - a wrapper around `propIf` function that returns the given `value` if the
32 * environment is `"development"` or the `alternate` if not
33 * @property {function} ifNotDevelopment, - a wrapper around `propIf` function that returns the given `value` if the
34 * environment is not `"development"` or the `alternate` if it is
35 * @property {function} ifDev - a wrapper around `propIf` function that returns the given `value` if the environment is
36 * `"dev"` or the `alternate` if not
37 * @property {function} ifNotDev, - a wrapper around `propIf` function that returns the given `value` if the
38 * environment is not `"dev"` or the `alternate` if it is
39 */
40
41/**
42 * This returns an object with methods to help you conditionally set values to your webpack configuration object.
43 * @param {Object|string} env This would be either the `env` object from webpack (function config API) or the value of
44 * `process.env.NODE_ENV`.
45 * @param {Array} vars A list of valid environments if utils are generated for
46 * @return {IfUtils} the IfUtils object for the given environment
47 */
48
49function getIfUtils(env) {
50 var vars = arguments.length <= 1 || arguments[1] === undefined ? ['production', 'prod', 'test', 'development', 'dev'] : arguments[1];
51
52 env = typeof env === 'string' ? _defineProperty({}, env, true) : env;
53 if ((typeof env === 'undefined' ? 'undefined' : _typeof(env)) !== 'object') {
54 throw new Error('webpack-config-utils:getIfUtils: env passed should be a string/Object. Was ' + JSON.stringify(env));
55 }
56 return vars.reduce(function (utils, variable) {
57 var envValue = !!env[variable];
58 var capitalVariable = capitalizeWord(variable);
59 utils['if' + capitalVariable] = function (value, alternate) {
60 return isUndefined(value) ? envValue : (0, _propIf.propIf)(envValue, value, alternate);
61 };
62 utils['ifNot' + capitalVariable] = function (value, alternate) {
63 return isUndefined(value) ? !envValue : (0, _propIf.propIfNot)(envValue, value, alternate);
64 };
65 return utils;
66 }, {});
67}
68
69// utilities
70
71function capitalizeWord(word) {
72 return word.substring(0, 1).toUpperCase() + word.substring(1);
73}
74
75function isUndefined(val) {
76 return typeof val === 'undefined';
77}
\No newline at end of file