美文网首页
create-react-app 踩坑记

create-react-app 踩坑记

作者: 别过经年 | 来源:发表于2018-05-04 17:26 被阅读218次

1. 引入antd 按需加载

我是npm run eject后的进行引入antd
首先需要安装 babel-plugin-import

create-react-app 生成项目后,在package.json下面会有下面的配置:

 "babel": {
     "presets": ["react-app"]
 }

在上面的基础上修改

  "babel": {
    "presets": ["react-app"],
    "plugins": [
      [
        "import",
        {
          "libraryName": "antd",
          "style": "css"
        }
      ]
    ]
  },

在react里直接写,就可以了

import { Button } from "antd";

按理说新建.babelrc

{
  "presets": ["react-app"],
  "plugins": [
    [
      "import",
      {
        "libraryName": "antd",
        "style": "css"
      }
    ]
  ]
}

这样也可以达到目的的,但是样式出不来

还有一种方式是修改webpack配置,但是记得在dev和prod都要修改

       {
            test: /\.(js|jsx|mjs)$/,
            include: paths.appSrc,
            loader: require.resolve("babel-loader"),
            options: {
              plugins: [
                //  css
                ["import", { libraryName: "antd", style: "css" }]
                //  less or sass
                // ["import", { libraryName: "antd", style: true }]
              ],

              // This is a feature of `babel-loader` for webpack (not Babel itself).
              // It enables caching results in ./node_modules/.cache/babel-loader/
              // directory for faster rebuilds.
              cacheDirectory: true
            }
          },

2. 打印编译时间

  1. yarn build --profile --json
    使用npm run build --profile --json无效
    Support for '--profile' option for webpack from react-scripts build
  2. time npm run build
    Add a compile time to webpack dev server and react-scripts #2576

webpack文档:

Hints from build stats

There is an analyse tool which can perform a detailed analysis and provide useful information on how to optimize your build size and performance.

You can generate the required JSON file by running webpack --profile --json > stats.json

build performance

相关文章

  • create-react-app 踩坑记

    1. 引入antd 按需加载 我是npm run eject后的进行引入antd首先需要安装 babel-plug...

  • Android Material Design 踩坑记(2)

    Android Material Design 踩坑记(1) CoordinatorLayout Behav...

  • Deepin使用踩坑记

    1. 前言 很喜欢Deepin,奈何坑太多,不过不怕,踩过去~ 2. 踩坑记 2.1 Deepin重启后文件管理器...

  • SpringStreaming+Kafka

    摘自 :Spark踩坑记——Spark Streaming+Kafka [TOC] SpringStreaming...

  • 原生App植入React Native 踩坑记

    原生App植入React Native 踩坑记 为什么我踩坑了有一个需求要去可以在当前工程的feature/RN ...

  • [ANR Warning]onMeasure time too

    ConstraintLayout 踩坑记一次封装组合控件时的坑,我才用了集成 ConstraintLayout 来...

  • IdentityServer 部署踩坑记

    IdentityServer 部署踩坑记 Intro 周末终于部署了 IdentityServer 以及 Iden...

  • 踩坑记

    1、android自签名证书Glide加载不出图片 关于https中自签名证书的介绍以及OkHttp中解决自签名证...

  • 踩坑记

    6月初,看到广西龙脊梯田有个疏秧节,很是心动!我十几年前就去过龙脊,当时觉得整片的梯田又美又壮观,壮族风情浓厚,就...

  • 踩坑记

    该篇文章记录踩过的坑 uglifyjs-webpack-plugin压缩代码报punc (()错误网上查资料,说是...

网友评论

      本文标题:create-react-app 踩坑记

      本文链接:https://www.haomeiwen.com/subject/iotjrftx.html