grunt-processhtml icon indicating copy to clipboard operation
grunt-processhtml copied to clipboard

How to completely omit blocks with unmatched build targets?

Open leolux opened this issue 8 years ago • 9 comments

Currently every block gets written to the destination file even when the build target does not match the block target. But this results in too many blocks within the destination file (each for every target).

Is there a way to completely exclude/omit blocks based on the build target? Or is it possible to combine or nest "build:remove:dev" with "build:js:dist inline" somehow to achive the desired effect?

leolux avatar May 24 '17 13:05 leolux

https://github.com/dciccale/grunt-processhtml#optionsstrip

dciccale avatar May 24 '17 14:05 dciccale

As the documentation says: "Specifying true will strip comments which do not match the current target" In my case this means that the destination file still contains all unprocessed script elements but without comments. Removing the comments is fine but how to remove the block content when the build target is unmatched?

leolux avatar May 24 '17 14:05 leolux

Could you provide a short example of the input -> output you seek?

dciccale avatar May 24 '17 14:05 dciccale

Ok, I got it! It is possible to have multiple targets seperated by comma. This allows me to remove the block for a certain target. A nice feature would be to have a target which includes the block only on a matched target thus all unmatched targets get removed automatically.

leolux avatar May 24 '17 14:05 leolux

To complete with an example:

<!-- build:remove:dist,js:dev inline -->
<script src="my/lib/path/lib.js"></script>
<!-- /build -->

This special comment combines "remove:dist" with "js:dev inline". Running "grunt dev" will replace the script element with the corresponding inline javascript. And running "grunt dist" will write nothing to the destination file.

Im am not sure why this syntax actually works because the documentation only says that you canpass multiple comma-separated targets and but not multiple types. In my example I passed multiple build types with a single target for each type. Is my syntax valid in your eyes?

Update: "grunt dev" does not inline the javascript so the example does not work as intended.

leolux avatar May 24 '17 15:05 leolux

hm i see thanks for the feedback. we'll see if this comes up again to make it a feature. not many grunt users anymore though.

however this maybe more useful to include directly in the core parser https://github.com/dciccale/node-htmlprocessor will keep it in mind

dciccale avatar May 24 '17 15:05 dciccale

Your example seems incomplete

dciccale avatar May 24 '17 15:05 dciccale

I am having problems escaping comments in markdown code. Now it looks fine :-)

leolux avatar May 24 '17 15:05 leolux

Update: The example does not work as expected for "grunt dev" because the script does not get inlined. So the syntax given in the example is wrong. Is it even possible to pass multiple types in the comment ("remove" and "js")?

leolux avatar May 24 '17 15:05 leolux