-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Importing Less files via plugins #2402
Comments
Can you clarify what you mean by "Less files" and "everything else"? |
I have a Less mixin library est, and I'd like to make a plugin version so that users won't need to explicitly do For instance, it provides a mixin .a {
.clearfix();
} Or add a simple And when build with the corresponding plugin, it will be compiled correctly. lessc style.less --est If I use the programatic way I can append library file before actual input but I'd like In Stylus it is possible to simply use So I'm wondering if you guys would have some idea about this. |
So that in addition to installing and updating a library I will have to install and update a corresponding plugin? (It's not that I'm strictly against this purpose - I guess it may have some uses in certain cases) - but in this particular case: why don't you just rename your
In your example it looks like you're just going to abuse the fact that plugins are installed into a predefined directory. Related to #1241. |
Thanks for ur reply Max. What I'm trying to do is make a mixin library both importable from Less include path and can be injected by a plugin (like #1241). It may have Less mixin files in I found that the plugin less-plugin-npm-import seem to be doing a similar job except it uses a In my particular case, maybe I can implement a similar file manager but explicit |
you can use visitors to alter the ast before eval - but thats after the import phase. We could consider making a new plugin type but you would have to pr it - it would be as above but encapsulated. If you are interested i can give feedback. |
Sounds like it may also be related to the proposed |
@lukeapage it seems that plugins can't access render options? So maybe it's not easy to implement using a visitor right now I think. |
plugins can access their own options - you can only access render options by getting into the render phase by creating a node that wraps another and intercepts generateCSS |
@lukeapage I'm trying to parse the library code into a tree and prepend its rules to the root ruleset of user AST. How can I access context and options inside a visitor? They seem to be necessary for |
Can't plugin just concatenate/inject anything it needs to the compiled source string (before actual parsing)? (That is, instead of making either programmatic imports or merging parsed trees we can just inject the list of imports as plain text string (of valid Less code)). I realize that currently there's no dedicated function or any "official" interface for this (so it's a room for some hack) but would not this be just more simple and more generic? (i.e. the main point: "imports" vs. "anything"). |
@seven-phases-max yes it's a lot easier but it seem to be impossible to do that without touching Less source code. The input string is not exposed for any change except for global variables and banner actually. Prepending rules to the AST is the only way to inject library for now I think. I agree it's not a elegant way and I don't know how to access parsing context inside a visitor. |
IIRC that shouldn't be impossible, because even in the examples on lesscss.org, it demonstrates rendering a LESS string into CSS output, so that string could in fact be a modified (prepended) source file, rather than a file reference. You should be able to either render a string or render a file. |
@matthew-dean I can't do that because |
Hmmm.... but... same idea, if your plugin added a custom file manager, then upon the first request, could you not return the prepended text + file? Not a perfect solution, but still seems un-impossible. |
File manager only works when we have |
We have post process plugins, it would be trivial to add a pre-process |
Just created a PR. Plugin code looks like this: Injector.prototype.process = function (src, extra) {
var injected = '@import "all.less";\n';
var ignored = extra.imports.contentsIgnoredChars;
var fileInfo = extra.fileInfo;
ignored[fileInfo.filename] = ignored[fileInfo.filename] || 0;
ignored[fileInfo.filename] += injected.length;
extra.context.paths.push(path.resolve(__dirname, '../src'));
return injected + src;
}; Don't know if this is a proper way to inject library. |
Hi,
Is there a way for Less plugins to import Less files before everything else while compilng?
Stylus seems to allow this kind of implicity import for a library.
The text was updated successfully, but these errors were encountered: