Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need function that remove comment without removing what inside. #3

Open
jfmmm opened this issue Apr 16, 2015 · 9 comments
Open

Need function that remove comment without removing what inside. #3

jfmmm opened this issue Apr 16, 2015 · 9 comments

Comments

@jfmmm
Copy link

jfmmm commented Apr 16, 2015

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.

@crissdev
Copy link
Owner

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

@Sugarcaen
Copy link

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

@jfmmm
Copy link
Author

jfmmm commented Feb 14, 2017

If I had a time machine maybe.

@JoshuaSCochrane
Copy link

@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...

@jfmmm
Copy link
Author

jfmmm commented Mar 20, 2018

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.

@Sugarcaen
Copy link

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?

@JoshuaSCochrane
Copy link

@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

@mckravchyk
Copy link

mckravchyk commented Sep 4, 2020

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.

@Cristy94
Copy link

Cristy94 commented Feb 27, 2021

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,
         ]
    }))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants