CSS不适用于反应

时间:2020-10-09 20:22:22

标签: css reactjs webpack css-loader

我有以下简单的项目,我想使用Webpack将基本CSS文件中的某些CSS应用于React项目中。

这是我使用的文件。

index.js

import Home from './Home.js';

Home.js

import React, { Component } from "react";
import ReactDOM from "react-dom";
import './Home.css';

class Home extends Component {
render() {

  return (
    <div className='card'>Hello World</div>
  );
}
}

export default Home;

const wrapper = document.getElementById("container");
wrapper ? ReactDOM.render(<Home />, wrapper) : false;

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>helloworld</title>
</head>
<body>
<div id="container"></div>
</body>
</html>

Home.css

body { background-color: orange; }
.card { background-color: purple; }

webpack.config.js

const path = require('path');
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules)/,
                include: /src/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env', '@babel/preset-react']
                    }
                }
            },
            {
                test:/\.css$/,
                use:['style-loader','css-loader']
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/index.html",
            filename: "./index.html"
        })
    ],
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    mode: 'development'
};

运行npm run build可以正常工作,我可以在浏览器中看到该页面,但是未应用任何样式。我在这里想念什么?

0 个答案:

没有答案