Skip to content

Automatically remove @keyframes duplicates #1506

Closed

Description

@Justineo

Hi,

When writing animations I don't want @keyframes rules appear in final .css files if they are not used (because if we support lots of animations that will be great amount of code). So I put a @keyframes rule inside a mixin, like this:

.play() {
  @keyframes play {
    from {
      margin-top: 0;
    }

    to {
      margin-top: 100px;
    }
  }
}

But if I call that mixin for several times, the @keyframes rule will also be repeated.

.test {
  .play();
}

.test2 {
  .play();
}

will output (in global scope):

@keyframes play {
  from {
    margin-top: 0;
  }
  to {
    margin-top: 100px;
  }
}
@keyframes play {
  from {
    margin-top: 0;
  }
  to {
    margin-top: 100px;
  }
}

Is there a way to avoid this? (I'm using lessc 1.4.2)

Activity

  1. Soviut commented on Aug 22, 2013

    @Soviut

    This seems to fall into the same area as grouping media queries.

    #950

    However, instead of grouping it's more of a "collapse" situation. It would also need to be optional, however, since there may be a valid reason someone might want to override a @keyframe rule.

    It seems that in both cases, some sort of syntax to indicate that a rule is allowed to be "grouped" or "collapsed" would be necessary.

  2. Justineo commented on Aug 22, 2013

    @Justineo
    ContributorAuthor

    @keyframes rules don't cascade so the last one always wins. I think overriding is not a problem because you just need to pick the last rule.

  3. lukeapage commented on Aug 22, 2013

    @lukeapage
    Member

    You could get round this by setting a global variable and using a guard so
    the mixin doesn't run if the variable is set...

  4. Justineo commented on Aug 22, 2013

    @Justineo
    ContributorAuthor

    @lukeapage
    I had considered that but it doesn't really solve the problem. I hope to 'Lessify' animation libraries like animate.css but it contains huge amount of @keyframes rules. If they cannot be produced into .css files upon mixin calls, I will have to output all of them. If I put them in mixins, I will only get those are actually used but with duplicates. I can't figure out a perfect solution in this case...

  5. lukeapage commented on Sep 1, 2013

    @lukeapage
    Member

    it would need to be an option - some people might want a key frame referenced inline or in another stylesheet.

  6. Justineo commented on Sep 2, 2013

    @Justineo
    ContributorAuthor

    @lukeapage
    Agree. So what I expect is that perhaps Less can offer some mechanism (maybe compilation option?) to automatically remove duplicate @keyframes rules (or even more general, other potential uncascaded rules) except the last one (by default it is turned off).

  7. lukeapage commented on Sep 2, 2013

    @lukeapage
    Member

    I thought your problem was unused keyframes, not duplicates?

  8. Justineo commented on Sep 2, 2013

    @Justineo
    ContributorAuthor

    I can think of two choices:

    1. directly write all @keyframes rules in the global scope, some of which may be unused, or
    2. put @keyframes rules in mixins and output them only when mixins are called, which generates duplicates.

    Your last comment is right, unused @keyframes rules cannot be removed because they might be referenced elsewhere. So I think there should be a way to remove duplicates.

  9. lukeapage commented on Feb 17, 2014

    @lukeapage
    Member

    Feature will be in next minor release - 1.7

  10. Justineo commented on Apr 22, 2014

    @Justineo
    ContributorAuthor

    @lukeapage
    The problem discussed in this issue doesn't seem to be solved in 1.7. The problem here is duplication after each mixin call, while 1.7 supported using rulesets as variables.

  11. seven-phases-max commented on Apr 23, 2014

    @seven-phases-max
    Member

    OK, I'll reopen this (probably @lukeapage mean some other @keyframes related feature not "removing duplicates"). So far I have not seen anyone was planning to implement this.

  12. changed the title [-]@keyframes inside mixins[/-] [+]Automatically remove @keyframes duplicates[/+] on Jan 16, 2016
  13. stale commented on Nov 14, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  14. atang-gfm commented on Jun 4, 2024

    @atang-gfm

    Hi @Justineo , wondering if you found a better solution to this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions