减少在下一个版本中的styled-component类名长度

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

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:

减少在下一个版本中的styled-component类名长度

When compiler.styledComponents is set:

减少在下一个版本中的styled-component类名长度

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:

减少在下一个版本中的styled-component类名长度

When compiler.styledComponents is set:

减少在下一个版本中的styled-component类名长度

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.

huangapple
  • 本文由 发表于 2023年4月17日 18:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034241.html
匿名

发表评论

匿名网友

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

确定