- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习一些关于如何在 Webpack 中为 React 设置内部 sass/scss/css 支持的教程。我终于得到了我满意的东西,使用 HtmlWebpackPlugin
生成基于模板的 index.html
,sass-loader
,typings-for-css-modules-loader
(用于处理 typescript 中的 css 模块的 css-loader
包装器)和 MiniCssExtractPlugin
,创建一个单独的我构建的 dist
文件夹中的 css 文件。我的配置是这样的:
module: {
rules: [
...
{ test: /\.(sa|sc|c)ss?$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {}
},
{
loader: 'typings-for-css-modules-loader',
options: {
modules: true,
namedExport: true,
camelCase: true
}
},
'sass-loader'
]
}
]
}
当我在我的 React 组件中将我的 css 类作为对象导入时,此设置有效:
你好.css
.foo {
color: #5df7ff;
}
你好.tsx
import * as React from 'react';
import { foo } from './hello.css';
export interface HelloProps {
compiler: string;
framework: string;
}
export const Hello = (props: HelloProps) =>
<h1 className={foo}>Hello: {props.compiler} and {props.framework}!!!</h1>; // This works: text is colored correctly
当我想在 React 组件中使用字符串类名称时,问题就出现了(与之前的 css 相同):
你好.tsx
import * as React from 'react';
import './hello.css';
export interface HelloProps {
compiler: string;
framework: string;
}
export const Hello = (props: HelloProps) =>
<h1 className="foo">Hello: {props.compiler} and {props.framework}!!!</h1>; // This does not work: text is not colored
我认为这是因为 Webpack 加载器不够“智能”,无法正确解析 React DOM 中的字符串类名:加载器不会将字符串类名映射到 css-loader 生成的散列类名。
我知道在 React 中使用字符串 css 类名并不是包含 css 类的惯用方式:理想情况下,每个组件都有一个完整的 css 类,使用您将在第一个示例中导入的 css 模块。
然而,当使用外部 UI 组件库(例如 Ant Design、Semantic UI)时,问题就出现了,这些组件库似乎引用了它们自己的 css 字符串类。
这让我无法使用这些外部库。
使用 antd
(Ant Design) 的例子:
anthello.tsx
import React from 'react';
import Button from 'antd/lib/button';
import 'antd/es/button/style/index.css';
export interface HelloAntProps {
message: string;
}
export const HelloAnt = ({ message }: HelloAntProps ) => {
return (
<div>
<h1>Hello {message}</h1>
<Button type="primary">Test</Button>
</div>
);
};
使用加载器堆栈生成的 css 文件:
._3WAJv754FZTMKDXGocE913 { /* This class corresponds to the Ant Design ant-btn class */
line-height: 1.5;
display: inline-block;
font-weight: 400;
text-align: center;
...
以及在实际 DOM 中查找的 css 类:
<button type="button" class="ant-btn ant-btn-primary"><span>Test</span></button>
<!-- This does not work, as the only classes present in the css are the hashed ones generated by css-loader -->
我不知道如何导入这些库并使用 Webpack 将它们打包到单个 css 包中而不让它工作。
我错过了什么吗?
tl;dr
有什么方法可以通过某些 Webpack 加载器在 React DOM 中使用 css-loader
正确解析 css 类字符串?我是否缺少任何解决方法?
最佳答案
更新:通过在我的 css-loader
中禁用“css-modules”选项设法解决了这个问题。
css 类名的散列是由于 css-modules
选项。通过禁用此选项,类名称不再被散列,使外部库能够直接引用它们自己的类。
看起来问题并不能通过保留 css-modules
选项轻松解决。
关于css - 在带有 css-loader 和 Webpack 的 React 中使用字符串类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51874593/
我正在使用 webpack 开始我的学习之路,但遇到了一个问题,我认为这是由 extract-loader 引起的。 .当webpack抓取我的HTML文件时,似乎无法正常编译,在使用import.m
我正在使用 tomcat 8,在 tomcat-home/config/catalina.properties ,有关于的部分 common.loader, server.loader and sha
在使用 import 语句时,我对区分 sass-loader 和 css-loader 有点困惑。据我所知,css loader resolve import statment(@import) 和
我的 webpack 加载器数组中有这个加载器: { test: /\.scss$/, exclude: /node_modules/, loaders: ExtractTextPlugin('sty
我对 url-loader 、 file-loader 和 image-loader 感到很困惑。谁能解释一下 url-loader 、 file-loader 和 image-loader 的区别是
我有 page.css @imports index.css。 page.css 和 index.css 都有 display: flex Webpack.config.js 包含: module:
我在 webpack 中使用生产模式构建的多入口点最终 bundle 中导出的多入口编译 js 文件始终包含加载器内容。如何消除它们以及为什么包含它们? 重现 git clone https://gi
模板加载器找到模板但未加载模板 TemplateDoesNotExist at /cardpayment/ cardpayment.html Request Method: GET Reque
当我尝试运行 gradle tR (tomcatRun) 时出现此错误 A child container failed during start java.util.concurrent.Execu
Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/projectna
我计划将 Webpack 用于一个项目,并且我正在使用 Html-loader + file-loader 设置我的工作流程,以获取带有图像动态 src 的生产 html 文件,正如 Colt Ste
我有一个巨大的 csv 文件,其中包含数百万条记录,我想使用 python 脚本将它加载到 Netezza 数据库中。我尝试了简单的插入查询,但速度非常非常慢。可以指出一些示例 python 脚本或一
我想将 ts-loader 与 babel-polyfill 一起使用,但不使用 babel-loader。但是当我尝试构建该项目时,我收到了此错误。谁能告诉我我缺少什么。 ERROR in ./sr
下面是我的 webpack.config.js 和 package.json module.exports = { entry: "./entry.js", output: { fi
我在两台不同的 PC 上遇到了一个问题。对于我的项目,我为开发安装了以下依赖项:(webpack webpack-cli @babel/core @babel/preset-env @babel/pr
模板文件保存在app目录下,但渲染时引发TemplateDoesNotExist异常: 模板加载器事后分析如下: Django 尝试按以下顺序加载这些模板: Using loader django.t
PHPUnit 手册说: If you point the PHPUnit command-line test runner to a directory it will look for *Test
我正在开发一个需要 html 的角度应用程序要提取为纯 HTML 文件的文件,同时应检查任何 要求这些图像(作为 Assets )。另外,图片是基于根路径的(所以 /images/something.
我们在 sql 加载器中遇到了问题。我们正在尝试将一个大约 46 亿行(近 340 GB)的数据文件加载到 2 个 oracle 表中,基于一些使用 Sql Loader 的条件。但是在加载了 42
我将 CSS 模块与 webpack css-loader 一起使用,然后将它们与 mini-css-extract-plugin 捆绑在一起。 这是我的配置的样子: { test: /\.c
我是一名优秀的程序员,十分优秀!