UNPKG

webpack-config-utils

Version:

Utilities to help your webpack config be easier to read

77 lines (65 loc) 3.98 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getIfUtils = undefined; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _propIf = require('./prop-if'); function _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; } exports.getIfUtils = getIfUtils; /** * The object returned from getIfUtils * @typedef {Object} IfUtils * @property {function} ifProduction - a wrapper around `propIf` function that returns the given `value` if the * environment is `"production"` or the `alternate` if not * @property {function} ifNotProduction, - a wrapper around `propIf` function that returns the given `value` if the * environment is not `"production"` or the `alternate` if it is * @property {function} ifProd - a wrapper around `propIf` function that returns the given `value` if the environment is * `"prod"` or the `alternate` if not * @property {function} ifNotProd, - a wrapper around `propIf` function that returns the given `value` if the * environment is not `"prod"` or the `alternate` if it is * @property {function} ifTest - a wrapper around `propIf` function that returns the given `value` if the environment is * `"test"` or the `alternate` if not * @property {function} ifNotTest, - a wrapper around `propIf` function that returns the given `value` if the * environment is not `"test"` or the `alternate` if it is * @property {function} ifDevelopment - a wrapper around `propIf` function that returns the given `value` if the * environment is `"development"` or the `alternate` if not * @property {function} ifNotDevelopment, - a wrapper around `propIf` function that returns the given `value` if the * environment is not `"development"` or the `alternate` if it is * @property {function} ifDev - a wrapper around `propIf` function that returns the given `value` if the environment is * `"dev"` or the `alternate` if not * @property {function} ifNotDev, - a wrapper around `propIf` function that returns the given `value` if the * environment is not `"dev"` or the `alternate` if it is */ /** * This returns an object with methods to help you conditionally set values to your webpack configuration object. * @param {Object|string} env This would be either the `env` object from webpack (function config API) or the value of * `process.env.NODE_ENV`. * @param {Array} vars A list of valid environments if utils are generated for * @return {IfUtils} the IfUtils object for the given environment */ function getIfUtils(env) { var vars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['production', 'prod', 'test', 'development', 'dev']; env = typeof env === 'string' ? _defineProperty({}, env, true) : env; if ((typeof env === 'undefined' ? 'undefined' : _typeof(env)) !== 'object') { throw new Error('webpack-config-utils:getIfUtils: env passed should be a string/Object. Was ' + JSON.stringify(env)); } return vars.reduce(function (utils, variable) { var envValue = !!env[variable]; var capitalVariable = capitalizeWord(variable); utils['if' + capitalVariable] = function (value, alternate) { return isUndefined(value) ? envValue : (0, _propIf.propIf)(envValue, value, alternate); }; utils['ifNot' + capitalVariable] = function (value, alternate) { return isUndefined(value) ? !envValue : (0, _propIf.propIfNot)(envValue, value, alternate); }; return utils; }, {}); } // utilities function capitalizeWord(word) { return word.substring(0, 1).toUpperCase() + word.substring(1); } function isUndefined(val) { return typeof val === 'undefined'; }