[PReact] Reduce the Size of a React App in Two Lines with preact-compat

Preact压缩React应用
本文介绍如何使用Preact兼容库来显著减小现有React应用程序的文件大小。通过将React替换为Preact,一个示例项目的主包大小从43KB减少到了13KB。文章详细说明了安装和配置步骤。

Not every app is greenfield, and it would be a shame if existing React apps could not benefit from the micro-size of Preact. In this lesson we’ll discuss what preact-compat is, how to use it, and some examples of the file-size savings available. https://github.com/developit/preact-compat

 

Create a new react app:

create-react-app preact-demo

 

Build:

npm run build

 

You can see that the size of main bundle file is 43kb.

 

Now let's run:

npm run eject

 

Install:

npm install --save preact preact-compat

 

open config/webpack.config.prod.js:

    alias: {
      
      // Support React Native Web
      // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
      'react-native': 'react-native-web',
      'react': 'preact-compat',
      'react-dom': 'preact-compat'
    },

In 'alias' field, add tow fields for preact.

 

Run the build again. Now you can see that the bundle size is '13KB'!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值