aelbore / esbuild-jest

A Jest transformer using esbuild
519 stars 51 forks source link

Auto-import React for JSX #65

Open AleksandrHovhannisyan opened 2 years ago

AleksandrHovhannisyan commented 2 years ago

esbuild supports options that allow you to automatically import React for JSX files so that you don't have to do import React from 'react' in every file. Is there currently a way to do that with this plugin, or will I need to manually import React?

httpete commented 2 years ago

I am hitting exactly this, if I had access to the inject: property from esbuild I would be totally set.

kelly-tock commented 2 years ago

am I reading the esbuild docs correctly, in the transform api you can't inject?

https://esbuild.github.io/api/#transform-api

jereklas commented 2 years ago

I think this could be resolved fairly easily if the internals of the library used build instead of transform. Is anyone even actively maintaining this library anymore?

kelly-tock commented 2 years ago

I don't think you'd want to use build actually, as jest transforms it in memory and executes it, which I think would be faster than emitting actual files. I got around this issue in a bit of a weird way, in my setup files script for jest I set react to a global variable, so

global.react = require('react');

the rest of my issues are things specific to esbuild and mocks.

jereklas commented 2 years ago

There's a write option on the build function that keeps everything in memory and doesn't write to file.

kelly-tock commented 2 years ago

ah, then that does sound good.

daniloab commented 2 years ago

related https://github.com/aelbore/esbuild-jest/issues/70