gulp-remove-code icon indicating copy to clipboard operation
gulp-remove-code copied to clipboard

Need function that remove comment without removing what inside.

Open jfmmm opened this issue 9 years ago • 9 comments

It would be nice if you could add a function or parameters that remove the "removeIf/endRemoveIf" comments without removing what inside of them.

I use gulp-remove-code to remove some stuff from the minified version of my JS files that I don't want in production, but in the unminified version I keep those thing there, but the "removeIf/endRemoveIf" stay as well.

Would love to be able to keep them but remove the "removeIf/endRemoveIf" comments from the final build.

maybe something like .pipe(removeCode({ removeCommentsOnly: true }))

Thx.

jfmmm avatar Apr 16 '15 22:04 jfmmm

Interesting suggestion. I'll have this implemented for the next version. Thanks.

crissdev avatar Apr 17 '15 07:04 crissdev

@jfmmm Couldn't you just use gulp-strip-comments?

Sugarcaen avatar Feb 13 '17 18:02 Sugarcaen

If I had a time machine maybe.

jfmmm avatar Feb 14 '17 00:02 jfmmm

@crissdev first, thanks for the module. Any chance of this getting implemented? You mentioned 'next version' back in 2015 and closed #11 last year (the title of which more closely matches what I'm thinking for this issue)

In the meantime, @Sugarcaen / @jfmmm how would you use gulp-strip-comments (or something else?) to only remove the empty/remaining '[rR]emoveIf lines? Seems like there's only an 'ignore' regex option there - no explicit 'remove only matches' option.

I'd love to be able to [at least somewhat] cleanly strip all 'true' removeIf sections as well as specifically the '[rR]emoveIf' comments from all 'false' removeIf sections...

JoshuaSCochrane avatar Mar 20 '18 03:03 JoshuaSCochrane

Looking at it I don't see how you would do that.. It would need some way to pass a regex or something.

Don't remember how I ended up fixing that and don't have access to that project anymore sadly.

jfmmm avatar Mar 20 '18 13:03 jfmmm

I just want to make sure I'm understanding you, @JoshuaSCochrane, its been a while since I've touched a project using gulp. You want to pipe something like:

    //removeif(production)
    app.doSomething()
    //endremoveif(production)
    app.doSomethingElse()

And get:

    app.doSomething()
    app.doSomethingElse()

Correct?

Sugarcaen avatar Mar 20 '18 15:03 Sugarcaen

@Sugarcaen precisely, except ideally controlled by an additional option to the gulp removeCode part, like if you have .pipe(removeCode({ production: true }, options.removeInactiveMarkers: true )) (or however you specify options - not quite clear and no examples in the readme). Or perhaps an addition to the markers themselves, something like //removeIf(production, markerEvenIfFalse: true) \ //endRemoveIf(production) so you can control it on a marker-by-marker basis (though I can't imagine why you'd want to keep any inactive markers, maybe someone does)...

I honestly very briefly considered doing something like this (contrived example):

//removeIf(production)
//removeIf(config_A)
//endRemoveIf(production)
module.exports.configBThing = configBThing;
//removeIf(production)
//endRemoveIf(config_A)
//endRemoveIf(production)

(so it would remove configBThing if config_A but always remove the removeIfs) but then I realized how abysmally ugly that was going to get and may not even work anyway... :p

JoshuaSCochrane avatar Mar 21 '18 01:03 JoshuaSCochrane

You can clean it up with gulp-delete-lines

// apply remove code
p = p.pipe(removeCode({ 'something': true }));

// remove lines starting with //removeIf and //endRemoveIf
p = p.pipe(deleteLines({'filters': [ /\/\/removeIf[^\n]*/g] }));
p = p.pipe(deleteLines({'filters': [ /\/\/endRemoveIf[^\n]*/g] }));

Note: This assumes that you use strictly //removeIf and //endRemoveIf comments with no space in-between.

Update: Using gulp-delete-lines instead of gulp-replace to remove whole lines.

mckravchyk avatar Sep 04 '20 09:09 mckravchyk

I also encountered this issue/feature request. I have a feature toggle removeIf(!featureA) which correctly remove the code if the featureA is disabled, but if it's enabled I just want to keep the code without the removeIf comment being there.

The gulp-delete-lines solution sounds good, maybe it could be integrated in the core options as @JoshuaSCochrane mentioned.

I used this:

removeCode()
    .pipe(gulpDeleteLines({
         'filters': [
             /\/\/(removeIf|endRemoveIf)[^\n]*/g,
         ]
    }))

Cristy94 avatar Feb 27 '21 18:02 Cristy94