You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As was already mentioned initially (and then in #10 and in various topics here and there), @plugin feature needs further improvements as its current implementation was just a temporary solution to get something in the release ASAP.
So the very first step for the unification would be to allow the @plugin directive to load both formats, i.e.:
@plugin"clean-css";
first try to load the "standard" format, i.e. less-plugin-clean-css (in the directories specified in the docs)
and only then try to load clean-css.js file (of the "easy-functions" format, in the directories of the compiled less project).
Contrary:
@plugin"clean-css.js";
looks directly for an "easy-functions" clean-css.js file.
As also mentioned earlier, loading "standard format" plugins with @plugin directive is expected to have certain acceptable limitations/restrictions to work properly (e.g. no special scoping and the directive should appear before any code it may have an effect on (specifically for "visitor" plugins)).
Setting "standard format" options via @plugin are yet to be specified later. Obviously the options will be set via typical @import/@media-like syntax, e.g.:
Good start. I'm actually less familiar with what you call the "standard format" plugins because the usage always seemed overly complex, so I never bothered with it. But @plugin plugins are a no-brainer. They're easy to write and they always work. Part of the reason they're easy is because they're limited, which is actually a good feature.
In any case, I don't see anything in the docs pertaining to directory location of the old plugin format. The plugin file actually has to be "pre-loaded", does it not? So I don't think Less looks for it anywhere. Whereas @plugin just follows typical @import Less rules. The plugin file should be with your "Less project" somewhere, which you need to reference explicitly.
Having @plugin do a special search first to prepend "less-plugin" on the name of the file seems like a regression. IMO it should continue to follow current @import/@plugin behavior of searching exactly the file you just wrote, in the relative or absolute directory you specified (barring any modifications of directory searching in your initial Less options).
If an author wants to follow a convention of using "less-plugin-" but wants to shorten the reference, they can just easily write:
@p: "less-plugin";
@plugin "@{p}-clean-css";
Or whatever else they want to do there. Adding two specific words in the name of a JavaScript file doesn't make sense to make a dedicated feature, since names of files are arbitrary. AND, if they want plugins to do such prepending automatically, they can just add a "less-plugin-" prepending plugin! 😉
@plugin "less-plugins";
@plugin "clean-css";
(Although the latter wouldn't be recommended, since it would be altering any plugin reference used anywhere. But still, it's an option.)
I agree that adding "options" to plugins can be discussed later.
Having @plugin do a special search first to prepend "less-plugin" on the name of the file seems like a regression
It depends. Here you're thinking (as @rjgotten also most likely did when submitting the initial @plugin PR) as someone who is just about to write and use his own project specific custom functions. But (not putting any IMHO, but it's obviously assumed anywhere anyway) the general auditory is just about:
npm install (-g) less-plugin-stdlib (the prefix is actually optional and used only to easily identify Less plugins among other nmp-modules)
@plugin "stdlib";
We can get back to less/less.js#1861 (comment) and start all over again from scratch of course. But this is how the "standard format" works (lessc --stdlib) and I don't see any reason for @plugin to not follow this convention.
searching exactly the file you just wrote
@plugin "path-to/the-file-you-just-wrote.js";
In any case, I don't see anything in the docs pertaining to directory location of the old plugin format.
Then it is the issue of the docs to be fixed.
Also speaking of the "old format". I guess we have to clarify this. The "standard format" is no way "the old" but actually the one and the only format. You may be less familiar with it but it's the only way by now to make something beside custom functions, so I recommend you to get more familiar with it and start looking where it has to be improved to meet your vision. Otherwise you're risking to continuously reinvent the wheel by proposing parallel incompatible solutions (like less/less.js#2852).
In other words, it's not about making the @plugin to be able to do what "standard format" can do, but just about making the current @plugin implementation to conform to the "standard format". Otherwise I'm afraid we definitely need to stop, disable both features, get back to less/less.js#1861 and start redesigning everything from scratch.
Also speaking of the "old format". I guess we have to clarify this. The "standard format" is no way "the old" but actually the one and the only format. You may be less familiar with it but it's the only way by now to make something beside custom functions, so I recommend you to get more familiar with it and start looking where it has to be improved to meet your vision.
That's fair. But what I meant was that we had talked about moving plugins to be loadable under @plugin, and I wasn't sure that the "standard format" was compatible with that model.
But this is how the "standard format" works (lessc --stdlib) and I don't see any reason for @plugin to not follow this convention.
This is also a good point. I see where you're coming from. I had assumed that a) backward compatibility was not possible because of the way plugins are loaded, b) the solution might have to be to deprecate plugin format A, and have plugin format B (@plugin) have the same technical capability as the current format but I wasn't sure that it could be compatible with the way plugins are currently written.
But, you're not seeing them as "two formats", so much as a newer way (and maybe easier way) to call a Less plugin.
In other words, it's not about making the @plugin to be able to do what "standard format" can do, but just about making the current @plugin implementation to conform to the "standard format".
I hear you. Thank you for clarifying. One advantage we have is that I think we can classify the current @plugin implementation as "experimental" (since it's not yet documented), and so therefore it shouldn't need any official "deprecation" for any possible breaking changes (although it would be nice to avoid).
One comment about loading order, though, as far as to which one is first. I still think the "explicit name" should be checked first. The original docs say, "We recommend the plugin starts "less-plugin" though that isn't required."
"less-plugin" is supported, but it still seems like a convenience addition that it's checked after the explicit name is checked, as the name is an optional recommendation. So I think the name check would happen more like:
@plugin "clean-css";
Check for npm installed "clean-css" plugin (or loaded file in the browser?).
Check for "clean-css.js" relative to the Less file that just called it.
Check for npm installed "less-plugin-clean-css" plugin.
Check for "less-plugin-clean-css.js" relative to the Less file that just called it.
There's bridging @plugin to current plugin implementations, but there's also bridging standard Less plugins to other file referencing conventions in Less. IMO convenience methods should typically be a fallback.
Well, my proposal of the order is based solely on "the better performance for the masses" sense of view. Yet again assuming the majority of users will use "public/shared" modules (which will have the prefix for obvious reasons) instead of self-written plugins, it would make sense to not burden the majority with additional search step that rarely matches anyway.
(Though with or without prefix and with or without extension, it still would make sense to first look for the local installation and only then for the global. Now the only problem is to define what is "local" in relativity to the current Less file (unlike node, Less does not have a concept of the "project directory" (usually it's assumed to be the directory of the master less file but it's not always the case)).
The only concern is the "browser-side" plugins: obviously since a node-based plugin has to be browserified somehow before to be used client-side, and that browserified version also ends in plugin.js file we have to introduce some artificial marker to distinguish between two formats (assuming we'll keep the "easy functions" format at least for the backward-compatibility even if it's totally overlapped with the "standard format").
Yet again assuming the majority of users will use "public/shared" modules (which will have the prefix for obvious reasons) instead of self-written plugins, it would make sense to not burden the majority with additional search step that rarely matches anyway.
A public/shared module does not automatically mean an "npm-installed" one. The jQuery plugin ecosystem does not rely on Node as a dependency, and I don't think Less should either. Officially, Less is platform-agnostic. To me, that means that "core Less" should assume nothing other than the file reference you gave is a valid, local file. True, the platform extensions can then define what is "valid and local". So, it would make sense that the Node platform extension might have a different search mechanism / order than what happens in-browser.
As an example that you're drawing from for convention: lessc is a platform-specific Node extension of Less.js, albeit an official one. So, by definition, the features of lessc are not universal, nor are the conventions.
I think, conventionally, Less plugins have basically been "Node-only", partly because they rely on NPM, and partly because they're written in such a way that excludes the browser (relies on Node as a dependency). I think that's part of what has limited plugin adoption and ease-of-use, so I wouldn't want to see that entirely as a guiding principle.
For instance, there are currently "Less plugins" that rely on NPM, explictly say they don't work in the browser, but are nothing more than additional Less functions. That's wholly unnecessary. I think for a successful plugin ecosystem, we should guide developers to create platform-agnostic Less plugins, since Less itself is platform-agnostic. Just like there were mistakes made with the initial @plugin implementation, I believe there were mistakes made with the initial plugin implementation in 2.0.
Which certainly doesn't mean that we should break those plugins on a whim. I just want to really focus on:
What makes plugins easy to write
What makes plugins easy to use
I think those are the two most-essential pieces of a successful plugin system. If, in our documentation, we give examples that suggest, "It's okay to create a Node-only plugin", then we're effectively saying that Node is really the only valid Less environment, which makes non-Node/NPM plugin scenarios harder to use.
So as to:
The only concern is the "browser-side" plugins: obviously since a node-based plugin has to be browserified somehow before to be used client-side
I think we should caution people against making "Node-side" or "browser-side" plugins as much as possible. Or at least thinking if such a thing is absolutely necessary. Rather than thinking of Less as a Node-based system that you add to with NPM-hosted plugins, it might be easier for some users to treat Less plugins like jQuery ones, where the plugins travel with the associated Less project, not that get installed somewhere "in-system", as those have dependencies which are not portable. Less plugins can easily exist as a single JS file which gets copied into your local Less source file directory.
But, the nice thing is that if we do things right, we don't have to adopt either "philosophy". Someone could choose to manage their Less plugins via NPM, and another person could copy and paste a file. If we write guidelines and examples appropriately, people can have the best of both worlds.
Just as an example as far as cross-platform plugin conventions, I've seen a library like Ractive (which can run in both browser and Node), basically do conventions as:
Plugins either exist as a single file, or include a dist/ directory with a single file that can be used in-browser (similar to what Less itself does)
Plugins basically have Node consumption and browser consumption instructions as a standard part of each plugin repo
One thing that PostCSS does is it has a plugin generation script which creates your initial repo. So, we could do something similar where someone enters the name of their plugin, and it does the following:
Sets up a package.json file, a dist/ folder, an initial index.js file (for NPM/Node), and both build and test scripts for the plugin
Populates the documentation to demonstrate Node usage via "npm install" and via @plugin.
Demonstrates in-browser use of the distribution file (possibly without the "less-plugin-" prefix? Or as an option when generating the repo?) with @plugin
a plugin generation script which creates your initial repo.
This is probably better to be done with something like Yeoman (though personally I'm not very familiar with it since it was broken for my dev-platform for a while).
Here you're thinking (as @rjgotten also most likely did when submitting the initial @plugin PR) as someone who is just about to write and use his own project specific custom functions.
Actually; I wrote @plugin the way I wrote it, to transparantly support plugins being included via transitive closure, i.e. , as dependencies of dependencies, without bothering the consumer (developer) of the Less file that imports the first dependency.
It was meant as a zero-setup, collision-free means to include custom functions for Less frameworks.
The zero-setup aspect makes it work transparantly with any side-by-side editor that calls out to Lessc to create CSS previews, without having to mess with custom settings. That's an additional perk and may be a good reason to also develop your own project-specific functions in the @plugin format.
we have to introduce some artificial marker to distinguish between two formats
Imports may use URLs, so why not @plugin "node://clean-css" to instruct the compiler to use the NodeJS module lookup rules.
A public/shared module does not automatically mean an "npm-installed" one.
Notice there's no mention of node or npm in the initial post.
@plugin"clean-css";
is fine to load PHP-written (optionally even particular platform-specific, e.g. Wordpress) less-plugin-clean-css in less.php for example.
It was meant as a zero-setup, collision-free means to include custom functions for Less frameworks.
This is illusion (no offense meant :) - it stops to be true as soon as you start to share the same plugin in different projects (so you either need a copy of the plugin in each or setup the shared paths, thus -> no zero-setup) or start to use different libraries that include different versions of the same plugin (so sooner or later someone comes with an idea of 'Less plugin manager/packager' utility).
So in general it's all the same problems whatever approach you choose.
This is illusion (no offense meant :) - it stops to be true as soon as you start to share the same plugin in different projects (so you either need a copy of the plugin in each or setup the shared paths, thus -> no zero-setup) or start to use different libraries that include different versions of the same plugin (so sooner or later someone comes with an idea of 'Less plugin manager/packager' utility).
Different versions of the same plugin are only an issue if you use NPM to manage your plugins. NPM is famously terrible and being able to install multiple versions to use simultaneously.
The way @rjgotten wrote it seems like it would work fine in that scenario, since functions are scoped, and the included JS file doesn't contain any sort of unique identifier that can collide if you have multiple copies. Therefore, you should be able to do:
#library1 {
@plugin"library1/foo-plugin.js";
bar: foo();
}
#library2 {
@plugin"library2/foo-plugin.js"; // same plugin, different version
bar: foo();
}
Standard/current format plugins modify Less at the core/root level, and so are inherently prone to collisions in the way you describe. Currently @plugin functions are not. It's one of the reasons that I feel that the copy/paste of a distribution file is actually the more appropriate recommended method. It can get included in the source files right where you need it.
And then you have everything that "builds" your final CSS file all in one place. It's not pulling your Less file from a local directory and then a JS file from some other place, even though they have similar URL paths.
I also agree though that this is probably the wrong way to go:
@plugin "node://clean-css"
Unless...... like the way that "less-plugin-" is magically prepended for lessc, we assign a meaning to the browser. (The browser knows to look in node_modules.)
That said, it's such a specific case that, again, the "node://" URL scheme should itself be supported by a plugin. With proper architecture and good documentation, we can keep the core light and support those kinds of customizations better.
I've said it before in other threads: there's no reason there can't be official plugins that pull in optional JS files. We have official platform extensions, so something that searches node_modules/ could be a supported plugin. A benefit to having official plugins is to help establish plugin convention.
That said, it's such a specific case that, again, the "node://" URL scheme should itself be supported by a plugin.
Ah. You beat me to it. Yeah; that was going to be my argument as well.
Also, just for the record: Node.js module lookup is different from what Lessc is currently using. Lessc is using require in its plugin loader, alright, but it searches an opinionated number of folders in a non-standard order, rather than following the node module lookup algorithm. (Look up the maddening details of the lookup algorithm at your own risk.
So it should be called something different from node:// anyway.
Well, it's not hard to guess what is the key differences between our visions. Some treat plugins as a method to extend a particular project, others think of it as an extension to the language/compiler to meet that project needs (hence first are biased towards local copy-pasted js files, second favor shared libs). And there's no way to prove anything but by practice itself.
And then you have everything that "builds" your final CSS file all in one place
Yep (for me there's no way to think of lists, colors or functions plugins as a part of a local project).
TL;DR: I figured out that the two formats actually needed to be unified after all, and went ahead and did the work.
Detailed Explanation
I started work on updating @plugin to @use, and then started to test it with a project. If you recall a while ago, I submitted a PR proposal for interpreting custom at-rules as functions. @seven-phases-max's suggestion was that this was handled by a plugin that added the appropriate visitors. This was fine by me, so I started developing this with the inline syntax.
However.... I began to run into some problems. The right vars / objects weren't exposed to the inline scripts, so those pieces from the PluginManager needed to be exposed. Then, I realized there were some use cases that couldn't really be handled by a script running once and then exiting. The @plugin/@use statements needed to support re-use. In other words, they needed to expose functions on a plugin object. You know, functions like install() and possibly a few others. (Meaning these "extensions" were beginning to look pretty much identical to "plugins".)
Meanwhile, I came across the plugin loader used by lessc. It handles retrieving a plugin by module name, and does a lot of convenient things like checking the version #, trying to prepend "less-plugin-" if no match is found, and if a plugin returns a function instead of an object, it attempts to create a new object out of it, which the Less.js API itself doesn't do, but should.
All of that led me to the inevitable conclusion that @seven-phases-max's instincts were right all along, that really, the historical "function script" @plugin format needed to align with documented plugin syntax, rather than creating another "format" which is actually not distinct enough to make sense.
Once I had the idea that the two formats might need to be unified after all, I obsessively programmed to see if this was a possibility, and eventually I had a branch with these changes.
Both formats have a single backwards-compatible format, by:
a. Evaluating less Node plugins within the function context @rjgotten created for @plugin.
b. Adding either file type to the plugin manager
c. Allowing an evaluated plugin file to either assign to module.exports = { ... } or return { ... } to return an object with functions. (Or returning a function which, when invoked, returns an object.)
d. Conditionally evaluating the install() method, only if it exists.
e. Using the plugin loader method for lessc as a "platform-specific" loader for @plugin imports.
f. Adding a "local path" option to that loader, so that the plugin loader for Node will also look locally (local to the @plugin reference), if @plugin is what invoked it.
I changed the visitor collection in the plugin manager to be a dynamic iterator. Meaning, if visitors are added during visiting, then those will still get visited. I did this to support the use case where @plugin was invoked within a ruleset. (I noticed while testing that visitors added "late" would never be visited.) This still makes visitors global, but I'm not sure if that's an issue.
Based on initial testing, all of this works. I was able to do this:
@plugin"inline-urls"; // loads less-plugin-inline-urls module installed via 'npm install';@plugin"local-file"; // loads local-file.js located in the same folder as the file where this was invoked
Note that the behavior of Node module loading is handled by the less-node plugin manager. I still need to test the less-browser plugin loader, which would only support local files. (Anything else would be too expensive in the browser, and it doesn't make sense to support Node in browser mode.)
Final Thoughts:
After seeing that all could be made possible under @plugin, I realized there was really no need to have to explicitly load a plugin under any circumstance (via passing to Less options). You don't have to require() a plugin and pass an object. @plugin is a cross-platform declaration, so it makes things a ton easier when explaining it.
I still have some more work to do to:
Add / test browser support
Add tests?
Fix thrown exceptions. Those are still a bit awkward when there's an exception in the plugin file. The returned LessError is not quite accurate.
Maybe having a look at the Less tree API. But that could be for 3.x.
I added these changes to the feature/@plugin branch and started a new 3.x branch, but, as I don't think there are any breaking changes, it could come in before 3.x.
Btw., indeed re-reading posts above I can see now that probably we digged too deep into "where plugin is loaded from" thing, which is in general quite subtle issue and won't be worth that possible overengineered @plugin/@use stuff while not really targeting the main thing (i.e. let the Less be as extensible as possible in an as easy as possible fashion).
@seven-phases-max Yes, I realized that too. It's actually quite a minor point. On Node the file searching order is not that relevant and really not that expensive. And I think I got stuck on having similar behavior on two different platforms, which is also not necessary. The compiling environments are very different, and it's easy to say, "On Node, Less will search node_modules first. In the browser, it won't." My plan is to have the browser loader do normal @import behavior, with the caveat of trying to prepend "less-plugin-" as a second try. With the exception that having ".js" just looks locally, as you first proposed. Basically, you had it right the first time, lol.
Once I got into the codebase and started digging/coding around plugins, the way you had described it made more sense. The tricky part was to fold in existing @plugin behavior, which we didn't necessarily have to do, but I liked how much it simplified things the way that @rjgotten had done it.
Meaning: as I currently have it written, a plugin doesn't have to export anything. It can add one function when it's first evaluated and be done (since the "functions" registry is now passed into the scope, as long as @plugin is the caller). So it makes the plugin "signature" optional (albeit recommended).
I have to do some work yet to fix lessc to use the new plugin loader, and setting up the browser, but should be good to go before too long.
If you have a chance to look and make any suggestions, please do.
I'm recommending that we do a 3.0 release with these plugin changes sooner rather than later. I've disabled inline JavaScript along with the plugin changes because of a security report that was sent to me. Talk to me offline if you want more details.
I realized in the process of documenting plugin usage (which I now realize is a good practice for figuring out if intended usage actually makes sense), that there were problems with a few specifics in the "format" of the file.
Namely: having globals like less and functions available, or ending with a return statement created add-on problems, like:
Linters immediately complain, and rightly so, because the file is essentially a JavaScript "fragment", to be later constituted.
It limits / changes where / when in the Less.js lifecycle a plugin can be added. For instance, it can longer be included in a <script> tag as is currently documented for pre-loading plugins.
I can (and probably will) still leave less, tree, and functions available as globals for any legacy @plugin v2.5.0 files that may exist, although there shouldn't be a lot due to lack of documentation. These globals will be replaced by local vars of the same name (and the same objects) when using the boilerplate UMD code (except for tree, but a reference to tree is no longer necessary), so it shouldn't be a problem.
Once again, if anyone objects, let me know, but the boilerplate code should allow plugins to be loaded in Node.js, either via require() or via lessc or via @plugin, as well as loading in the browser via @plugin or in a <script> tag.
I'm not sure of a use case for a cross-platform file-manager. A plugin that connected to the Dropbox API? Seems silly in the browser. But the point is just to get a few plugin examples that are universal, so the use case doesn't matter that much.
If someone can also come up with a repo layout like:
-src/// can be directly "require'd()"-index.js-otherStuff.js-UMD-wrapper.js// wraps module in boilerplate for browser buildmyplugin.js// builds for browsermyplugin.min.jsgulpfile.js// makes the above browser buildspackage.json// has necessary includes for building a plugin
That keeps plugins simple & flat, and can keep source files organized while outputting single-file builds.
@matthew-dean
I'm all for the UMD pattern. Makes everything nice and explicit. However, I'd rename registerPlugin in your documentation to a more simple factory. That particular function doesn't actually perform any registering of the plugin, it only creates the plugin object. Less itself does the actual registering by calling the install member.
It's kind of sad that the browser-globals based version bloats the UMD config because of a need to detect prior existence of the global less object and the less.plugins array configuration. I wonder if you could shorten it. Possibly something like:
Or maybe just forego the two-tier namespace altogether and just use a global less_plugins array that Less can detect in addition to the properly namespaced less.plugins. That would allow you to turn it into a one-liner, such as:
Also, my gut tells me the install method is better with the parameter order altered to place pluginManager last. The less and functions members are probably going to be the most used, which will allow plugin authors to omit the more complex pluginManager from implementations that don't need it.
Also, my gut tells me the install method is better with the parameter order altered to place pluginManager last.
Ideally, yes, but that would break existing plugins. The function registry is something I added, otherwise there's no "local" function registry passed to install, as it was previously just a global var.
Renaming to factory() is fine. It's an arbitrary name.
Yes, you are right that I was trying to make it so Less config could appear before or after plugin <script> tags, if someone was including a plugin that way. In fact, I was hoping to simplify the documentation so that adding a plugin didn't mean creating a Less object at all (if you weren't adding any other configuration).
So, yeah, I could make it another global to shorten it, but I would capitalize LESS_PLUGINS for an internally-used global. That's just the naming convention I've used.
Update: actually, you're right on another count. It does need to be a second global, because otherwise you'll have people do this:
Activity
matthew-dean commentedon May 1, 2016
Good start. I'm actually less familiar with what you call the "standard format" plugins because the usage always seemed overly complex, so I never bothered with it. But
@pluginplugins are a no-brainer. They're easy to write and they always work. Part of the reason they're easy is because they're limited, which is actually a good feature.In any case, I don't see anything in the docs pertaining to directory location of the old plugin format. The plugin file actually has to be "pre-loaded", does it not? So I don't think Less looks for it anywhere. Whereas
@pluginjust follows typical@importLess rules. The plugin file should be with your "Less project" somewhere, which you need to reference explicitly.Having
@plugindo a special search first to prepend "less-plugin" on the name of the file seems like a regression. IMO it should continue to follow current@import/@pluginbehavior of searching exactly the file you just wrote, in the relative or absolute directory you specified (barring any modifications of directory searching in your initial Less options).If an author wants to follow a convention of using "less-plugin-" but wants to shorten the reference, they can just easily write:
Or whatever else they want to do there. Adding two specific words in the name of a JavaScript file doesn't make sense to make a dedicated feature, since names of files are arbitrary. AND, if they want plugins to do such prepending automatically, they can just add a "less-plugin-" prepending plugin! 😉
(Although the latter wouldn't be recommended, since it would be altering any plugin reference used anywhere. But still, it's an option.)
I agree that adding "options" to plugins can be discussed later.
seven-phases-max commentedon May 1, 2016
It depends. Here you're thinking (as @rjgotten also most likely did when submitting the initial
@pluginPR) as someone who is just about to write and use his own project specific custom functions. But (not putting any IMHO, but it's obviously assumed anywhere anyway) the general auditory is just about:npm install (-g) less-plugin-stdlib(the prefix is actually optional and used only to easily identify Less plugins among other nmp-modules)@plugin "stdlib";We can get back to less/less.js#1861 (comment) and start all over again from scratch of course. But this is how the "standard format" works (
lessc --stdlib) and I don't see any reason for@pluginto not follow this convention.@plugin "path-to/the-file-you-just-wrote.js";Then it is the issue of the docs to be fixed.
Also speaking of the "old format". I guess we have to clarify this. The "standard format" is no way "the old" but actually the one and the only format. You may be less familiar with it but it's the only way by now to make something beside custom functions, so I recommend you to get more familiar with it and start looking where it has to be improved to meet your vision. Otherwise you're risking to continuously reinvent the wheel by proposing parallel incompatible solutions (like less/less.js#2852).
In other words, it's not about making the
@pluginto be able to do what "standard format" can do, but just about making the current@pluginimplementation to conform to the "standard format". Otherwise I'm afraid we definitely need to stop, disable both features, get back to less/less.js#1861 and start redesigning everything from scratch.matthew-dean commentedon May 1, 2016
That's fair. But what I meant was that we had talked about moving plugins to be loadable under
@plugin, and I wasn't sure that the "standard format" was compatible with that model.This is also a good point. I see where you're coming from. I had assumed that a) backward compatibility was not possible because of the way plugins are loaded, b) the solution might have to be to deprecate plugin format A, and have plugin format B (
@plugin) have the same technical capability as the current format but I wasn't sure that it could be compatible with the way plugins are currently written.But, you're not seeing them as "two formats", so much as a newer way (and maybe easier way) to call a Less plugin.
I hear you. Thank you for clarifying. One advantage we have is that I think we can classify the current
@pluginimplementation as "experimental" (since it's not yet documented), and so therefore it shouldn't need any official "deprecation" for any possible breaking changes (although it would be nice to avoid).matthew-dean commentedon May 1, 2016
One comment about loading order, though, as far as to which one is first. I still think the "explicit name" should be checked first. The original docs say, "We recommend the plugin starts "less-plugin" though that isn't required."
"less-plugin" is supported, but it still seems like a convenience addition that it's checked after the explicit name is checked, as the name is an optional recommendation. So I think the name check would happen more like:
There's bridging
@pluginto current plugin implementations, but there's also bridging standard Less plugins to other file referencing conventions in Less. IMO convenience methods should typically be a fallback.seven-phases-max commentedon May 1, 2016
Well, my proposal of the order is based solely on "the better performance for the masses" sense of view. Yet again assuming the majority of users will use "public/shared" modules (which will have the prefix for obvious reasons) instead of self-written plugins, it would make sense to not burden the majority with additional search step that rarely matches anyway.
(Though with or without prefix and with or without extension, it still would make sense to first look for the local installation and only then for the global. Now the only problem is to define what is "local" in relativity to the current Less file (unlike
node, Less does not have a concept of the "project directory" (usually it's assumed to be the directory of the master less file but it's not always the case)).The only concern is the "browser-side" plugins: obviously since a node-based plugin has to be browserified somehow before to be used client-side, and that browserified version also ends in
plugin.jsfile we have to introduce some artificial marker to distinguish between two formats (assuming we'll keep the "easy functions" format at least for the backward-compatibility even if it's totally overlapped with the "standard format").matthew-dean commentedon May 1, 2016
A public/shared module does not automatically mean an "npm-installed" one. The jQuery plugin ecosystem does not rely on Node as a dependency, and I don't think Less should either. Officially, Less is platform-agnostic. To me, that means that "core Less" should assume nothing other than the file reference you gave is a valid, local file. True, the platform extensions can then define what is "valid and local". So, it would make sense that the Node platform extension might have a different search mechanism / order than what happens in-browser.
As an example that you're drawing from for convention:
lesscis a platform-specific Node extension of Less.js, albeit an official one. So, by definition, the features of lessc are not universal, nor are the conventions.I think, conventionally, Less plugins have basically been "Node-only", partly because they rely on NPM, and partly because they're written in such a way that excludes the browser (relies on Node as a dependency). I think that's part of what has limited plugin adoption and ease-of-use, so I wouldn't want to see that entirely as a guiding principle.
For instance, there are currently "Less plugins" that rely on NPM, explictly say they don't work in the browser, but are nothing more than additional Less functions. That's wholly unnecessary. I think for a successful plugin ecosystem, we should guide developers to create platform-agnostic Less plugins, since Less itself is platform-agnostic. Just like there were mistakes made with the initial
@pluginimplementation, I believe there were mistakes made with the initial plugin implementation in 2.0.Which certainly doesn't mean that we should break those plugins on a whim. I just want to really focus on:
I think those are the two most-essential pieces of a successful plugin system. If, in our documentation, we give examples that suggest, "It's okay to create a Node-only plugin", then we're effectively saying that Node is really the only valid Less environment, which makes non-Node/NPM plugin scenarios harder to use.
So as to:
I think we should caution people against making "Node-side" or "browser-side" plugins as much as possible. Or at least thinking if such a thing is absolutely necessary. Rather than thinking of Less as a Node-based system that you add to with NPM-hosted plugins, it might be easier for some users to treat Less plugins like jQuery ones, where the plugins travel with the associated Less project, not that get installed somewhere "in-system", as those have dependencies which are not portable. Less plugins can easily exist as a single JS file which gets copied into your local Less source file directory.
But, the nice thing is that if we do things right, we don't have to adopt either "philosophy". Someone could choose to manage their Less plugins via NPM, and another person could copy and paste a file. If we write guidelines and examples appropriately, people can have the best of both worlds.
matthew-dean commentedon May 1, 2016
Just as an example as far as cross-platform plugin conventions, I've seen a library like Ractive (which can run in both browser and Node), basically do conventions as:
One thing that PostCSS does is it has a plugin generation script which creates your initial repo. So, we could do something similar where someone enters the name of their plugin, and it does the following:
dist/folder, an initial index.js file (for NPM/Node), and both build and test scripts for the plugin@plugin.@pluginseven-phases-max commentedon May 1, 2016
This is probably better to be done with something like
Yeoman(though personally I'm not very familiar with it since it was broken for my dev-platform for a while).matthew-dean commentedon May 1, 2016
Yeoman may be what was/is used, in fact. So, yep, something along those lines.
rjgotten commentedon May 2, 2016
Actually; I wrote
@pluginthe way I wrote it, to transparantly support plugins being included via transitive closure, i.e. , as dependencies of dependencies, without bothering the consumer (developer) of the Less file that imports the first dependency.It was meant as a zero-setup, collision-free means to include custom functions for Less frameworks.
The zero-setup aspect makes it work transparantly with any side-by-side editor that calls out to Lessc to create CSS previews, without having to mess with custom settings. That's an additional perk and may be a good reason to also develop your own project-specific functions in the
@pluginformat.Imports may use URLs, so why not
@plugin "node://clean-css"to instruct the compiler to use the NodeJS module lookup rules.seven-phases-max commentedon May 2, 2016
same mistake as:
Notice there's no mention of
nodeornpmin the initial post.is fine to load PHP-written (optionally even particular platform-specific, e.g. Wordpress)
less-plugin-clean-cssinless.phpfor example.This is illusion (no offense meant :) - it stops to be true as soon as you start to share the same plugin in different projects (so you either need a copy of the plugin in each or setup the shared paths, thus -> no zero-setup) or start to use different libraries that include different versions of the same plugin (so sooner or later someone comes with an idea of 'Less plugin manager/packager' utility).
So in general it's all the same problems whatever approach you choose.
matthew-dean commentedon May 2, 2016
Different versions of the same plugin are only an issue if you use NPM to manage your plugins. NPM is famously terrible and being able to install multiple versions to use simultaneously.
The way @rjgotten wrote it seems like it would work fine in that scenario, since functions are scoped, and the included JS file doesn't contain any sort of unique identifier that can collide if you have multiple copies. Therefore, you should be able to do:
Standard/current format plugins modify Less at the core/root level, and so are inherently prone to collisions in the way you describe. Currently
@pluginfunctions are not. It's one of the reasons that I feel that the copy/paste of a distribution file is actually the more appropriate recommended method. It can get included in the source files right where you need it.And then you have everything that "builds" your final CSS file all in one place. It's not pulling your Less file from a local directory and then a JS file from some other place, even though they have similar URL paths.
The way I visualize this is like:
matthew-dean commentedon May 2, 2016
I also agree though that this is probably the wrong way to go:
Unless...... like the way that "less-plugin-" is magically prepended for lessc, we assign a meaning to the browser. (The browser knows to look in node_modules.)
That said, it's such a specific case that, again, the "node://" URL scheme should itself be supported by a plugin. With proper architecture and good documentation, we can keep the core light and support those kinds of customizations better.
I've said it before in other threads: there's no reason there can't be official plugins that pull in optional JS files. We have official platform extensions, so something that searches
node_modules/could be a supported plugin. A benefit to having official plugins is to help establish plugin convention.rjgotten commentedon May 2, 2016
Ah. You beat me to it. Yeah; that was going to be my argument as well.
Also, just for the record: Node.js module lookup is different from what Lessc is currently using. Lessc is using
requirein its plugin loader, alright, but it searches an opinionated number of folders in a non-standard order, rather than following the node module lookup algorithm. (Look up the maddening details of the lookup algorithm at your own risk.So it should be called something different from
node://anyway.seven-phases-max commentedon May 2, 2016
Well, it's not hard to guess what is the key differences between our visions. Some treat plugins as a method to extend a particular project, others think of it as an extension to the language/compiler to meet that project needs (hence first are biased towards local copy-pasted js files, second favor shared libs). And there's no way to prove anything but by practice itself.
Yep (for me there's no way to think of
lists,colorsorfunctionsplugins as a part of a local project).31 remaining items
matthew-dean commentedon Jul 2, 2016
Soooooo........ @rjgotten @seven-phases-max
Here's a little progress update.
TL;DR: I figured out that the two formats actually needed to be unified after all, and went ahead and did the work.
Detailed Explanation
I started work on updating
@pluginto@use, and then started to test it with a project. If you recall a while ago, I submitted a PR proposal for interpreting custom at-rules as functions. @seven-phases-max's suggestion was that this was handled by a plugin that added the appropriate visitors. This was fine by me, so I started developing this with the inline syntax.However.... I began to run into some problems. The right vars / objects weren't exposed to the inline scripts, so those pieces from the PluginManager needed to be exposed. Then, I realized there were some use cases that couldn't really be handled by a script running once and then exiting. The
@plugin/@usestatements needed to support re-use. In other words, they needed to expose functions on a plugin object. You know, functions likeinstall()and possibly a few others. (Meaning these "extensions" were beginning to look pretty much identical to "plugins".)Meanwhile, I came across the plugin loader used by lessc. It handles retrieving a plugin by module name, and does a lot of convenient things like checking the version #, trying to prepend "less-plugin-" if no match is found, and if a plugin returns a function instead of an object, it attempts to create a new object out of it, which the Less.js API itself doesn't do, but should.
All of that led me to the inevitable conclusion that @seven-phases-max's instincts were right all along, that really, the historical "function script"
@pluginformat needed to align with documented plugin syntax, rather than creating another "format" which is actually not distinct enough to make sense.Once I had the idea that the two formats might need to be unified after all, I obsessively programmed to see if this was a possibility, and eventually I had a branch with these changes.
a. Evaluating less Node plugins within the function context @rjgotten created for
@plugin.b. Adding either file type to the plugin manager
c. Allowing an evaluated plugin file to either assign to
module.exports = { ... }orreturn { ... }to return an object with functions. (Or returning a function which, when invoked, returns an object.)d. Conditionally evaluating the
install()method, only if it exists.e. Using the plugin loader method for lessc as a "platform-specific" loader for
@pluginimports.f. Adding a "local path" option to that loader, so that the plugin loader for Node will also look locally (local to the
@pluginreference), if@pluginis what invoked it.@pluginwas invoked within a ruleset. (I noticed while testing that visitors added "late" would never be visited.) This still makes visitors global, but I'm not sure if that's an issue.Based on initial testing, all of this works. I was able to do this:
Note that the behavior of Node module loading is handled by the less-node plugin manager. I still need to test the less-browser plugin loader, which would only support local files. (Anything else would be too expensive in the browser, and it doesn't make sense to support Node in browser mode.)
Final Thoughts:
After seeing that all could be made possible under
@plugin, I realized there was really no need to have to explicitly load a plugin under any circumstance (via passing to Less options). You don't have torequire()a plugin and pass an object.@pluginis a cross-platform declaration, so it makes things a ton easier when explaining it.I still have some more work to do to:
3.x.I added these changes to the
feature/@pluginbranch and started a new3.xbranch, but, as I don't think there are any breaking changes, it could come in before 3.x.Feedback welcome.
seven-phases-max commentedon Jul 2, 2016
👍
Btw., indeed re-reading posts above I can see now that probably we digged too deep into "where plugin is loaded from" thing, which is in general quite subtle issue and won't be worth that possible overengineered
@plugin/@usestuff while not really targeting the main thing (i.e. let the Less be as extensible as possible in an as easy as possible fashion).matthew-dean commentedon Jul 2, 2016
@seven-phases-max Yes, I realized that too. It's actually quite a minor point. On Node the file searching order is not that relevant and really not that expensive. And I think I got stuck on having similar behavior on two different platforms, which is also not necessary. The compiling environments are very different, and it's easy to say, "On Node, Less will search node_modules first. In the browser, it won't." My plan is to have the browser loader do normal
@importbehavior, with the caveat of trying to prepend "less-plugin-" as a second try. With the exception that having ".js" just looks locally, as you first proposed. Basically, you had it right the first time, lol.Once I got into the codebase and started digging/coding around plugins, the way you had described it made more sense. The tricky part was to fold in existing
@pluginbehavior, which we didn't necessarily have to do, but I liked how much it simplified things the way that @rjgotten had done it.Meaning: as I currently have it written, a plugin doesn't have to export anything. It can add one function when it's first evaluated and be done (since the "functions" registry is now passed into the scope, as long as
@pluginis the caller). So it makes the plugin "signature" optional (albeit recommended).I have to do some work yet to fix
lesscto use the new plugin loader, and setting up the browser, but should be good to go before too long.If you have a chance to look and make any suggestions, please do.
rjgotten commentedon Jul 4, 2016
Sounds good. 👍
matthew-dean commentedon Jul 13, 2016
I'm recommending that we do a 3.0 release with these plugin changes sooner rather than later. I've disabled inline JavaScript along with the plugin changes because of a security report that was sent to me. Talk to me offline if you want more details.
matthew-dean commentedon Jan 7, 2017
Documentation well on its way! https://github.com/less/less-docs/blob/3.x/content/features/plugins.md
matthew-dean commentedon Jan 9, 2017
Update on
@pluginI realized in the process of documenting plugin usage (which I now realize is a good practice for figuring out if intended usage actually makes sense), that there were problems with a few specifics in the "format" of the file.
Namely: having globals like
lessandfunctionsavailable, or ending with a return statement created add-on problems, like:<script>tag as is currently documented for pre-loading plugins.So, what I've decided to do (but let me know if there's any opposition) is to recommend in the documentation specific boilerplate code that's essentially UMD format. (See the recently updated: https://github.com/less/less-docs/blob/3.x/content/features/plugins.md)
I can (and probably will) still leave
less,tree, andfunctionsavailable as globals for any legacy@pluginv2.5.0 files that may exist, although there shouldn't be a lot due to lack of documentation. These globals will be replaced by local vars of the same name (and the same objects) when using the boilerplate UMD code (except fortree, but a reference to tree is no longer necessary), so it shouldn't be a problem.Once again, if anyone objects, let me know, but the boilerplate code should allow plugins to be loaded in Node.js, either via
require()or vialesscor via@plugin, as well as loading in the browser via@pluginor in a<script>tag.matthew-dean commentedon Jan 9, 2017
What would help me is if someone could get repos set up with cross-platform UMD plugins, to replace:
I'm not sure of a use case for a cross-platform file-manager. A plugin that connected to the Dropbox API? Seems silly in the browser. But the point is just to get a few plugin examples that are universal, so the use case doesn't matter that much.
If someone can also come up with a repo layout like:
That keeps plugins simple & flat, and can keep source files organized while outputting single-file builds.
rjgotten commentedon Jan 9, 2017
@matthew-dean
I'm all for the UMD pattern. Makes everything nice and explicit. However, I'd rename
registerPluginin your documentation to a more simplefactory. That particular function doesn't actually perform any registering of the plugin, it only creates the plugin object. Less itself does the actual registering by calling theinstallmember.It's kind of sad that the browser-globals based version bloats the UMD config because of a need to detect prior existence of the global
lessobject and theless.pluginsarray configuration. I wonder if you could shorten it. Possibly something like:Or maybe just forego the two-tier namespace altogether and just use a global
less_pluginsarray that Less can detect in addition to the properly namespacedless.plugins. That would allow you to turn it into a one-liner, such as:Also, my gut tells me the
installmethod is better with the parameter order altered to placepluginManagerlast. Thelessandfunctionsmembers are probably going to be the most used, which will allow plugin authors to omit the more complexpluginManagerfrom implementations that don't need it.In total:
matthew-dean commentedon Jan 9, 2017
Ideally, yes, but that would break existing plugins. The function registry is something I added, otherwise there's no "local" function registry passed to install, as it was previously just a global var.
Renaming to
factory()is fine. It's an arbitrary name.Yes, you are right that I was trying to make it so Less config could appear before or after plugin
<script>tags, if someone was including a plugin that way. In fact, I was hoping to simplify the documentation so that adding a plugin didn't mean creating a Less object at all (if you weren't adding any other configuration).So, yeah, I could make it another global to shorten it, but I would capitalize
LESS_PLUGINSfor an internally-used global. That's just the naming convention I've used.Update: actually, you're right on another count. It does need to be a second global, because otherwise you'll have people do this:
matthew-dean commentedon Jan 9, 2017
Also, to be more accurate,
functionsis really (local)functionRegistry, but I did that for backwards-compatibility / simplicity.matthew-dean commentedon Jan 18, 2017
Docs updated: http://lesscss.org/3.x/features/#plugin-atrules-feature
matthew-dean commentedon Jun 11, 2018
Should this discussion be closed for cleanup?
seven-phases-max commentedon Jun 12, 2018
Closing as implemented in v3 (for various feature spin-offs mentioned during the discussion please create dedicated tickets).