使用babel-plugin-react-cssmodules替代react-css-modules

栏目: JavaScript · 发布时间: 7年前

内容简介:在更新 React 至 16.6.*后,使用 react-css-modules 的项目会出现以下 Warning主要是因为在 react-cssmodules 中,重写了 this.props,详见我们访问 react-css-modules 的项目,发现官方推荐了 babel-plugin-react-css-modules 作为替代品,其有以下优势:

在更新 React 至 16.6.*后,使用 react-css-modules 的项目会出现以下 Warning

Expected instance props to match memoized props before componentDidUpdate. This is likely due to a bug in React. Please file an issue.

主要是因为在 react-cssmodules 中,重写了 this.props,详见 issues

我们访问 react-css-modules 的项目,发现官方推荐了 babel-plugin-react-css-modules 作为替代品,其有以下优势:

  1. 将处理过程交给 babel 而不是 runtime, 提升 runtime 性能
  2. 不用频繁的调用 cssmodules 高阶组件
  3. 统一处理 options 等

替换过程

官方案例很简单,但是我们替换的过程中还是遇到了挺多的问题。主要包含以下几个步骤:

1. 安装 babel-plugin-react-css-modules

不赘述

2. 升级 babel 7

升级 babel 为@babel@7.1.6

对应的 plugin 也需要进行升级, 官方有介绍相应的替换

"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.6",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",

3. 格式化 stylus 文件

我们需要对 stylus 进行统一的格式化处理,防止 babel 对语法校验出错,由于文件较多,我们使用 stylus-supremacy 进行处理,使用 node 写一个批处理文件:

const stylusSupremacy = require("stylus-supremacy");
const fs = require("fs");
const path = require("path");

const formattingOptions = {
  insertColons: false,
  insertSemicolons: false,
  insertBraces: false
};

function formatFile(path){
  const content = fs.readFileSync(path, "utf8");
  const result = stylusSupremacy.format(content, formattingOptions);
  fs.writeFileSync(path, result, "utf8");
  console.log("format 成功: " + path);
}
...

4. 配置 css-loaders

const cssRegex = /\.css$/;
const cssModuleRegex = /\.cssmodule\.css$/;
const stylRegex = /\.styl$/;
const stylModuleRegex = /\.cssmodule\.styl$/;

const getStyleLoaders = (cssOptions, preProcessor) => {
  const loaders = [
    require.resolve("style-loader"),
    {
      loader: require.resolve("css-loader"),
      options: cssOptions
    }
  ];
  if (preProcessor) {
    loaders.push(require.resolve(preProcessor));
  }
  return loaders;
};

// webpack配置,添加getLocalIdent方法
rules: [
  {
    test: stylRegex,
    exclude: stylModuleRegex,
    use: getStyleLoaders({ importLoaders: 2 }, "stylus-loader")
  },
  {
    test: stylModuleRegex,
    use: getStyleLoaders(
      {
        importLoaders: 2,
        modules: true,
        camelCase: "dashes",
        getLocalIdent({ resourcePath }, localIdentName, localName) {
          return generateScope(localName, resourcePath);
        }
      },
      "stylus-loader"
    )
  }
];

5. 配置 babel-plugin-react-css-modules

generateScopedName 需要与 cssloader 处理的结果一致,否则将会失效。

同时,styl 文件需要 sugarss 进行处理,记得安装

[
  "react-css-modules",
  {
    generateScopedName: localIdentName,
    filetypes: { ".styl": { syntax: "sugarss" } },
    webpackHotModuleReloading: true, // 支持热加载
    handleMissingStyleName: "warn", // 没有时进行警告
    exclude: ".css$"
  }
];

6. 运行

删除之前的 react-css-modules

问题

  • 同时处理多个文件时报错

在文件中引用多个文件,会报错

import './common.css'
import './index.cssmodule.styl'

解决方案: 添加配置: exclude: “.css\$”


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

阿里巴巴Java开发手册

阿里巴巴Java开发手册

杨冠宝 / 电子工业出版社 / 2018-1 / 35

《阿里巴巴Java开发手册》的愿景是码出高效,码出质量。它结合作者的开发经验和架构历程,提炼阿里巴巴集团技术团队的集体编程经验和软件设计智慧,浓缩成为立体的编程规范和最佳实践。众所周知,现代软件行业的高速发展对开发者的综合素质要求越来越高,因为不仅是编程相关的知识点,其他维度的知识点也会影响软件的最终交付质量,比如,数据库的表结构和索引设计缺陷可能带来软件的架构缺陷或性能风险;单元测试的失位导致集......一起来看看 《阿里巴巴Java开发手册》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具