-
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
Limit data-uri size to 32KB, falling back to relative url() #1190
Changes from 2 commits
d262e64
0fadc57
2d69b88
b51d0df
6624216
312e65a
f10f5c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -416,6 +416,24 @@ tree.functions = { | |
|
||
var buf = fs.readFileSync(filePath); | ||
|
||
// IE8 cannot handle a data-uri larger than 32KB. If this is exceeded | ||
// and the --ieCompat flag is enabled, return a normal url() instead. | ||
var DATA_URI_MAX_KB = 32, | ||
fileSizeInKB = parseInt((buf.length / 1024), 10); | ||
if (fileSizeInKB >= DATA_URI_MAX_KB) { | ||
// the url() must be relative, not an absolute file path | ||
filePath = path.relative(this.currentDirectory, filePath); | ||
|
||
if (this.env.ieCompat !== false) { | ||
// TODO: respect verbose or silent flags here | ||
console.error("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB); | ||
return new(tree.URL)(new(tree.Anonymous)(filePath)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use tree.Quoted instead of tree.Anonymous then the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, I'll push that shortly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the other hand, quoting isn't necessary inside |
||
} else { | ||
// if explicitly disabled (via --no-ie-compat on CLI, or env.ieCompat === false), merely warn | ||
console.warn("WARNING: Embedding %s (%dKB) exceeds IE8's data-uri size limit of %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB); | ||
} | ||
} | ||
|
||
buf = useBase64 ? buf.toString('base64') | ||
: encodeURIComponent(buf); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure about console.error - is this an error or a warning or just info?
whats our plan with supporting verbose/silent flags? Guess they just need putting in the env from the options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semantically, you're right, this is not strictly an error. A warning is more appropriate here. However, in
node
,console.warn
andconsole.error
both emit tostderr
, so the difference is academic.I tried to think of a different place to put the verbose and silent flags, but nothing makes as much sense as the env object (I get nervous about bloating it too much, but then realize it's just a lightweight object with a series of properties...). I like the approach of a utility logging function, similar to
less.writeError
inlib/less/index.js
. It would allow a central point of config evaluation, formatting, etc. However, that pattern would involve a lot more monkeying around with scope and whatnot, sincefunctions.js
does not have access to theless
object, nor is there currently a concept of "global" config flags (instead, they're copied and passed around).For now, adding
verbose
andsilent
to theenv
flags will do. I'll patch it later today.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
long term I would probably add a log function to env - that's why I made it an object is to provide the glue methods that need to be done in multiple places.. but I don't mind if you just add those 2 properties for now.