-
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
Add a data-uri function. #1086
Add a data-uri function. #1086
Conversation
as described in a comment on less#775
Hm. Is this possibly problematic because it requires node's |
it doesn't make much sense browser-side to me, so returning |
Cool, I'll try that. Do you have some kind of |
either that or typeof window are the way it is done at the moment. |
Possible enhancement... Could it default to trying to work out the mime type? If you only get one argument... |
Hm, what's the best way to work out the mimetype? File extension, maybe? That could be some pretty complicated logic... |
https://npmjs.org/package/mime should work |
The previous pull used file extension with one special case for jpg.. I Could use a node package, but would rather be cautious about adding a new |
and move require('fs') into the function, so other things still work in the browser.
I ended up adding the dependency for |
pulled into 1.4.0 Also fixed the browser to return |
Neat, thanks! |
There were some things missed in this pull request and problems introduced with this code which need to be addressed. a) Less.js already has properties set up which return whether or not the parser is set to a browser, Node, or Rhino environment. However, this code ignores all of this and attempts to again detect browser support, even though browser support has already been detected in a way that can be overridden. So, this code breaks configurability. b) Up until this pull request, file operations were restricted to @import statements. The importer in Less.js is designed so that a JavaScript author can adapt the plugin to their environment by overriding the less.Parser.importer function in order to handle file operations. This code breaks that modularity as well by assuming and only supporting Node-based operations, which means that file operations are no longer supported consistently and universally. A JavaScript author can override and support the importer for file operations in unique environments, but not data-uri. (See Issue #1392) Neither of these things are well-documented, so it's understandable that they were missed, but they should be addressed moving forward (preferably in a bug-fix release). We're exploring making a more graceful environment plugin model that is more environment-agnostic, with a more clearly-defined API, so that we can write features like this without having to make explicit code paths for Node, Rhino and/or the browser. But for now, data-uri should be adjusted to match the current model for importers. |
Useful for adding in data uris from files.
Usage:
will render
If the mimetype has the
;base64
addition, the file will be base64 encoded.Enjoy!