0

我正在尝试在 react 中使用 css 模块,但它不起作用。

此代码的日志:

import React from 'react'
import styles from '../../css/test.css'

class Test extends React.Component {
    render() {
        console.log(styles)
        return (
            <div className={styles.app}>
                <p>This text will be blue</p>
            </div>
        );
    }
}

export default Test

返回对象 {}

并且呈现的代码是没有类的标签:

<div><p>This text will be blue</p></div>

css 代码可在网站上找到,这是我的 test.css:

.test p {
  color: blue;
} 

如果我将 div 更改为 class='test',则 p 的颜色变为蓝色

这是我的 webpack.config.js

var path = require('path')
var webpack = require('webpack')
var HappyPack = require('happypack')
var BundleTracker = require('webpack-bundle-tracker')
var path = require('path')
var ExtractTextPlugin = require("extract-text-webpack-plugin")

function _path(p) {
  return path.join(__dirname, p);
}

module.exports = {

    context: __dirname,
    entry: [
        './assets/js/index'
    ], 

    output: {
        path: path.resolve('./assets/bundles/'), 
        filename: '[name].js'
    },

    devtool: 'inline-eval-cheap-source-map',

    plugins: [
        new BundleTracker({filename: './webpack-stats.json'}), 
        new webpack.ProvidePlugin({ 
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery' 
        }),
        new HappyPack({
            id: 'jsx',
            threads: 4,
            loaders: ["babel-loader"]
        }),
        new ExtractTextPlugin("[name].css", { allChunks: true })

    ],

    module: {
        loaders: [

             {
                test: /\.css$/,
                include: path.resolve(__dirname, './assets/css/'),
                loader: ExtractTextPlugin.extract("style-loader", "css-loader!resolve-url-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]")
            },

            {
                test: /\.scss$/,
                include: path.resolve(__dirname, './assets/css/'),
                loader: ExtractTextPlugin.extract("style-loader", "css-loader!resolve-url-loader!sass-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]")
            },

            {
                test: /\.jsx?$/, 
                include: path.resolve(__dirname, './assets/js/'),
                exclude: /node_modules/, 
                loaders: ["happypack/loader?id=jsx"]
            },
            {
                test: /\.png$/,
                loader: 'file-loader',
                query: {
                    name: '/static/img/[name].[ext]'
                }
            }
        ]
    },

    resolve: {
        modulesDirectories: ['node_modules'],
        extensions: ['', '.js', '.jsx'],
        alias: {
          'inputmask' : _path('node_modules/jquery-mask-plugin/dist/jquery.mask')
        }, 
    }   
}

谁能帮我?

提前致谢。

4

3 回答 3

0

这个问题已经过去了很多时间,所以我的 webpack 用其他技术更新了很多次。

这个 webpack 配置正在工作:

...
module.exports = {
  entry,
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  devtool:
    process.env.NODE_ENV === 'production' ? 'source-map' : 'inline-source-map',

  module: {
    rules: [
      {
        test: /\.jsx?$/,
        include: path.resolve(__dirname, './app/view/'),
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.pcss$/,
        include: path.resolve(__dirname, './app/view/'),
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader:
              'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: function() {
                return [
                  require('postcss-import'),
                  require('postcss-mixins'),
                  require('postcss-cssnext')({
                    browsers: ['last 2 versions']
                  }),
                  require('postcss-nested'),
                  require('postcss-brand-colors')
                ];
              }
            }
          }
        ],
        exclude: /node_modules/
      },
      {
        test: /\.(png|svg|jpg|webp)$/,
        use: {
          loader: 'file-loader'
        }
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx'],
    modules: [path.resolve(__dirname, 'node_modules')]
  },
  plugins
};

我猜这行的 webpack 上的旧版本存在问题:

'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'

尝试 importLoaders 和 importLoader

你也可以看到我的回购

于 2017-11-14T18:26:20.187 回答
0

看起来您将 css-loader 参数传递给 resolve-url-loader:

"css-loader!resolve-url-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]"

应该:

"css-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]&importLoaders=1!resolve-url-loader"
于 2016-10-18T00:08:33.053 回答
-2

在我的具体情况下,我使用的是官方实用程序facebook/create-react-app。您必须运行以下命令才能访问 webpack 配置:

npm run eject

然后,您需要编辑config/webpack.config.jscss-loadermodules选项并将其设置为true.

use: getStyleLoaders({
                importLoaders: 1,
                sourceMap: isEnvProduction && shouldUseSourceMap,
                modules:true <-----
              }),
于 2019-11-14T08:46:29.380 回答