Replies: 4 comments 2 replies
-
Hmm, you should be able to implement a "pre-evaluation" visitor that essentially wraps the mixin? The tricky bit might be that mixins can be overloaded, so you'd have to do that with each mixin if you are calling an overloaded one. |
Beta Was this translation helpful? Give feedback.
-
Maybe this can get you started: function CountMixinsPlugin() {
this.mixinCalls = [];
}
CountMixinsPlugin.prototype.install = function (less, pluginManager) {
const pluginSelf = this;
function MixinCallCollector() {
this.isPreEvalVisitor = true;
this._v = new less.visitors.Visitor(this);
}
MixinCallCollector.prototype.run = function (root) {
return this._v.visit(root);
};
MixinCallCollector.prototype.visitMixinCall = function (node) {
const argStrings = node.arguments.map(argObj => {
const cssNode = argObj.value || argObj;
const css = (cssNode && cssNode.toCSS) ? cssNode.toCSS({}) : '';
return argObj.name ? `${argObj.name}: ${css}` : css;
});
pluginSelf.mixinCalls.push({
selector: node.selector.toCSS({}),
args: argStrings,
file: node.currentFileInfo && node.currentFileInfo.filename,
index: node.index
});
return node;
};
pluginManager.addVisitor(new MixinCallCollector());
};
module.exports = CountMixinsPlugin; const less = require('less');
const CountMixinsPlugin = require('./count-mixins-plugin');
const plugin = new CountMixinsPlugin();
let yourLessCode = `
.rounded(@radius: 5px) {
border-radius: @radius;
-webkit-border-radius: @radius;
}
.box-shadow(@x, @y, @blur, @color) {
box-shadow: @x @y @blur @color;
}
.important {
font-weight: bold;
color: red;
}
.button {
.rounded(10px);
.box-shadow(2px, 4px, 6px, #333);
.important;
}
.alert {
.rounded;
}
`;
less.render(yourLessCode, { plugins: [plugin] }).then(() => {
console.log('All mixin calls:', plugin.mixinCalls);
}); Output:
|
Beta Was this translation helpful? Give feedback.
-
Hello guys! Thank you for the response. Hello @puckowski! Thank you for the code example! I've tried it and got basically the same results. (Of course, the values in a real project were different). It seems, the following line doesn't work as expected: `argObj.name ? `${argObj.name}: ${css}` : css` I tried just In your output the
and not:
as I hoped. So got I. What I want to achieve is sort of strong static type checking. First, you “declare” a “type”:
Then any argument with a name, started with But I can't do that until I have defined |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys,
Is there a way to handle every call of every mixin from a plugin? I want to add some additional checks of arguments. In the handler I need a list of all params (names and values).
Regards,
Beta Was this translation helpful? Give feedback.
All reactions