Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Add unit test and linter #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add unit test and linter #4

wants to merge 4 commits into from

Conversation

fbaiodias
Copy link
Owner

Hey @jonathanperret and @nhardy,

While I was adding a unit test to this loader, doing buffer.toString() of some random content returned �����ݢg(�ק and buffer.toString('base64') returned somerandomconten which also seems incorrect.

As according to the webpack docs, "the loader is called with only one parameter -- a string containing the content of the resource file", I tried changing it to module.exports = Buffer.from("${content}").

Does this look good to you? Or am I missing something?

index.js Outdated

return 'module.exports = Buffer.from("' + content.toString('base64') + '", "base64")';
};
return `module.exports = Buffer.from("${content}")`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe content needs to be encoded as being binary data, it may contain characters which would break the naïve approach here, e.g. ", \n, \0, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Webpack docs seem to be wrong, what Webpack passes your loader is actually a Buffer object: this is why we can call .toString('base64') on it, for example, which wouldn't work with a string. In general, it wouldn't make sense to load an arbitrary file as a string so it has to work this way.

When you write ${content} though, you are asking this Buffer to convert itself to a string. Buffer is smart enough to escape control characters to escape sequences (e.g. \u0000) but as @nhardy says, it doesn't know that you are pasting that string between double quotes so it will not escape ", \ or \n, breaking the generated source. Also, Buffer will try to interpret the file's contents as UTF-8 and make a mess of things if the file is anything else.

What exactly is the problem you are encountering with the test?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah, the trick is that because you have module.exports.raw = true; in the loader, Webpack calls it with a Buffer and not a string. So the docs are probably accurate for the general case, but I can't find documentation for that raw flag.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, makes sense. Thank you!
I've pushed a new commit, please have a look again 🙏

index.spec.js Outdated

describe('buffer-loader', () => {
it('returns a buffer with the content', () => {
const content = 'some random content'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want this to reflect the way Webpack calls your loader, you would have to write this as:

const content = Buffer.from('some random content', 'utf-8');

"jest": {
"testEnvironment": "node"
},
"standard": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

standard is nice but semistandard is better 😉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants