将React 17与Webpack 5和babel-plugin-react-css-modules(具有样式名称的CSS模块)集成

Sharath

我正在尝试使用最新版本设置React应用程序。React 17,Webpack 5和其他模块。

我需要通过使用babel-plugin-react-css-modules与styleName概念的css模块

尝试运行代码将显示输出,但未应用任何样式。

package.json

{
  "name": "react-app",
  "version": "1.0.0",
  "description": "React Template App",
  "author": "",
  "license": "ISC",
  "main": "index.js",
  "scripts": {
    "start": "webpack serve --mode development",
    "build": "webpack --mode production",
    "format": "prettier --write \"src/**/*.js\"",
    "eslint-fix": "eslint --fix \"src/**/*.js\""
  },
  "dependencies": {
    "babel-plugin-react-css-modules": "^5.2.6",
    "react": "^17.0.1",
    "react-dom": "^17.0.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.10",
    "@babel/preset-env": "^7.12.11",
    "@babel/preset-react": "^7.12.10",
    "babel-eslint": "^10.1.0",
    "babel-loader": "^8.2.2",
    "css-loader": "^5.0.1",
    "eslint": "^7.16.0",
    "eslint-config-react": "^1.1.7",
    "eslint-loader": "^4.0.2",
    "eslint-plugin-react": "^7.21.5",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^4.5.0",
    "less": "^4.0.0",
    "less-loader": "^7.2.0",
    "mini-css-extract-plugin": "^1.3.3",
    "prettier": "2.2.1",
    "style-loader": "^2.0.0",
    "webpack": "^5.11.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.0"
  }
}

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname + '/dist',
    publicPath: '/',
    filename: 'bundle.js'
  },
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    open: true,
    host: "localhost",
    compress: true,
    port: process.env.CLIENT_PORT,
    hot: true,
    quiet: false
  },
  module: {
    rules: [{
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'eslint-loader']
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          MiniCssExtractPlugin.loader,
          // "less-loader",
          {
            loader: 'css-loader',
            options: {
              modules: true,
            }
          }
        ],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ["file-loader"]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./public/index.html",
      filename: "./index.html",
      favicon: "./public/favicon.ico"
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ],
  devtool: 'inline-source-map',
};

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    [
      "babel-plugin-react-css-modules",
      {
        "webpackHotModuleReloading": true,
        "autoResolveMultipleImports": true
      }
    ]
  ]
}

home.js

import React from "react";

import "./style.css";

const Home = () => {
  return (
    <div styleName="container">
      <div styleName="title">Hello from Home CSS</div>
    </div>
  );
};

export default Home;

style.css

.container {
  display: flex;
  flex-direction: column;
}

.title {
  font-size: 24px;
  color: red;
}
tmhao2005

这是一个有趣的问题,需要我们做更多的事情才能使其工作如下:

  • babel-plugin-react-css-modulescss-loader生成名称时无法正常使用但幸运的是,我们可以使用某人的临时修复方法来解决@dr.pogodin/babel-plugin-react-css-modules因此,只需安装所需的软件包:
npm i -D @dr.pogodin/babel-plugin-react-css-modules postcss // postcss is required aslo
  • 通过更改名称来重新配置babel配置.babelrc
{
  // ...
  "plugins": [
    [
      "@dr.pogodin/babel-plugin-react-css-modules",
      {
        "webpackHotModuleReloading": true,
        "autoResolveMultipleImports": true
      }
    ]
  ]
}

  • 最后,我们将改变类的名称,使其保持一致之间babel-plugin-react-css-modules css-loaderwebpack.config.js
{
  test: /\.css$/,
  use: [
    // ...
    {
      loader: 'css-loader',
      options: {
        modules: {
          localIdentName: '[path]___[name]__[local]___[hash:base64:5]', // This pattern matches with the default in `babel-plugin-react-css-modules`
        },
      }
    }
  ],
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

babel-plugin-react-css-modules与styleName的样式不匹配

babel-plugin-react-css-modules不会更改样式标签中的类名称

Material-UI-支持babel-plugin-react-css-modules和rtl应用

Webpack和React识别CSS类名称

如何在create-react-app中设置babel-plugin-react-css-modules?

无法使用“ babel-plugin-react-css-modules”导入CSS-获取“ ParseError:意外令牌”

使用 webpack 和 React 在开发/生产中配置 CSS 模块

如何使用Typescript,React和Webpack导入SCSS或CSS模块

使用Webpack,Typescript和React的CSS模块格式错误?

具有CSS模块和React的多个className

CSS模块,React和覆盖CSS类

React和CSS动画

WebPack-React-TypeScript:加载CSS和导出类型

使Webpack CSS和React一起工作

以文本形式导入和读取CSS文件的内容(React / webpack)

CSS未在React和Webpack的生产中加载

Webpack,React和Babel,不呈现DOM

无法将babel-plugin-module-resolver与expo和react-native一起使用

将样式CSS导入React JS应用

将 CSS 和 HTML 汉堡菜单转换为 React 组件

将Webpack与babel和babel-preset-react和babel-preset-es2015一起使用

React将Div和Table组件转换为具有多个页面和CSS的PDF

React / Webpack图像未加载,但CSS加载

模块构建失败-Webpack,React,Babel

在React中将字符串类名称与CSS-loader和Webpack一起使用

通过SASS支持在带有Typescript,SASS和CSS模块的React组件库中搜索Webpack配置

在React中导入CSS文件-Webpack /内联样式

在 React Hooks 中使用带有样式组件的 CSS attr()

无法将CSS文件导入React项目