React18TS项目:配置react-css-modules,使用styleName

这篇具有很好参考价值的文章主要介绍了React18TS项目:配置react-css-modules,使用styleName。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

他的好处不说了

网上一堆文章一个能打的都没有,

添加开发依赖

pnpm add -D @dr.pogodin/babel-plugin-react-css-modules @types/react-css-modules  

Babel Plugin "React CSS Modules" | Dr. Pogodin Studio

看@dr.pogodin/babel-plugin-react-css-modules官方文档

不使用babel-plugin-react-css-modules

手搭webpack配置需要处理

1.能启用css modules

对于裸 Webpack,请参见webpack css-loader的 modules 的选项 链接。

2.配置@dr.pogodin/babel-plugin-react-css-modules

使用

  • 将此插件作为直接依赖项安装(在不允许编译时 styleName 解析的边缘情况下,插件会回退到运行时解析)。
npm install --save @dr.pogodin/babel-plugin-react-css-modules
  • Install Webpack at least as a dev dependency:
    至少将 Webpack 作为开发依赖项安装 Webpack:
npm install --save-dev webpack
  • Add the plugin to Babel configuration:
    将插件添加到 Babel 配置中:
{
  "plugins": [
    ["@dr.pogodin/react-css-modules", {
      // The default localIdentName in "css-loader" is "[hash:base64]",
      // it is highly-recommended to explicitly specify the same value
      // both here, and in "css-loader" options, including the hash length
      // (the last digit in the template below).
      "generateScopedName": "[hash:base64:6]"

      // See below for all valid options.
    }]
  ]
}

我自己项目手写的webpack.base.js中使用

const CSS_MODULE_LOCAL_IDENT_NAME = '[name]__[local]___[hash:base64:5]';


{
	test: /.(jsx?)|(tsx?)$/,
		exclude: /node_modules/,
		use: [
		{
			loader: 'babel-loader',
			options: {
				presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
				plugins: [
					[
						'@dr.pogodin/react-css-modules',
						{
							generateScopedName: CSS_MODULE_LOCAL_IDENT_NAME,
							autoResolveMultipleImports: true, // 允许多个匿名导入
							webpackHotModuleReloading: true, // 启用热模块重新加载代码的注入
							handleMissingStyleName: 'throw', // 确定应为未定义的 CSS 模块执行的操作
							filetypes: {
								'.less': {
									syntax: 'postcss-less',
								},
							},
						},
					],
				],
			},
		},
	],
},
{
	test: /\.less$/,
	use: [
		'style-loader',
		{
			loader: 'css-loader',
			options: {
				modules: {
					localIdentName: CSS_MODULE_LOCAL_IDENT_NAME,
					auto: resourcePath => resourcePath.endsWith('.module.less'),
				},
			},
		},
		{
			loader: 'postcss-loader',
			options: {
				postcssOptions: {
					plugins: [['postcss-preset-env', {}]],
				},
			},
		},
		{
			loader: 'less-loader',
			options: {
				lessOptions: {
					javascriptEnabled: true,
				},
			},
		},
	],
	// 排除 node_modules 目录
	exclude: /node_modules/,
},

3.TS项目不能直接在jsx中使用styleName,会报错,

需要引入@types/react-css-modules

就可以使用module.less和styleName了

项目中使用

index.module.less

.adminColor {
	color: aquamarine;
	background-color: black;
}

index.tsx

import React from 'react';
import './index.module.less';

const Dashboard: React.FC = () => {
	return (
		<>
			<h2 styleName="adminColor">我的</h2>
		<div styleName="adminColor">我的</div>
		</>
	);
};
export default Dashboard;

效果

React18TS项目:配置react-css-modules,使用styleName,React,webpack,react.js,css,前端

样式编码

React18TS项目:配置react-css-modules,使用styleName,React,webpack,react.js,css,前端文章来源地址https://www.toymoban.com/news/detail-660348.html

到了这里,关于React18TS项目:配置react-css-modules,使用styleName的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【React】TS项目配置Redux

    在React中使用Redux,官方要求安装两个插件, Redux Toolkit 和 react-redux Redux Toolkit (RTK): 官方推荐编写Redux逻辑的方式,是一套工具的集合集, 简化书写方式 。 简化 store 的配置方式 内置 immer 支持可变式状态修改 内置 thunk 更好的异步创建 react-redux :用来 链接 Redux 和 React组

    2024年01月22日
    浏览(29)
  • react+ts【项目实战一】配置项目/路由/redux

    1、该项目使用的是ts创建的 所以需要加上 --template typescript create-react-app kiki_ts_react_music --template typescript 2、 整理项目结构 删除一些自己用不到的文件 1.2.1 更换icon 1.2.2 更换项目名称 在index.html文件里面 1.2.1 配置项目别名 1、 npm i -D @craco/craco 2、在根文件创建 craco.config.ts 3、修

    2024年02月19日
    浏览(29)
  • 【实战】 五、CSS 其实很简单 - 用 CSS-in-JS 添加样式(上) —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(六)

    学习内容来源:React + React Hook + TS 最佳实践-慕课网 相对原教程,我在学习开始时(2023.03)采用的是当前最新版本: 项 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月12日
    浏览(33)
  • 【实战】 五、CSS 其实很简单 - 用 CSS-in-JS 添加样式(下) —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(七)

    学习内容来源:React + React Hook + TS 最佳实践-慕课网 相对原教程,我在学习开始时(2023.03)采用的是当前最新版本: 项 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月12日
    浏览(24)
  • React 配置别名 @ ( js/ts 项目中通过 @craco/craco 配置)

    在 Vue 项目当中,可以使用 @ 来表示 src/ ,但在 React 项目中,默认却没有该功能,因此需要进行手动的配置来实现该功能。 别名主要解决的问题:每个页面都使用路径的方式进行引入,这样很麻烦,效率很低,这个时候可以配置一个别名,来直接使用别名引入。 使用 @craco

    2024年02月10日
    浏览(30)
  • (react+ts)vite项目中的路径别名的配置

    找到vite.config.ts,这里会现实报错,需要安装一下 npm i -D @types/node 这个库的ts声明配置 虽然配置好了@但是输入@没有提示,找到tsconfig.json

    2024年02月08日
    浏览(26)
  • 【实战】 项目起航:项目初始化与配置 —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(一)

    学习内容来源:React + React Hook + TS 最佳实践-慕课网 相对原教程,我在学习开始时(2023.03)采用的是当前最新版本: 项 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月10日
    浏览(69)
  • React 配置别名 @ ( js/ts 项目中通过 webpack.config.js 配置)

    在 Vue 项目当中,可以使用  @  来表示  src/ ,但在 React 项目中,默认却没有该功能,因此需要进行手动的配置来实现该功能。 别名主要解决的问题:每个页面都使用路径的方式进行引入,这样很麻烦,效率很低,这个时候可以配置一个别名,来直接使用别名引入。 使用

    2024年02月09日
    浏览(29)
  • 【实战】一、项目起航:项目初始化与配置 —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(一)

    学习内容来源:React + React Hook + TS 最佳实践-慕课网 相对原教程,我在学习开始时(2023.03)采用的是当前最新版本: 项 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月12日
    浏览(35)
  • react import 引用失效 node_modules/@types/react/index.d.ts not a module.ts

    react + ts的项目,正常使用vs code打开, 先运行 npm install 安装依赖过后 结果所有的react引用依旧标红,如下图所示: 点击红线 show problem(查看问题),提示 node_modules/@types/react/index.d.ts not a module.ts 试着用Visual Studio可以正常打开运行,疑似Visual Studio Code IDE 类型识别问题。 在Vis

    2024年02月04日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包