-
-
Notifications
You must be signed in to change notification settings - Fork 158
feat(index): add option to disable base64
encoding (options.base64
)
#28
feat(index): add option to disable base64
encoding (options.base64
)
#28
Conversation
It seems to me that |
@brettvitaz I wasn't a huge fan of that syntax either, but my intention was actually to follow convention. Webpack's loader-utils says that this is how a flag is toggled using query parameters (see the And RFC2397 really treats this as a flag, which is why I went in that direction. I think adding additional encodings to the spec is highly unlikely. And I didn't want to add needless complications to the code. If instead we used a parameter that takes a variable value, then there would need to be some additional error checking done. What should be done if an incorrect or blank value is provided? Fallback to the default? Throw an error? Do we ignore case? So it seemed to me best to just go with a toggle and avoid that -- there's no real upside to it, as far as I could tell. But if the consensus is that this should be a more explicit parameter, I'm happy to implement it that way. |
Do you know if it's possible to automatically detect base64? That would be better than manually having to specify it. I haven't properly looked into this issue yet so don't know if it's possible for sure. Btw, in a few weeks I'm going to spend some time on file-loader and url-loader, and I'll come back to review this PR :). |
@SpaceK33z I suspect it may be possible to analyze the buffer and determine if every octet is an ASCII character, and if so then don't bother using base64 encoding. I'm not sure how reliable that would be. |
In any case you need to sign the CLA by closing and reopening the PR to trigger the CLA Bot again :) |
@michael-ciniawsky Yeah not a problem -- I'll keep en eye on #70 and submit a new PR when that's merged, if that seems reasonable? Should I close this PR then? |
base64
encoding
@michael-ciniawsky It's been a while so I'm trying to refresh my memory about this, so hopefully I'm not missing anything. Here's the relevant part from the RFC regarding data URLs:
So given that, my takeaway was/is:
Also I noticed in #70 that the conditional addition of the semicolon is incorrect. In the code it's included only if there is a mime type value. But it should actually be dependent on whether the
And the "base64" string would actually be interpreted as the mime type. It should actually be:
Again, I'm basing this entirely on my interpretation of the RFC. As far as I can tell 2397 is the relevant one, but they can get a bit confusing so perhaps there's a different spec we should be following? If you want me to comment on #70 instead, let me know. Or if you want me to do some work on this, I'm happy to. |
@joekrill first, sry for the delay :) && thx for the explanation
{
loader: 'url-loader'
options: {
encoding: false // (default: true) {Boolean}
}
} ? |
@joekrill resurrecting this one from the dead as the repo is getting some love. Are you interested in brining this PR up to date with the current codebase and continuing review? |
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.
It would be great if the option could be set from options
. I think this change needs a test. Could you add one please?
base64
encodingbase64
encoding (options.base64
)
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.
This needs to be implemented as a loader option instead of a sole query param
This PR is definitely stalled. I'm going to close it (temporarily) within the next ~48-72 hours due to that fact. Feel free to reopen at any time |
Sorry everyone, I'm trying to get myself back up to speed on this and the changes to Webpack loaders since I first made this PR. But it looks like it should be pretty straightforward to update. Let me see if I can get this updated quickly. |
@michael-ciniawsky @ooflorent rebased and updated this as requested. |
index.js
Outdated
@@ -0,0 +1,23 @@ | |||
/* |
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.
Bad rebase as it seems :) This file should be obsolete
|
||
const data = base64 | ||
? src.toString('base64') | ||
: escape(src.toString('binary')); |
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.
Why 'binary'
(latin1) here? Wouldn't 'utf8'
be 'better'?
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.
'binary'
is required for this to work with binary images (PNG, JPEG, etc)... which, while a bit unusual, seems to be perfectly valid.
test/base64-option.test.js
Outdated
test('{Boolean} false', async () => { | ||
const config = { | ||
loader: { | ||
test: /\.png$/, |
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.
Please test with an actual .svg
file here. Create a base64
folder in test/fixtures
with the following contents (you can copy file.svg
=> base64/file.svg
)
fixtures
|– base64
||– file.svg
||– fixture.js // import svg from './file.svg' ...
|– ...
|–fixture.js
and
- const stats = await webpack('fixture.js', config);
+ const stats = await webpack('base64/fixtures.js', config);
* Add option.base64 toggle * Update the README.md to reflect the new option
aba11da
to
d37b108
Compare
Closing due to inactivity. Feel free to reopen new PR. Thanks! |
Fixes an additional bug where the semicolon was only added when amatching mime type was found -- but the semicolon should always be
present when base64 encoding is used, regardless of whether a mime
type is specified (see https://tools.ietf.org/html/rfc2397)
resolves #25