Disclaimer: the post is now quite old. Vite has a dedicated react plugin which you can easily configure now to get the intended outcome. The comment section has better snippets to help you :)
Vite is the next cool thing everyone's talking about. For the uninitiated, it's a tool that provides a dev server with a blazingly fast refresh(HMR) speed and comes loaded with the Rollup module bundler for generating highly optimized builds.
Most of you would already be familiar with EmotionJs - a very popular CSS-in-JS library. It comes with a react flavor which provides a css
prop that greatly enhances the overall developer experience of writing styles in your react component.
An example from the official docs
However, every time we need to use this very convenient CSS prop, we would need to add emotion's custom JSX pragma on the very top of our jsx
component.
/** @jsx jsx */
import { jsx } from '@emotion/react'
If you are planning to give Vite a shot - The good news is that you don't need to do any additional tinkering. Emotion will work without any break when using the above approach. But, there is a far better way, with which we can simply avoid importing this chunk of import in all our JSX files.
To do that - you'd need to update the esbuild
options in your project's vite.config
file.
import { defineConfig } from 'vite';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [...],
esbuild: {
jsxFactory: `jsx`,
jsxInject: `import { jsx } from '@emotion/react'`,
}
...
});
Vite uses esbuild under the hood for compilation.
jsxInject
simply set esbuild's--inject
transformation option and auto imports the provided module in all.jsx
files.jsxFactory
overrides the defaultReact.creatElement
with emotions
jsx` factory function.
And, that's it. You can now use emotion in all your jsx components by default.
If you want you can also import the css
function along with jsx
to avoid it importing later in your components to construct serialized style objects.
jsxInject: `import {jsx, css} from '@emotion/react'`
Top comments (10)
I got it working with as little as:
Yes, the post is quite old now. I wrote it when Vite was just released :)
Thanks for helping other wandering developers with your comment.
I got emotion to work only by specifying the babel plugin on vite react plugin config as per this article
Without it, the css was not working and was seend as
[[Object, Object]]
in the dom.I'm trying to use component selectors in my emotion styles. Did you have any luck doing so?
For example, I have this
OnOffSwitch
component which has some child elements:These styles (anything below
display: inline-block;
) are being ignored now.I've found this, but there's no mention of how to achieve this with Vite: github.com/emotion-js/emotion/issu...
Also this, which throws an error immediately ("Uncaught SyntaxError: missing ) after argument list"):
stackoverflow.com/questions/614352...
Nice post! How would this work with Typescript? With your solution TS complains that
css
does not exist on that type...I faced the same issue, adding this to tsconfig.json fixed it for me:
Thanks for the tips!
It would help if the code was added using code block rather than an image. People with disabilities might not be able to read the code from the image and it's harder to copy-paste it. :)
And this code is definitely something I'm looking forward to copy-paste rather than try to type in myself (I don't see much benefit from typing and/or memorizing this)
Thanks for the suggestion Viktor. Edited my post.
This wasn't working for me (though I'm using the react-ts template). The linked article below worked immediately.
dev.to/glocore/configure-emotion-w...
awesome! was just messing with this today