Skip to content

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

Open

Description

@jfmmm

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.

Activity

  1. crissdev commented on Apr 17, 2015

    @crissdev
    Owner

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

  2. Sugarcaen commented on Feb 13, 2017

    @Sugarcaen

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

  3. jfmmm commented on Feb 14, 2017

    @jfmmm
    Author

    If I had a time machine maybe.

  4. JoshuaSCochrane commented on Mar 20, 2018

    @JoshuaSCochrane

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

  5. jfmmm commented on Mar 20, 2018

    @jfmmm
    Author

    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.

  6. Sugarcaen commented on Mar 20, 2018

    @Sugarcaen

    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?

  7. JoshuaSCochrane commented on Mar 21, 2018

    @JoshuaSCochrane

    @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

  8. mckravchyk commented on Sep 4, 2020

    @mckravchyk

    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.

  9. Cristy94 commented on Feb 27, 2021

    @Cristy94

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions