less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Compile less mixin to file where it was called

Open gadzhimari opened this issue 8 years ago • 6 comments

Hi. I didn't find anything in docs for this case. I have two common components styles(Button and Link) in react application which has sometimes the same view. And I declared common styles in mixin.less file. But the problem is, when I look at chrome inspector and see that the output css came from mixin.less file instead of file where it was called. For example,

// mixin.less
.setSize(@size) {
   font-size: @size;
}

And this is calling above mixin

// Button.less
.setSize(16px);
// Link.less 
.setSize(16px); // output from mixin.less, but I want to compile that portion of css to isolated component style file

gadzhimari avatar Apr 03 '18 12:04 gadzhimari

Honestly I'm not sure I understand if you're talking about sourcemaps (the thing you see in the browser inspector) or just about destination css files. The concrete copy-paste-text (of all this stuff) from the inspector would be helpful (especially since a react framework may vastly interfere in how the things are actually compiled).

seven-phases-max avatar Apr 03 '18 13:04 seven-phases-max

Also (if its about sourcemaps) I wonder if there could ever be any consensus about that at all. E.g. notice that if I see some border: 1px solid shmoo; code, I'll prefer to see the exact line where shmoo came from and if it's:

.somemixin() {
    border: 1px solid shmoo;
}

that'll be the mixin definition.

Obviously, if in countrary that would be:

.somemixin(@foo) {
    border: 1px solid @foo;
}
// and then:
.somemixin(shmoo);

I'd like to see the mixin call instead of its definition.

In other words, there can be no the best general approach, it's very use-case dependent. (Basically this just illustrates a fundamental limitation of the use of sourcemaps in their current form as a debugging format for CSS compilers. It's just the lack of chaining references, e.g. "css-line -> mixin-call -> mixin-definition -> and-so-on").

seven-phases-max avatar Apr 03 '18 13:04 seven-phases-max

Yeah, I'm talking about sourcemaps. Here real-world example. I'm calling two mixins from Link.less file. Those mixins declared in mixin.less file. After compilation that I see in devtools 2018-04-03_18-17-14 Those mixins also called from other components less-files. And I want to in every less file where I call mixins, show this style only in component css-file. Is it possible?

gadzhimari avatar Apr 03 '18 15:04 gadzhimari

And how for instance does a mixin for .Link_color_blog look like? If it's something like:

.mixin() {
    link: #5db030; // whatever
}

.Link_color_blog {
    .mixin();
}

then it's an issue for sure.

If however it is something like:

.mixin() {
    &_color_blog {
        link: #5db030; // whatever
    }
}

.Link {
    .mixin();
}

it would be the thing I described above, i.e. either outcome is questionable and depends on a use-case.

seven-phases-max avatar Apr 03 '18 17:04 seven-phases-max

//mixin.less
.buttonColorMixin(
  @blockName,
  @modKey,
  @modVal,
  @property,
  @color,
) {
  .@{blockName}_@{modKey}_@{modVal} {
    @{property}: @color;
  }
}
// Link.less
@blockName: Link;
@colors:
  attention #f22222,
  outline #b9b9b9,
  blog #5db030,
  event #9a5397;

.for-each(@pair in @colors) {
  @key: at(@pair, 1);
  @value: at(@pair, 2);

  .buttonColorMixin(@blockName, color, @key, fill, @value);
}

Here I used additional less-plugin-list

gadzhimari avatar Apr 03 '18 20:04 gadzhimari

So this is what I described above: the selector is inside the mixin hence the sourcemap points to where it really is. E.g. imagine if you replace the mixin call with @import (also having a few selectors inside) and wonder why a sourcemap points into the file and not at the @import line itself.

seven-phases-max avatar Apr 03 '18 21:04 seven-phases-max