How can I use the css prop in @emotion/react without importing /** @jsxImportSource @emotion/react */ in every file?

huangapple go评论94阅读模式
英文:

How can I use the css prop in @emotion/react without importing /** @jsxImportSource @emotion/react */ in every file?

问题

I want to use the @emotion/react library so that I can use the css prop like in this example:

<div
    css={{
        backgroundColor: 'pink',
        height: '500px',
        width: '500px',
    }}
></div>

However, I do not want to import /** @jsxImportSource @emotion/react */ at the top of every file that is using the css prop.
Please provide me a step-by-step workaround to be able to have a running react project using the css prop without the strange import, using any of the following:
create-react-app, create-react-app + typescript, vite, vite + typescript

Thank you!

英文:

I want to use the @emotion/react library so that I can use the css prop like in this example:

			&lt;div
				css={{
					backgroundColor: &#39;pink&#39;,
					height: &quot;500px&quot;,
					width: &quot;500px,
				}}
			&gt;
			&lt;/div&gt;

However, I do not want to import /** @jsxImportSource @emotion/react */ at the top of every file that is using the css prop.
Please provide me a step by step work-around to be able to have a running react project using the css prop without the strange import, using any of the following:
create-react-app, create-react-app + typescript, vite, vite + typescript

Thanks you!

答案1

得分: 2

使用CRACO,我们可以覆盖CRA配置并包括babel插件/预设。

对于create-react-app:

npm i @emotion/react

npm i -D @emotion/babel-plugin @craco/craco

在项目根目录(与package.json旁边)创建一个名为craco.config.js的文件,包含以下代码:

module.exports = {
    babel: {
        presets: [
            [
                "@babel/preset-react",
                { "runtime": "automatic", "importSource": "@emotion/react" }
            ]
        ],
        plugins: ["@emotion/babel-plugin"]
    }
}

更新package.json中的脚本:

"scripts": {
-  "start": "react-scripts start",
+  "start": "craco start",
-  "build": "react-scripts build",
+  "build": "craco build",
-  "test": "react-scripts test",
+  "test": "craco test"
-  "eject": "react-scripts eject"
}

对于TypeScript CRA,请确保在compilerOptions中的tsconfig中包含以下内容:

"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"

现在测试你的代码,不需要导入应该可以正常工作。

<div
    css={{
        backgroundColor: 'pink',
        height: "500px",
        width: "500px",
        '&:hover': {
            backgroundColor: 'green'
        },
        '@media(max-width: 1000px)': {
            backgroundColor: 'blue'
        }
    }}
>
英文:

Using CRACO we can override CRA configuration and include babel plugins/presets.

For create-react-app:

npm i @emotion/react

npm i -D @emotion/babel-plugin @craco/craco

create file called craco.config.js at the root of the project (next to package.json), with the following code:

module.exports = {
    babel: {
        presets: [
            [
                &quot;@babel/preset-react&quot;,
                { &quot;runtime&quot;: &quot;automatic&quot;, &quot;importSource&quot;: &quot;@emotion/react&quot; }
            ]
        ],
        plugins: [&quot;@emotion/babel-plugin&quot;]
    }
}

update scripts in package.json:

&quot;scripts&quot;: {
-  &quot;start&quot;: &quot;react-scripts start&quot;,
+  &quot;start&quot;: &quot;craco start&quot;,
-  &quot;build&quot;: &quot;react-scripts build&quot;,
+  &quot;build&quot;: &quot;craco build&quot;,
-  &quot;test&quot;: &quot;react-scripts test&quot;,
+  &quot;test&quot;: &quot;craco test&quot;
-  &quot;eject&quot;: &quot;react-scripts eject&quot;
}

for typescript CRA, make sure to include this in tsconfig inside compilerOptions

&quot;jsx&quot;: &quot;react-jsx&quot;,
&quot;jsxImportSource&quot;: &quot;@emotion/react&quot;

Now test your code without imports and it should be working.

&lt;div
    css={{
        backgroundColor: &#39;pink&#39;,
        height: &quot;500px&quot;,
        width: &quot;500px&quot;,
        &#39;&amp;:hover&#39;: {
            backgroundColor: &#39;green&#39;
        },
        &#39;@media(max-width: 1000px)&#39;: {
            backgroundColor: &#39;blue&#39;
        }
    }}
&gt;

huangapple
  • 本文由 发表于 2023年6月1日 04:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定