简体   繁体   English

使用CSS模块时,后台url中的相对路径不起作用

[英]Relative path in background url doesn't work when use css-modules

I use react-react-app. 我使用react-react-app。 I set url: false , and enable css-modules. 我设置url: false ,并启用css-modules。

webpack.gonfig.dev.js(module/ rules): 
       {
        test: /\.css$/,
        use: [
          require.resolve("style-loader"),
          {
            loader: require.resolve("css-loader"),
            options: {
              importLoaders: 1,
              modules: true,
              localIdentName: "[name]__[local]___[hash:base64:5]",
              url: false
            }
          }
        ]
      },

webpack.config.prod.js: webpack.config.prod.js:

                {
                  loader: require.resolve("css-loader"),
                  options: {
                    importLoaders: 1,
                    modules: true,
                    url: false,
                    minimize: true,
                    sourceMap: shouldUseSourceMap
                  }
                },

./index.css background doesn't work( If I use absolute path it works ): ./index.css背景不起作用( 如果使用绝对路径,则可以 ):

.whiteBoardMainSection {
background-image: url(../../assets/white-board-hospital.png);

}

import CSS 导入CSS

import styles from "./index.css";

I shouldn't use url: false when url in node_modules throw err.I should add exclude: [/node_modules/] in loader. 当node_modules中的url抛出err时,我不应该使用url: false我应该在加载器中添加exclude: [/node_modules/]

          {
        test: /\.css$/,
        use: [
          require.resolve("style-loader"),
          {
            loader: require.resolve("css-loader"),
            options: {
              importLoaders: 1,
              modules: true,
              localIdentName: "[name]__[local]___[hash:base64:5]"
            }
          }
        ],
        exclude: [/node_modules/]
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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