esbuild-jest icon indicating copy to clipboard operation
esbuild-jest copied to clipboard

Auto-import React for JSX

Open AleksandrHovhannisyan opened this issue 2 years ago • 7 comments

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?

AleksandrHovhannisyan avatar Jan 06 '22 23:01 AleksandrHovhannisyan

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

httpete avatar Jan 19 '22 16:01 httpete

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

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

kelly-tock avatar Jan 28 '22 18:01 kelly-tock

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?

jereklas avatar Jan 31 '22 06:01 jereklas

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.

kelly-tock avatar Jan 31 '22 12:01 kelly-tock

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

jereklas avatar Jan 31 '22 16:01 jereklas

ah, then that does sound good.

kelly-tock avatar Jan 31 '22 16:01 kelly-tock

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

daniloab avatar Mar 07 '22 19:03 daniloab