Comments (7)
To solve this problem, ifdef-loader should optionally allow something like this:
/// #if USE_FLASH
import embedpano from 'lib/krpano_flash/swfkrpano';
/// #else
/// import embedpano from 'lib/krpano/krpano';
/// #endif
Obviously it's perfectly legit code for any linting system because all of the #else
block is commented
ifdef-loader should remove all beginning-of-line triple-slashs in #elif
and #else
block at compile-time
from ifdef-loader.
Unfortunately this is not easy to solve. Because ifdef-loader
syntax resides in a //
comment, there are lot of cases where the resulting unprocessed code is not a valid code. This is this tool's biggest limitation.
When I have cases like that, I switch back to a simple if() else
, letting webpack
/Uglify
remove the unused code. E.g.
if(USE_FLASH) {
...
} else {
...
}
Now this is a bad example because import
s can't be put under if()
s, but there are lot of other cases where this trick does apply.
from ifdef-loader.
For now, we could use
let embedpano
/// #if USE_FLASH
embedpano = require('lib/krpano_flash/swfkrpano')
/// #else
embedpano = require('lib/krpano/krpano')
/// #endif
(CommonJS require()
is synchronous and similar to static import
)
or
/// #if USE_FLASH
import embedpano1 from 'lib/krpano_flash/swfkrpano';
// #else
import embedpano2 from 'lib/krpano/krpano';
/// #endif
const embedpano = typeof embedpano1 !== "undefined" ? embedpano1 : embedpano2
But the aforementioned #else
block comment should be a better way if implemented in ifdef-loader
ifdef-loader provides a reliable way to conditionally eliminate code, if we do something like
const USE_FLASH = true
// ...
const embedpano = USE_FLASH ? require('lib/krpano_flash/swfkrpano') : require('lib/krpano/krpano')
Then both 'lib/krpano_flash/swfkrpano' and 'lib/krpano/krpano' will probably be included in the final code (this depends on how far the dead code elimination of babel, webpack, uglify, terser could reach, and is not that predictable), which is likely not what we want.
from ifdef-loader.
I have the same issue. Is there any plan to fix this or suggestion for workaround?
from ifdef-loader.
I had a similar issue and after many other variants I only found the following workaround.
Not really nice code, but it works for me in the context of a clientside app with typescript, vue and webpack.
theming.vue
<script lang="ts">
let foo: any;
/// #if THEME=='special'
import special from "./themes/special/foo.vue";
// standard
/// #else
import standard from "./themes/standard /foo.vue";
/// #endif
if (typeof special != "undefined")
foo= special;
else if (typeof standard != "undefined")
foo= standard;
export default foo;
</script>
In my case I can go trough the eslint and typescript pipeline.
from ifdef-loader.
@tomchen wow, neat idea!
from ifdef-loader.
this is now addressed in v2.3.0 with the option ifdef-uncomment-prefix
(see the README).
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.