英文:
reduce styled-component className length in next
问题
When using styled-components
in Next with React, it can become challenging to ensure that the styled components render correctly. To address this issue, Next supplies the compiler.styledComponents
flag in next.config.js
as follows:
compiler: {
styledComponents: true
}
However, when this flag is enabled, it leads to the class names becoming unreadable as they increase in size exponentially.
The following images illustrate the difference in class names between when compiler.styledComponents
is disabled and when it is enabled.
When compiler.styledComponents
is not set:
When compiler.styledComponents
is set:
Is there a way to reduce the class name sizes to just their regular sc-XXXXXX
names?
I should note that we're not using the latest version of Next, but instead next@12.3.4
.
英文:
When using styled-components
in Next with React, it can become challenging to ensure that the styled components render correctly. To address this issue, Next supplies the compiler.styledComponents
flag
in next.config.js
as follows:
compiler: {
styledComponents: true
}
However, when this flag is enabled, it leads to the class names becoming unreadable as they increase in size exponentially.
The following images illustrate the difference in class names between when compiler.styledComponents
is disabled and when it is enabled.
When compiler.styledComponents
is not set:
When compiler.styledComponents
is set:
Is there a way to reduce the class name sizes to just their regular sc-XXXXXX
names?
I should note that we're not using the latest version of Next, but instead next@12.3.4
答案1
得分: 5
在 babel-plugin-styled-components
中禁用 displayName
感谢 @vlad-vladov 指出了这方面的文档。
在 Next.js 12 和 13 文档中关于 Next.js 编译器 中指出,Next.js 使用了 babel-plugin-styled-components
的一个版本,并且在开发环境中默认启用了 displayName
选项,而在生产环境中禁用了它。关于 babel-plugin-styled-components
的文档 中对 displayName
选项有以下说明:
> 此选项会增强每个组件附加的 CSS 类名称,以便在没有 React DevTools 的情况下更好地识别 DOM 中的组件。在您的页面源代码中,您将看到:<button class="Button-asdf123 asdf123" />
而不仅仅是 <button class="asdf123" />
。
要禁用 displayName
,只需将其设置为 false
:
module.exports = {
compiler: {
styledComponents: { displayName: false }
}
}
另外,为了进一步解释为什么您的类名如此之长,fileName
选项(默认启用)会在启用时将当前文件的名称添加到 displayName
的开头。
英文:
Disable displayName
in babel-plugin-styled-components
Thanks to @vlad-vladov for pointing out the docs for this.
In the Next.js 12 and 13 docs for the Next.js compiler, it is stated that Next.js is using a port of babel-plugin-styled-components
and that the displayName
option is enabled by default in development and disabled in production. The documentation for babel-plugin-styled-components
states the following about the displayName
option:
> This option enhances the attached CSS class name on each component with richer output to help identify your components in the DOM without React DevTools. In your page source you'll see: <button class="Button-asdf123 asdf123" />
instead of just <button class="asdf123" />
.
To disable displayName
, just put false
:
module.exports = {
compiler: {
styledComponents: { displayName: false }
}
}
And to further explain why your class names were so long, the fileName
option (enabled by default) adds the name of the current file to the beginning of the displayName
when it's enabled.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论