在的WebPack CSS配置中未检测类样式,而是检测元件的样式

问题描述 投票:2回答:1

我使用与的WebPack和巴贝尔反应。我的WebPack配置如下。当我写CSS到像DIV,UL,李等它在我App.js检测到,但是当我尝试写基于类CSS它没有显示在页面上检测到的元素。

webpack.config.js

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: {
    main: path.join(__dirname, '..', '/src/index.js'),
  },
  output: {
    path: path.resolve(__dirname, '..', 'dist'),
    filename: '[name].[hash].bundle.js',
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader?name=[name].[ext]',
      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude: /node_modules/,
      },
      {
        test: /\.s?css$/,
        exclude: /node_modules/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: true,
                modules: true,
                importLoaders: true,
                localIdentName: '[name]__[local]--[hash:base64:5].css',
                sourcemaps: true,
              },
            },
            {
              loader: 'sass-loader',
              options: {
                sourcemaps: true,
              },
            },
          ],
        }),
      },
      {
        test: /\.s?css$/,
        include: /node_modules/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: true,
              },
            },
            {
              loader: 'sass-loader',
              options: {
                sourcemaps: true,
              },
            },
          ],
        }),
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
};

index.css

.test {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 1em; 
  list-style: none;
  padding: 12px;
}

App.js

import React from 'react';
import '../index.css';


const App = () => (
  <div>
     <h1>Hello Adeva!!</h1>
     <ul className="test">
        <li>Romans</li>
        <li>Corinthians</li>
        <li>Thessalonians</li>
        <li>Ephesians</li>
        <li>Phillipians</li>
        <li>Colossians</li>
      </ul>
  </div>
)

export default App;

我反应过来的版本是16.8.1,是的WebPack 4.29.3。请指导我,因为这的WebPack配置件事很让我困惑。

javascript css reactjs webpack
1个回答
2
投票

您已配置的WebPack在你的CSS-装载机配置添加modules: true用css模块。使用CSS模块,您对访问样式JavaScript对象,就像这样:

import React from 'react';
import styles from '../index.css';


const App = () => (
  <div>
     <h1>Hello Adeva!!</h1>
     <ul className={styles.test}>
  </div>
)

-1
投票

为什么复杂的事情就去做

<ul class="test">

纯CSS和HTML

© www.soinside.com 2019 - 2024. All rights reserved.