Giter Club home page Giter Club logo

Comments (3)

nippur72 avatar nippur72 commented on July 21, 2024

I would say it's desidered behaviour: what comes after the if is evaluated as a plain JavaScript expression, so throwing ReferenceError is expected to happen if STATS is not defined at all (at least in strict mode).

I don't think that can be fixed easily since the loader doesn't have its own expression parser, it just uses something like eval() to evaluate expressions.

And you can't just trap the error and resolve to false because it wouldn't work if you write #if !STATS.

Possible workarounds:

  • initilize with STATS: undefined
  • check with #if typeof STATS === 'undefined'

from ifdef-loader.

TonoFilth avatar TonoFilth commented on July 21, 2024

Just for clarification, what I was trying to achieve is to have two different configurations, one for development and other for production:

dev: {
  DEVELOPMENT: true,
  STATS: true,
},
prod: {
  PRODUCTION: true,
}

Now I'm forced to duplicate the defines:

dev: {
  DEVELOPMENT: true,
  PRODUCTION: false,
  STATS: true,
},
prod: {
  DEVELOPMENT: false,
  PRODUCTION: true,
  STATS: false
}

I don't like this solution, so I will go with your workaround #if typeof STATS === 'undefined'.

Thanks a lot for the quick response!

from ifdef-loader.

TonoFilth avatar TonoFilth commented on July 21, 2024

I've modified the beginning of the evaluate() function in preprocessor.js and added the following code:

function evaluate(condition, keyword, defs) {
  condition = condition.replace(/defined\( *([a-z|A-Z|0-9|_]+) *\)/g, (match, p1) => {
    return defs[p1] === true;
  })
  ...
}

This allow me to write options in this way:

dev: {
  DEVELOPMENT: true,
  STATS: true,
},
prod: {
  PRODUCTION: true,
}

and use the preprocessor defines in this way:

/// #if defined(STATS)
console.log('stats defined');
/// #endif

If STATS is not defined there is no error thrown. I don't like to modify library sources directly but I prefer to work in this way.

Thanks for your help.

from ifdef-loader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.