diff --git a/src/get-if-utils.js b/src/get-if-utils.js index 71ce88d..4c9a83c 100644 --- a/src/get-if-utils.js +++ b/src/get-if-utils.js @@ -31,16 +31,16 @@ export {getIfUtils} * 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) { +function getIfUtils(env, vars = ['production', 'prod', 'test', 'development', 'dev']) { env = typeof env === 'string' ? {[env]: true} : env if (typeof env !== 'object') { throw new Error( `webpack-config-utils:getIfUtils: env passed should be a string/Object. Was ${JSON.stringify(env)}` ) } - const vars = ['production', 'prod', 'test', 'development', 'dev'] return vars.reduce((utils, variable) => { const envValue = !!env[variable] const capitalVariable = capitalizeWord(variable) diff --git a/src/get-if-utils.test.js b/src/get-if-utils.test.js index 4725409..ba8664d 100644 --- a/src/get-if-utils.test.js +++ b/src/get-if-utils.test.js @@ -15,6 +15,16 @@ test('has if and ifNot methods for the expected environment values', t => { const diff = difference(methods, expectedMethods) t.deepEqual(diff, []) }) +test('has ifXXX and ifNotXXX methods for the expected environment values', t => { + const expectedMethods = [ + 'ifFoo', 'ifNotFoo', + 'ifBar', 'ifNotBar', + ] + const utils = getIfUtils({}, ['foo', 'bar']) + const methods = Object.keys(utils) + const diff = difference(methods, expectedMethods) + t.deepEqual(diff, []) +}) test('works with string values', t => { const {ifProduction} = getIfUtils('production') @@ -36,3 +46,11 @@ test('returns true/false when given no arguments', t => { t.is(ifProd(), false) t.is(ifNotDev(), true) }) + +test('returns true/false for custom ifXXX when given no arguments', t => { + const {ifWatch, ifProd, ifNotDev, ifTest} = getIfUtils('watch', ['prod', 'dev', 'watch']) + t.is(ifWatch(), true) + t.is(ifProd(), false) + t.is(ifNotDev(), true) + t.is(ifTest, undefined) +})