nippur72 / ifdef-loader

Webpack loader for JavaScript/TypeScript conditional compilation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] treat undefined keys as false

lexey111 opened this issue · comments

Hi! Thank you very much for the loader, it is very useful.

It would be very nice to add one minor feature to it - some option in config to treat "absent" (or unset) keys as "false".

but if they are absent, how do you know their names in order to turn them into false?

Like this (dirty patch) -

preprocessor.ts

function evaluate(condition: string, defs: OptionObject): boolean {
   const code = `return (${condition}) ? true : false;`;
   const args = Object.keys(defs);

   let result: boolean;
   try {
     const f = new Function(...args, code);
     result = f(...args.map((k) => defs[k]));
      //console.log(`evaluation of (${condition}) === ${result}`);
   }
   catch(error) {
      // throw `error evaluation #if condition(${condition}): ${error}`;
      return false; // ...if option "treatAbsentAsFalse" is set and error is ReferenceError
   }

   return result;
}