分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

[Webpack + React] Import CSS Modules with TypeScript and webpack

发布时间:2023-09-06 01:39责任编辑:蔡小小关键词:CSSWebwebpackReact

If you try to use CSS Modules in TypeScript the same way you would use them in JavaScript, with webpack’s css-loader, you’ll see an error: ”TS2307: Cannot find module ‘./styles.css‘”. typings-for-css-modules-loader is a drop-in replacement for css-loader that works with TypeScript and generates typings for CSS on the fly.

You’ll need to install dependencies first:

npm install --save-dev css-loader typings-for-css-modules-loader

Webpack:

...{ ???????????????test: /\.css$/, ???????????????include: path.join(__dirname, ‘src‘), ???????????????use: [ ???????????????????‘style-loader‘, ???????????????????{ ???????????????????????loader: ‘typings-for-css-modules-loader‘, ???????????????????????options: { ???????????????????????????modules: true, ???????????????????????????namedExport: true, ???????????????????????????camelCase: true, ???????????????????????}, ???????????????????}, ???????????????], ???????????},...

css:

.salmon-button { ???padding: 0.5em 1em; ???background: snow; ???color: salmon; ???border: 1px solid salmon; ???border-radius: 3px; ???font-size: 1.6em; ???font-weight: bold;}

js:

With camelCase turned on in Webpack, we can do:

import * as styles from ‘./index.css‘;const html = `<button class=${styles.salmonButton}>Click me!</button>`;document.getElementById(‘app‘).innerHTML = html;

Instead of:

styles[‘salmon-button‘]

[Webpack + React] Import CSS Modules with TypeScript and webpack

原文地址:https://www.cnblogs.com/Answer1215/p/8361472.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved