Skip to content
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

Issue with binary loaders + workaround #92

Open
jameschao opened this issue Aug 14, 2023 · 0 comments
Open

Issue with binary loaders + workaround #92

jameschao opened this issue Aug 14, 2023 · 0 comments

Comments

@jameschao
Copy link

Hi, I found that esbuild-jest doesn't work with binary loaders, i.e., it results in incorrect data.

It turns out this, it's because rather than using the source code text that jest provides (which seems to have certain encoding), esbuild-jest should pass the actual file contents to esbuild.transformSync().

esbuild v0.14.52 added a fix for handling binary contents properly in transform, and this was referenced evanw/esbuild#2424 also.

I have a working solution, and tried to contribute back, but there were other package version changes, so I was afraid of breaking other things unintentionally. So, decided to raise an issue here instead.

Here's my workaround by adding a custom jest transformer:

const { transformSync } = require('esbuild');
const { readFileSync } = require('fs');

/**
 * This transformer is used to transform binary files using esbuild.
 *
 * This is needed because esbuild-jest currently does not handle binary files transformation properly.
 */
module.exports = {
    process(sourceText, sourcePath) {
        // read the file contents directly rather than using sourceText,
        // which is a string rather than the actual data
        const fileData = readFileSync(sourcePath);
        const { code } = transformSync(fileData, { loader: 'binary' });

        return {
            code,
        };
    },
};
@jameschao jameschao changed the title Not working for binary loaders Issue with binary loaders + workaround Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant