简体   繁体   English

React Js,Web Pack - 无法将 css 文件从 node_module 导入到 React 组件中

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM