Team A 和 Team B 导入的类污染了同一个全局命名空间。

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

Classes brought from Team A and Team B polluting the same global namespace

问题

在这种情况下,如何解决这个问题?

英文:

I am new to Tailwind CSS but there is something that confuses me.

Let's run a scenario:

Team A

  • builds a UI component library using TailwindCSS (Button, Forms, Dialog, etc)
  • publish NPM package where exports index.ts and theme/tailwind.css

Team B

  • npm install Team A library. Imports theme/tailwind.css into their application main.ts (entry point).

At this point, their main.ts should have

import { Button } from '@team-a/ui`
import '@team-a/ui/theme/tailwind.css` // tailwind classes coming from Team A
import `./theme/main.css` // tailwind CSS global file belonging to Team B

At this point in time, in the <style> tags in the head, we will have classes brought from Team A and Team B, polluting the same global namespace.

How do you get around this issue?

答案1

得分: 1

以下是翻译好的部分:

  1. 使用@layers
  2. 将CSS的作用范围限定为仅适用于特定的标签及其子元素。通常,框架会处理这一点,比如React使用CSS模块,Angular使用视图封装,但也可以使用PostCSS或Sass实现。
  3. 使用每个团队的独特字符串作为前缀,如果它们生成分离的Tailwind输出CSS。
英文:

There a few ways to avoid this, but if both teams uses tailwind there should not be any issue. unless you have custom CSS that for example overrides custom properties at the :root.

Regardless here a few ways to avoid clashing CSS styles.

  1. use @layers,
  2. Scope the CSS to apply only to a specific tag and below. This is a tricky one, usually frameworks will handle that, react with CSS modules, angular with view encapsulation, but it is possible with postcss or sass.
  3. Prefix the tailwind config with each team unique string, if they build separated tailwind output CSS.

huangapple
  • 本文由 发表于 2023年2月19日 16:48:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498963.html
匿名

发表评论

匿名网友

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

确定