反转模块不起作用,React 16

时间:2018-03-16 18:49:48

标签: reactjs webpack css-modules

我已经安装了css-loader和style-loader。然后我配置了webpack.dev和webpack.prod。 我想webpack存在问题,因为组件的语法并不复杂。 该组件忽略样式。为什么呢?

的package.json

"dependencies": {
    "css-loader": "^0.28.11",
    "prop-types": "^15.6.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.1.1",
    "style-loader": "^0.20.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject"`enter code here`: "react-scripts eject"  
  }

webpack.config.dev.js

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

webpack.config.prod.js

test: /\.css$/,
loader: ExtractTextPlugin.extract(
    Object.assign(
    {
        fallback: {
            loader: require.resolve('style-loader'),
            options: {
                hmr: false,
            },
        },
        use: [
        {
            loader: require.resolve('css-loader'),
            options: {
                importLoaders: 1,
                modules: true,
                localIdentName: '[name]__[local]__[hash:base64:5]',
                minimize: true,
                sourceMap: shouldUseSourceMap,
            },
        },

成分<​​/ P>

import React, { Component } from 'react';
import styles from './addTask.css';

export default class AddTask extends Component {
  render() {
    return (
      <div className={styles.big}>
        this should be a big font
      </div>
    );
  }
}

组件的css

.big {
    font-size: 111px;
}

5 个答案:

答案 0 :(得分:3)

addTask.css
重命名为
addTask.module.css
,并将导入内容更改为
import styles from './addTask.module.css'

答案 1 :(得分:1)

我有同样的问题。正在使用git跟踪文件。

在运行npm run eject以在webpack.config文件中进行编辑之前,我已经添加了并将react软件包提交到​​仓库中。

然后我运行npm run eject并添加了上述更改。再次我重新启动npm,它对我有用。

答案 2 :(得分:0)

您可以为webpack配置文件尝试以下语法吗?

https://webpack.js.org/concepts/loaders/#configuration
https://github.com/webpack-contrib/css-loader

    for (TouchableObj obj : untouchedSet) {  // <--- This is where ConcurrentModificationException strikes
        if (obj.isTouched()) {
            untouchedSet.remove(obj);
            touchedSt.add(obj);
            break;  // this is key to avoiding returning to the foreach
        }
    }

答案 3 :(得分:0)

尝试将其粘贴到“ test:/.css$/,”下面。它帮助了我。

use: [
                  require.resolve('style-loader'),
                  {
                      loader: require.resolve('css-loader'),
                      options: {
                          importLoaders: 1,
                          modules:true,
                          localIdentName:'[name]__[local]__[hash:base64:5]'
                      },
                  },
                  {
                      loader: require.resolve('postcss-loader'),
                      options: {
                          // Necessary for external CSS imports to work
                          // https://github.com/facebookincubator/create-react-app/issues/2677
                          ident: 'postcss',
                          plugins: () => [
                              require('postcss-flexbugs-fixes'),
                              autoprefixer({
                                  browsers: [
                                      '>1%',
                                      'last 4 versions',
                                      'Firefox ESR',
                                      'not ie < 9', // React doesn't support IE8 anyway
                                  ],
                                  flexbox: 'no-2009',
                              }),
                          ],
                      },
                  },
              ],

答案 4 :(得分:0)

为了让React了解您的CSS文件是一个CSS模块,您需要在使用比.module更新的react-script版本的同时,在文件名中添加后缀2.0.0

只需将addTask.css重命名为addTask.module.css并将导入更改为:

import styles from './addTask.module.css'