[英]React Js, Web Pack - not able to import css file from node_module into a react component
I'm using the react-html5-camera-photo
node module in my project to take a photo from the camera.我在我的项目中使用
react-html5-camera-photo
节点模块从相机拍摄照片。 The library requires the component using the Camera
component to import a css
file ( react-html5-camera-photo/build/css/index.css
) to style capture buttons.该库需要使用
Camera
组件的组件导入css
文件 ( react-html5-camera-photo/build/css/index.css
) 以设置捕获按钮的样式。
Even after adding import 'react-html5-camera-photo/build/css/index.css'
into my react component, the styles are not applied and on checking the rendered elements through inspect element none of the styles are applied from the imported css
file.即使在将
import 'react-html5-camera-photo/build/css/index.css'
到我的 react 组件中后,也不会应用样式,并且在通过检查元素检查渲染元素时,导入的css
也没有应用任何样式文件。
I tried changing my webpack.config.js
settings but had no success.我尝试更改我的
webpack.config.js
设置但没有成功。
Here are my webpack.config.js
settings这是我的
webpack.config.js
设置
module: {
rules: [
{
test: /react\-html5\-camera\-photo(.*?)\.css$/,
use: [
{ loader: "css-loader", options:{
modules: false
}}
]
},
{
test: /\.(scss|css)$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader",
options: {
modules: true,
}},
{ loader: "sass-loader" }
]
}
]
}
But with this setting, the css-loader
shows Module build error
但是有了这个设置,
css-loader
显示Module build error
ERROR in ../node_modules/react-html5-camera-photo/build/css/index.css
Module build failed (from ../node_modules/css-loader/dist/cjs.js):
CssSyntaxError
(2:7) \node_modules\react-html5-camera-photo\build\css\index.css Unknown word
1 |
> 2 | import API from "!../../../style-loader/dist/runtime/injectStylesIntoStyleTag.js";
| ^
3 | import domAPI from "!../../../style-loader/dist/runtime/styleDomAPI.js";
4 | import insertFn from "!../../../style-loader/dist/runtime/insertBySelector.js";
@ ./components/OnsiteSituation/OnsiteSituation.tsx 84:0-54
@ ./components/ServiceLead/ServiceLead.tsx 47:0-65 79:803-818
@ ./App.tsx 33:0-63 45:370-381
@ ./index.tsx 3:0-26 8:21-24
webpack 5.64.0 compiled with 1 error in 29408 ms
Thanks in advance,提前致谢,
I'm guessing the issue is react-html5-camera-photo\\build\\css\\index.css
was processing with css-loader
twice due to meeting the rules twice so I think your issue can be sorted out by just exclude
react-html5-camera-photo
from the 2nd rule as following:我猜这个问题是
react-html5-camera-photo\\build\\css\\index.css
由于两次满足规则而使用css-loader
处理两次,所以我认为您的问题可以通过exclude
react-html5-camera-photo
来自第二条规则如下:
[
{
test: /react\-html5\-camera\-photo(.*?)\.css$/,
use: [
// ...
]
},
{
test: /\.(scss|css)$/,
// exclude your above rule to avoid processing twice
exclude: /react\-html5\-camera\-photo(.*?)\.css$/,
use: [
// ...
]
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.