small preprocessor - #ifdef like statement ?

Posted on
  • Hi Gordon,

    It would be very nice if a tiny preprocessor with #ifdef / #endif statements where included in WebIDE. I would like to strip out debug code that way.

    Regards

    Sacha

  • Hmm, it's a thought. I wonder whether minification could be modified to do what you want though. For instance:

    var DBG = false;
    
    if (DBG) console.log("Debug");
    console.log("Foo");
    

    That actually works fine when minified - it goes down to:

    var DBG=!1;console.log("Foo");
    

    However when in a function, it doesn't work properly (because it thinks that the state of DBG might get changed). Object.freeze doesn't seem to work either. You could do:

    (function() {
      var DBG = false;
    
      function foo() {
        if (DBG) console.log("Debug");
        console.log("Foo");
      }
    
      setTimeout(foo,1000);
    })();
    

    And that minifies down really nicely:

    (function(){setTimeout(function(){console.log("Foo")},1E3)})();
    
  • Hello Gordon,

    Adding such a feature to the minifcation would be great.

    Regards

    Sacha

  • There are several new syntax proposals for ES7 that would like to make use of "#", as it's one of the last ascii characters available. I strongly recommend avoiding the introduction of non-standard syntactic forms.

  • @rwaldron: any thoughts on telling the minifier that DBG is a constant that doesn't change? It sounded like DBG=Object.freeze(DBG) might do it, but it didn't seem to work in the closure compiler.

  • Right, that won't work—Object.freeze only sets the meta object descriptor properties to: [[configurable]]: false, [[writable]]: false. It won't turn a non-object into a const. What you could do is provide a "built-in" keyword whose value is true and is essentially a const binding. Special keywords are safe additions, as long as they're not any of the FutureReservedWords: class, enum, extends, super, const, export, import, implements, let, private, public, interface, package, protected, static or yield; additionally, there is precedent in node with bindings like process, which aren't const, but are special (if you attempt to reassign it, the process itself will crash)

  • I'm not talking about extending Espruino at all here... I can want a way to tell someone else's minifier that a value isn't going to change.

    I guess I could pre-parse the code, detect the pattern var DBG = true/false; at the beginning, and then search/replace DBG with the actual value before passing the code to the minifier. It's pretty ugly though.

  • I see, I misunderstood.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

small preprocessor - #ifdef like statement ?

Posted by Avatar for Sacha @Sacha

Actions