您好,欢迎访问一九零五行业门户网

聊聊css为什么需要模块化?怎么进行模块化?

你的 css 也需要模块化css “局部”样式sass、less 通过 @import ,部分解决的 css 模块化的问题。
由于 css 是全局的,在被引入的文件和当前文件出现重名的情况下,前者样式就会被后者覆盖。
在引入一些公用组件,或者多人协作开发同一页面的时候,就需要考虑样式会不会被覆盖,这很麻烦。
// file a.name {    color: red}// file b@import a.scss;.name {    color: green}
css 全局样式的特点,导致 css 难以维护,所以需要一种 css “局部”样式的解决方案。
也就是彻底的 css 模块化,@import 进来的 css 模块,需要隐藏自己的内部作用域。
css modules 原理通过在每个 class 名后带一个独一无二 hash 值,这样就不有存在全局命名冲突的问题了。这样就相当于伪造了“局部”样式。
// 原始样式 styles.css.title {  color: red;}// 原始模板 demo.htmlimport styles from 'styles.css';<h1 class={styles.title}>  hello world</h1>// 编译后的 styles.css.title_3zyde {  color: red;}// 编译后的 demo.html<h1 class="title_3zyde">  hello world</h1>
webpack 与 css moduleswebpack 自带的 css-loader 组件,自带了 css modules,通过简单的配置即可使用。
{    test: /\.css$/,    loader: css?modules&localidentname=[name]__[local]--[hash:base64:5]}
命名规范是从 bem 扩展而来。
block: 对应模块名 [name]
element: 对应节点名 [local]
modifier: 对应节点状态 [hash:base64:5]
使用 __ 和 -- 是为了区块内单词的分割节点区分开来。
最终 class 名为 styles__title--3zyde。
在生产环境中使用在实际生产中,结合 sass 使用会更加便利。以下是结合 sass 使用的 webpack 的配置文件。
{    test: /\.scss$/,    loader: style!css?modules&importloaders=1&localidentname=[name]__[local]--[hash:base64:5]!sass?sourcemap=true&sourcemapcontents=true}
通常除了局部样式,还需要全局样式,比如 base.css 等基础文件。
将公用样式文件和组件样式文件分别放入到两个不同的目标下。如下。
.├── app                      │   ├── styles               # 公用样式│   │     ├── app.scss       │   │     └── base.scss      │   ││   └── components           # 组件          ├── component.jsx  # 组件模板          └── component.scss # 组件样式
然后通过 webpack 配置,将在 app/styles 文件夹的外的(exclude) scss 文件局部化。
{    test: /\.scss$/,    exclude: path.resolve(__dirname, 'app/styles'),    loader: style!css?modules&importloaders=1&localidentname=[name]__[local]--[hash:base64:5]!sass?sourcemap=true&sourcemapcontents=true},{    test: /\.scss$/,    include: path.resolve(__dirname, 'app/styles'),    loader: style!css?sass?sourcemap=true&sourcemapcontents=true}
有时候,一个元素有多个 class 名,可以通过  join( )  或字符串模版的方式来给元素添加多个 class 名。
// join-react.jsx<h1 classname={[styles.title,styles.bold].join(" ")}>  hello world</h1>// stringtemp-react.jsx<h1 classname={`${styles.title} ${styles.bold}`}>  hello world</h1>
如果只写一个 class 就能把样式定义好,那么最好把所有样式写在一个 class 中。
所以,如果我们使用了多个 class 定义样式,通常会带一些一些逻辑判断。这个时候写起来就会麻烦不少。
引入 classnames ,即可以解决给元素写多个 class 名的问题,也可以解决写逻辑判断的麻烦问题。
classnames('foo', 'bar'); // => 'foo bar'classnames('foo', { bar: true }); // => 'foo bar'classnames({ 'foo-bar': true }); // => 'foo-bar'classnames({ 'foo-bar': false }); // => ''classnames({ foo: true }, { bar: true }); // => 'foo bar'classnames({ foo: true, bar: true }); // => 'foo bar'// lots of arguments of various typesclassnames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'// other falsy values are just ignoredclassnames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
引入 css modules 的样式模块,每个 class 每次都要写 styles.xxx 也是很麻烦,在《深入react技术栈》提到了 react-css-modules 的库,来减少代码的书写,感兴趣的同学可以研究下。
推荐学习:《css视频教程》
以上就是聊聊css为什么需要模块化?怎么进行模块化?的详细内容。
其它类似信息

推荐信息