react中antd+css Module一起使用

antd 和 css modules 不能混用,针对antd的css 单独写一条loader的规则,不开启 css modules。

使用 exclude 和 include 配置参考(https://segmentfault.com/q/1010000011223900),修改webpack.config.dev.js 和 webpack.config-prod.js 配置文件 (尽量不要使用less-loader 来处理css文件,在与antd一起使用时可能出现错误,单独写一条规则),所以我们只能两个来处理css的loader。

在react中使用antd+less+css modules参考(https://www.jianshu.com/p/51ff1c8be301)

// ant的按需加载
                    {
                        test: /\.css$/,
                        include: /node_modules/,
                        use: [
                            require.resolve(‘style-loader‘),
                            {
                                loader: require.resolve(‘css-loader‘),
                                options: {
                                    importLoaders: 1,
                                },
                            },
                            {
                                loader: require.resolve(‘postcss-loader‘),
                                options: {
                                    // Necessary for external CSS imports to work
                                    // https://github.com/facebookincubator/create-react-app/issues/2677
                                    ident: ‘postcss‘,
                                    plugins: () => [
                                        require(‘postcss-flexbugs-fixes‘),
                                        autoprefixer({
                                            browsers: [
                                                ‘>1%‘,
                                                ‘last 4 versions‘,
                                                ‘Firefox ESR‘,
                                                ‘not ie < 9‘, // React doesn‘t support IE8 anyway
                                            ],
                                            flexbox: ‘no-2009‘,
                                        }),
                                    ],
                                },
                            },
                        ],
                    },
// 正常的网页中的css
                    {
                        test: /\.css$/,
                        exclude: /node_modules/,
                        use: [
                            require.resolve(‘style-loader‘),
                            {
                                loader: require.resolve(‘css-loader‘),
                                options: {
                                    importLoaders: 1,
                                    modules: true,
                                    localIdentName: "[name]__[local]___[hash:base64:5]",

                                },
                            },
                            {
                                loader: require.resolve(‘postcss-loader‘),
                                options: {
                                    // Necessary for external CSS imports to work
                                    // https://github.com/facebookincubator/create-react-app/issues/2677
                                    ident: ‘postcss‘,
                                    plugins: () => [
                                        require(‘postcss-flexbugs-fixes‘),
                                        autoprefixer({
                                            browsers: [
                                                ‘>1%‘,
                                                ‘last 4 versions‘,
                                                ‘Firefox ESR‘,
                                                ‘not ie < 9‘, // React doesn‘t support IE8 anyway
                                            ],
                                            flexbox: ‘no-2009‘,
                                        }),
                                    ],
                                },
                            },
                        ],
                    },

原文地址:https://www.cnblogs.com/lanshu123/p/10660707.html

时间: 2024-08-25 04:22:12

react中antd+css Module一起使用的相关文章

React中使用CSS

第一种: 在组件中直接使用style 不需要组件从外部引入css文件,直接在组件中书写. import React, { Component } from "react"; const div1 = { width: "300px", margin: "30px auto", backgroundColor: "#44014C", //驼峰法 minHeight: "200px", boxSizing: &

React 中使用CSS的方法

不需要组件从外部引入css文件,直接在组件中书写. import React, { Component } from "react";const div1 = { width: "300px", margin: "30px auto", backgroundColor: "#44014C", //驼峰法 minHeight: "200px", boxSizing: "border-box"

react里面使用css module方式

我使用的是create-react-app创建的项目,用的webpack2,在里面更改配置 { test: /\.css$/, use: [ require.resolve("style-loader"), { loader: require.resolve("css-loader"), options: { importLoaders: 1, modules: true, localIdentName: "[path][name]__[local]--[

[React] {svg, css module, sass} support in Create React App 2.0

create-react-app version 2.0 added a lot of new features. One of the new features is added the svgr webpack loader to wrap SVGs in React components as a named export. This let’s you either grab the filename from the default export or grab a wrapped S

React中css的使用

网页的布局.颜色.形状等UI展示方式主要是由Css进行设置,在ReactJs中也是一样.ReactJs中的Css结构方式与传统的Web网页类似,但依然存在一些差异.ReactJs中Css文件本身的编写上并没有差异,我们可以定义特定的样式类名,也可以正对Html元素定义样式.差异主要在React对Css的使用上 引用的差异: 传统html文件中css文件引用方式: <link rel="stylesheet" type="text/css" href="

React中使用Ant Table组件

一.Ant Design of React http://ant.design/docs/react/introduce 二.建立webpack工程 webpack+react demo下载 项目的启动,参考 三.简单配置 1.工程下载下来之后,在src目录下新建目录“table”,新建app.js,内容如下. import React from 'react'; import ReactDOM from 'react-dom'; import ExampleTable from './Exam

react+mobx+antd按需加载 出现Support for the experimental syntax &#39;decorators-legacy&#39; isn&#39;t currently enabled

baidu上面的说法大多是在 项目的package.json 中添加decorators-legacy 因为引入了antd的按需加载 所以只需要在config-overrides.js中添加addDecoratorsLegacy() const { override, fixBabelImports,addDecoratorsLegacy } = require('customize-cra'); module.exports = override( fixBabelImports('impor

React第六篇: 搭建React + Router + antd + nodejs + express框架搭建(nodejs做前后端server)

前提: nodejs >= 10.0;  这里不推荐用官网的yarn安装antd的模块,因为后续会出错,错误如图: 也不推荐用npx方法来搭建react骨架,也会出错,让我们开始吧!!   前端React+Antd框架搭建 1.安装并启动create-react-app骨架应用 打开cmd按顺序执行以下指令: npm install -g create-react-app   (全局安装create-react-app, 默认会安装在C盘个人用户下) create-react-app my-ap

react&amp;webpack使用css、less &amp;&amp; 安装原则 --- 从根本上解决问题。

在webpack-react项目中,css的使用对于不同人有不同的选择,早起是推荐在jsx文件中使用 css inline js的,但是这种方法要写很多对象来表示一个一个的标签,并且对于这些对象,我们在写样式时,还必须要使用驼峰式的写法,所以,这无疑使我们不能接受的,最好的做法就是讲css文件写在一个单独的文件夹中外联进来. webpack同时也是可以将css文件当做一个一个的模块的,所以,我们就需要对css模块的处理进行配置. npm install css-loader style-load