我正在与webpack和babel交流。我有如下的webpack配置。当我将CSS写入div,ul,li等元素时,在我的App.js
中会检测到它,但是当我尝试编写基于类的CSS时,在页面上却找不到它。
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'],
},
};
.test {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 1em;
list-style: none;
padding: 12px;
}
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配置对我来说非常令人困惑。
答案 0 :(得分:2)
您已经通过在css-loader配置中添加modules: true
,将webpack配置为使用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 :(得分:-1)
为什么会使事情复杂化
<ul class="test">
纯CSS和html