less-docs icon indicating copy to clipboard operation
less-docs copied to clipboard

`@plugin` documentation

Open calvinjuarez opened this issue 9 years ago • 5 comments

This is a general call for documentation for the use of @plugin at http://lesscss.org/usage/#plugins. (See less/less.js#2416, and pull requests less/less.js#2479 & less/less.js#2522).

calvinjuarez avatar Apr 30 '15 18:04 calvinjuarez

Yeah, this needs to happen. It's probably Less's coolest new feature that has yet to be known about.

matthew-dean avatar Oct 01 '15 19:10 matthew-dean

edit

Seems to be working, though getting "less" inside your plugin is a bit annoying

test.js

// Following line is a bit unintuitive:
var less = process.mainModule.children[2].exports;

less.functions.functionRegistry.add("myfunction", function() {
     console.log("here!", arguments);
     return new less.tree.Color("ff0000");
});

something.less

@plugin "test.js";
test {
  color: myfunction();
}

Test it like this: lessc something.less

I think for simple js plugins with no node command, the "less" should be given as a global to the local plugin.

Or alternatively the require should be available in simple local plugins.

Ciantic avatar Jan 03 '16 16:01 Ciantic

I'm with @Ciantic, there needs to be an easier way to get to Less from a plugin.

calvinjuarez avatar Jan 04 '16 20:01 calvinjuarez

Actually, it works like this:

test.js

functions.add('myfunction', function() {
  return new tree.Color('ff0000');
});

The variables functions and tree are available because that's how it is implemented:

new Function("functions", "tree", "fileInfo", contents);

thorn0 avatar Jan 27 '18 17:01 thorn0

@thorn0 less is also available within a @plugin call in 3.x.

https://github.com/less/less.js/blob/3.x/lib/less/environment/abstract-plugin-loader.js#L65

matthew-dean avatar Jan 27 '18 20:01 matthew-dean