afterwind-io / preprocessor-loader

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

Support React/jsx style comments by ignoring newline characters in the main regex. #1

Closed cjweigle closed 6 years ago

cjweigle commented 6 years ago

Hello. You've made a great plugin. It's exactly what I needed. Thank you.

When preprocessing a project using react, adding a conditional block inside of a jsx expression causes a syntax error. The closing brace of the jsx comment gets removed with the block. Ignoring newline characters in the regex prevents this from happening.

Example Usage:

const MyConditionalComponent = ()=> <MyComponent>
<div><h2>Hello </h2>

{/*
// #!if SAY_WORLD
*/}
                World!
{/*
// #!endif
*/}

</div>
</MyComponent>
afterwind-io commented 6 years ago

@chronweigle Sorry for the delay. I'm quite busy recently. Looking into the case now.

afterwind-io commented 6 years ago

@chronweigle I'm a bit confused about the example. A little more complex one like:

{/*
// #!if SAY_WORLD
*/}
  World!
{/*
// #!else
*/}
  World2!
{/*
// #!endif
*/}

When SAY_WORLD is true, yields:

{/*
*/}
  World!
{/*
*/}

which looks odd and verbose, but still works. The whole file is proceeded line by line, split by \n. So the \s part in the regex should not match anything across the current line.

To solve the problem, I have made a new update to support new syntax like:

{/* #!if HELLO_WORLD */}
Hello World!
{/* #!endif */}

OR

/* #!if HELLO_WORLD */
Hello World!
/* #!endif */

See commit 0e44f43 for code changes and new tests.

afterwind-io commented 6 years ago

@chronweigle Hi, checkout v1.0.4 to see if it works :smiley:

cjweigle commented 6 years ago

Works like a charm, thank you