简体   繁体   English

使用 Webpack 4 在 React 项目中添加 css 文件

[英]Addng css files in React project with Webpack 4

I am building a simple React app from scratch using Webpack.我正在使用 Webpack 从头开始​​构建一个简单的 React 应用程序。 I was finally able to run the dev server and when I tried to apply some styles via CSS, the files wouldn't load and I assume my Webpack 4 configuration is not right.我终于能够运行开发服务器,当我尝试通过 CSS 应用一些样式时,文件无法加载,我认为我的 Webpack 4 配置不正确。

Here is the code:这是代码:

webpack.config.js webpack.config.js

// const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const path = require('path');


module.exports = {
    mode: 'development',
    entry: ['./src/index.js'],
    resolve: {
        extensions: [".js", ".jsx"]
    },
    output: {
      filename: 'main.js',
      path: path.resolve(__dirname, 'dist'),
      publicPath: '/dist',
    },
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 8080
    },
    module: {
        rules: [
            {
                test: /\.js|.jsx$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                options: {
                    presets: ["react"]
                }

            },
            {
                test: /\.css$/,
                use: ["style-loader", "css-loader"]
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url-loader'
            }
        ]
    },

};

My project structure is like this, I will include only src because it lives in the root:我的项目结构是这样的,我将只包含src因为它位于根目录中:

 **src**
      |_assets
      |_componentns
      |_styles
        |_App.css
        |_index.css
      App.jsx
      index.js
      index.html

I would like to be able to add multiple css files for each component I have and apply them, and to be able to style the index the index.html .我希望能够为我拥有的每个组件添加多个 css 文件并应用它们,并且能够将索引设置为index.html Thank you very much for your help.非常感谢您的帮助。

Your webpack configuration looks fine.您的webpack配置看起来不错。 make sure you import the required css files in your components .确保在components import所需的css文件。

import "./App.css";

class App extends Component {
  render() {
    return (
      <div>
        <Child />
      </div>
    );
  }
}

All you have to do is import the CSS files when needed as you would a JavaScript module.您所要做的就是在需要时像导入 JavaScript 模块一样导入 CSS 文件。 So if you want to have a style sheet for your whole application, you can import a global stylesheet in your index.js .因此,如果您想为整个应用程序创建一个样式表,您可以在index.js导入一个全局样式表。

import './styles/index.css';

and you can do the same for each component with specific styles并且您可以对具有特定样式的每个组件执行相同操作

import './styles/App.css'

in which case you might want to setup CSS modules to avoid overlapping class names.在这种情况下,您可能希望设置 CSS 模块以避免类名重叠。

Ok, rookie mistake here, the way I ahve set up webpack is I have to build it first and then run the dev server, no the other way around.好吧,这里是菜鸟错误,我设置 webpack 的方式是我必须先构建它然后运行开发服务器,而不是相反。 All answers above are valid and helpful, I just forgot to run build after changes.上面的所有答案都是有效且有用的,我只是忘记在更改后运行构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM