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

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

内容简介:在更新 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\$”


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

查看所有标签

猜你喜欢:

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

世界因你不同:李开复自传(纪念版)

世界因你不同:李开复自传(纪念版)

李开复,范海涛 著作 / 中信出版社 / 2015-7-10 / 39.00

编辑推荐 1.李开复唯一一部描写全面生平事迹的传记:《世界因你不同:李开复自传》书中讲述了家庭教育培育的“天才少年”;学校教育塑造的“创新青年”,走入世界顶级大公司,苹果、微软、谷歌等亲历的风云内幕,岁月30载不懈奋斗、追求事业成功的辉煌历程。 2.娓娓道来、字字珠玑、可读性和故事性皆佳。李开复博士是青少年成长成才的励志偶像,年轻家长、学校教师阅读后也能从中得到感悟和启发。 3.......一起来看看 《世界因你不同:李开复自传(纪念版)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具