afterwind-io / preprocessor-loader

Bring the awesome "Conditional Compilation" to the Webpack, and more.
MIT License
40 stars 12 forks source link

Nested if statements don't resolve correctly. #29

Closed tech-meppem closed 2 years ago

tech-meppem commented 2 years ago

Nesting #!if statements in other #!if statements doesn't resolve how you would expect it to.

Having a param such as debug set to true, and then doing the following:

// #!if debug
    // #!if !debug
        // #!if debug
            console.log("hello")
        // #!endif
    // #!else
        console.log("world")
    // #!endif
// #!endif

You would expect it to output console.log("world"), however, console.log("hello") is produced. Changing the first line to // #!if !debug doesn't change the output either, even though you would then expect neither line to exist.

Also, having something like this:

// #!if !debug
    // #!if debug && !debug
        console.log("hello")
    // #!endif
    console.log("world")
// #!endif

debug is true, so the expected output would be for the whole block to excluded. However, both #!if statements terminate at the first #!endif, and so regardless of the value of debug, console.log("world") is always outputted.

Tested on webpack-preprocessor-loader@1.1.4 & webpack@5.68.0

afterwind-io commented 2 years ago

Nested ifs is indeed a basic feature in the preprocessor from other languages. Since the need of the whole preprocessor thing is pretty rare in vanilla javascript, the preprocessor loader is intended to be as simple as possible. That is why it lacks such features.

@tech-meppem Just curious, under what tricky circumstances you need such a complex nested condition?

Anyway, I am planing to implement the feature.

tech-meppem commented 2 years ago

@afterwind-io We build some of our frontend files differently for different units / devices. For example, lets say I have devices A, B and C. I was trying to do something like:

#!if A || C
// large block of code that's the same for A & C
#!if A
//specific to A
#!elseif C
//specific to C
#!endif
// large block of code that's the same for A & C
#!endif

And we were getting the problem that the second "large block of code" also appeared for B, because both ifs end at the first endif

afterwind-io commented 2 years ago

Implemented in v1.2.0

tech-meppem commented 2 years ago

Implemented in v1.2.0

Thanks! We'll try it out soon.