Comments (3)
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.
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.
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)
- babel-loader complains about syntax errors in code that should be removed HOT 1
- Improve documentation to include onlyCompileBundledFiles option
- Webpack & Angular 7 HOT 7
- [Feature Request] treat undefined keys as false HOT 2
- Regular expression not working well in some cases HOT 6
- Define environment variables by default HOT 1
- Deprecation warning when used with webpack 5.0.0
- /////// leftovers in HTML code when mixed with JSX HOT 8
- [Feature Request] Provide a way to write optional code hide in comment HOT 3
- Getting wrong line numbers: "Including lines [NaN-NaN]" HOT 2
- ifdef-loader not reflecting in main.js
- #If #endif blocks highlighting in vscode
- ESLint bad indentation HOT 2
- not working with CRLF line ending file HOT 1
- Give a option to decide whether the loader is cacheable HOT 2
- Add #define HOT 1
- ifdef-uncomment-prefix reveal_code RegExp is incorrect HOT 1
- Upgrade to webpack 5 internally
- How to config for .ts file? HOT 2
- Has anyone gotten ifdef-loader to work with Angular 13? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ifdef-loader.