Yur*_*chi 9 swc reactjs babeljs styled-components next.js
当我尝试使用样式组件运行 Nextjs 项目时,我遇到了此错误。
这是我的 .babelrc
{
"plugins": [
[
"babel-plugin-styled-components",
{
"ssr": true,
"displayName": true
}
]
],
"presets": ["next/babel", "@babel/preset-typescript"]
}
Run Code Online (Sandbox Code Playgroud)
这是我的 next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
styledComponents:
Boolean |
{
displayName: Boolean | undefined,
ssr: Boolean | undefined
}
}
module.exports = nextConfig
Run Code Online (Sandbox Code Playgroud)
那么这是我的错误:语法错误:“next/font”需要 SWC,尽管由于存在自定义 babel 配置而正在使用 Babel。了解更多: https: //nextjs.org/docs/messages/babel-font-loader-conflict
我尝试在 next.config.js 中使用自定义 babel 配置,但没有按预期工作。
而且,这是我的_Document:
import Document, {
Html,
Head,
Main,
NextScript,
DocumentContext
} from 'next/document'
import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />)
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
}
} finally {
sheet.seal()
}
}
render() {
return (
<Html lang="pt-BR">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11123 次 |
最近记录: |