这篇文章主要介绍了webpack项目轻松混用css module的方法,内容挺不错的,现在分享给大家,也给大家做个参考。
前言
本文讲述css-loader
开启css模块
功能之后,如何与引用的npm包中样式文件不产生冲突。
比如antd-mobilenpm
包的引入。在不做特殊处理的前提下,样式文件将会被转译成css module
。
一、产生问题的原因
{ test: /\.css$/, use: [ 'style-loader', { loader: 'css-loader', options: { modules: true, localIdentName: '[hash:base64:6]' } }, 'postcss-loader' ] }
以上代码片段,摘自webpack配置的module.rule。
可以看出wepack在编译过程中,遇到.css结尾的文件,都会交由postcss-loader、css-loader和style-loader依次处理。
因为css-loader开启了css模块功能,所以,所有经过处理的css文件,类名都将被改变。
二、初步改进
使用exclude和include进行区分
1.node_module文件夹内的文件,避免被当前rule处理
{ test: /\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader', options: { modules: true, localIdentName: '[hash:base64:6]' } }, { loader: 'postcss-loader' } ], exclude:[path.resolve(__dirname, '..', 'node_modules')] }
如上所示,将node_module文件夹内的文件,用exclude排除在外,不用当前rule进行处理。
2.单独处理node_module内的css文件
{ test: /\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader' }, { loader: 'postcss-loader' } ], include:[path.resolve(__dirname, '..', 'node_modules')] }
三、升级版,支持css module模式和普通模式混用
1.用文件名区分两种模式
*.global.css 普通模式
*.css css module模式
这里统一用 global 关键词进行识别。
2.用正则表达式匹配文件
// css module { test: new RegExp(`^(?!.*\\.global).*\\.css`), use: [ { loader: 'style-loader' }, { loader: 'css-loader', options: { modules: true, localIdentName: '[hash:base64:6]' } }, { loader: 'postcss-loader' } ], exclude:[path.resolve(__dirname, '..', 'node_modules')] } // 普通模式 { test: new RegExp(`^(.*\\.global).*\\.css`), use: [ { loader: 'style-loader' }, { loader: 'css-loader', }, { loader: 'postcss-loader' } ], exclude:[path.resolve(__dirname, '..', 'node_modules')] }
四、其他问题
less在使用css module时,对url的解析存在冲突,可以用resolve-url-loader进行解决,直接上代码:
test: /\.less/, use: [ { loader: "style-loader" }, { loader: "css-loader", options: { sourceMap: true, importLoaders: 2 } }, { loader: "postcss-loader", options: { sourceMap: true } }, { loader: "resolve-url-loader", options: { sourceMap: true } }, { loader: "less-loader", options: { sourceMap: true } } ]
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
关于CSS中权值、层叠和重要性(!important)的分析
关于CSS3中动画属性transform和transition以及animation属性的区别分析
The above is the detailed content of About the method of mixing css module in webpack project. For more information, please follow other related articles on the PHP Chinese website!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor
