Skip to content

Commit e7dc87f

Browse files
feat(BrandLoadingScreen): improve lottie animations (#1235)
In order to verify the improvements, I've modified locally the size-stats example and used bundle analizer to compare the results. 1. Used an online tool to compress the lottie JSON files for Vivo-new logo. This improved in 20% the total size of the lotties: - Before: ![MicrosoftTeams-image](https://github.com/user-attachments/assets/e75682f6-4957-495c-8236-d806f0c62882) - After: ![MicrosoftTeams-image (1)](https://github.com/user-attachments/assets/80f5ad0c-4ba6-4e38-b96d-d51fce67fc0b) 2. I noticed that the main bottleneck was using `lottie-react` to render the lotties. This lib imports `lottie-web`, and that library is quiet heavy. [Some people recommended](airbnb/lottie-web#1184 (comment)) importing `lottie-light` from `lottie-web` and using it as replacement for the default one. What I've done is copying a minimized version of ([lottie-react](https://github.com/Gamote/lottie-react/blob/main/src/components/Lottie.ts) into our repository and adapted it to use `lottie-light` instead of `lottie-web`. This caused a pretty big improvement (over 50% of the total size of the lib). - Before: ![MicrosoftTeams-image (2)](https://github.com/user-attachments/assets/57d48ee0-bacc-49d0-9522-c51712366637) - After: ![MicrosoftTeams-image (3)](https://github.com/user-attachments/assets/135ae454-7abb-4735-a465-5bbdc446e4f3) In total, by using the exact same example in size-stats, we can see that the bundles size has been reduced by over 160kb: - Before: ![MicrosoftTeams-image (4)](https://github.com/user-attachments/assets/aaf590cd-5f0d-4df6-874f-69e61f488529) - After: ![MicrosoftTeams-image (5)](https://github.com/user-attachments/assets/9b79fc91-f568-4070-b319-34fab818d936) ## 🗒️ Note The size stats comment in this PR only shows the improvements caused by compressing the vivo lotties. The ones obtained by replacing the lottie library are not visible because the example page in the size-stats doesn't use a BrandLoadingScreen component (and the lotties are imported lazily). Refer to the images I've attached in this description to see the impact.
1 parent 02c91f6 commit e7dc87f

10 files changed

Lines changed: 3539 additions & 3666 deletions

File tree

58.3 KB
Binary file not shown.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
diff --git a/build/index.d.ts b/build/index.d.ts
2+
index d7bcde667d3db9510ee67e27ca4e6ecdd0641e14..9b28fd02c458e524107ed3f61cd945fecc0eeeed 100644
3+
--- a/build/index.d.ts
4+
+++ b/build/index.d.ts
5+
@@ -1,6 +1,6 @@
6+
/// <reference types="react" />
7+
-import { AnimationDirection, AnimationSegment, AnimationItem, RendererType, AnimationConfigWithData, AnimationEventName } from 'lottie-web';
8+
-export { default as LottiePlayer } from 'lottie-web';
9+
+import { AnimationDirection, AnimationSegment, AnimationItem, RendererType, AnimationConfigWithData, AnimationEventName } from 'lottie-web/build/player/lottie_light';
10+
+export { default as LottiePlayer } from 'lottie-web/build/player/lottie_light';
11+
import * as react from 'react';
12+
import react__default, { RefObject, MutableRefObject, AnimationEventHandler, ReactElement, CSSProperties } from 'react';
13+
14+
diff --git a/build/index.es.js b/build/index.es.js
15+
index a2d9d48661a41bfb5c44df246eec19935d8a1162..1394d1116bd5a4b86ee2fdbbb9f096ad3824207f 100644
16+
--- a/build/index.es.js
17+
+++ b/build/index.es.js
18+
@@ -1,5 +1,5 @@
19+
-import lottie from 'lottie-web';
20+
-export { default as LottiePlayer } from 'lottie-web';
21+
+import lottie from 'lottie-web/build/player/lottie_light';
22+
+export { default as LottiePlayer } from 'lottie-web/build/player/lottie_light';
23+
import React, { useState, useRef, useEffect } from 'react';
24+
25+
function _iterableToArrayLimit(arr, i) {
26+
diff --git a/build/index.js b/build/index.js
27+
index ad6dbb0947b4a342383436808f2be3ab7161eaf3..3111e99f9e55e85786d971b931aedfaca419a617 100644
28+
--- a/build/index.js
29+
+++ b/build/index.js
30+
@@ -2,7 +2,7 @@
31+
32+
Object.defineProperty(exports, '__esModule', { value: true });
33+
34+
-var lottie = require('lottie-web');
35+
+var lottie = require('lottie-web/build/player/lottie_light');
36+
var React = require('react');
37+
38+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
39+
diff --git a/build/index.umd.js b/build/index.umd.js
40+
index 05a46ac14be5f01ef400c7dda874f4d4646d8f82..077e99122143581e0154794d8ca2419881e1bd84 100644
41+
--- a/build/index.umd.js
42+
+++ b/build/index.umd.js
43+
@@ -1,6 +1,6 @@
44+
(function (global, factory) {
45+
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lottie-web'), require('react')) :
46+
- typeof define === 'function' && define.amd ? define(['exports', 'lottie-web', 'react'], factory) :
47+
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lottie-web/build/player/lottie_light'), require('react')) :
48+
+ typeof define === 'function' && define.amd ? define(['exports', 'lottie-web/build/player/lottie_light', 'react'], factory) :
49+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["lottie-react"] = {}, global.Lottie, global.React));
50+
})(this, (function (exports, lottie, React) { 'use strict';
51+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Start prototyping interfaces with Mistica components in the
115115
- [Analytics](https://github.com/Telefonica/mistica-web/blob/master/doc/analytics.md)
116116
- [Fonts](https://github.com/Telefonica/mistica-web/blob/master/doc/fonts.md)
117117
- [Testing notes](https://github.com/Telefonica/mistica-web/blob/master/doc/testing.md)
118+
- [Using Lottie](https://github.com/Telefonica/mistica-web/blob/master/doc/lottie.md)
118119

119120
## Development
120121

doc/lottie.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Optimizing bundles size when using Lotties
2+
3+
You may notice that when using `BrandLoadingScreen` component, the `lottie-web` package is imported lazily.
4+
This package is a dependency of the `lottie-react` library that we use to render Lottie files.
5+
6+
There are several reports mentioning that `lottie-web` is quiet big, so their developers have created a
7+
"light" version of it, that only supports some of the features
8+
([Link](https://github.com/airbnb/lottie-web/wiki/Lottie-Light)).
9+
10+
If your use case of `lottie-react` library is not too complex and this light version covers your needs, you
11+
can patch their library to import the `lottie-web` functionalities from `lottie-web/build/player/lottie_light`
12+
instead. This will reduce the size of the library considerably.
13+
14+
You can refer to this [example patch](../.yarn/patches/lottie-react-npm-2.4.0-d4d95c55aa.patch) generated by
15+
using `yarn patch` to understand how to update the `lottie-react` library. There is also
16+
[a pull request](https://github.com/Gamote/lottie-react/pull/86) in their GitHub repository where you can see
17+
the required diff in the `lottie-react` source code.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@
200200
"react-dom": "18.2.0",
201201
"@types/react": "^18.0.26",
202202
"@types/react-dom": "^18.0.10",
203-
"@testing-library/dom": "^8.19.1"
203+
"@testing-library/dom": "^8.19.1",
204+
"lottie-react@^2.4.0": "patch:lottie-react@npm%3A2.4.0#./.yarn/patches/lottie-react-npm-2.4.0-d4d95c55aa.patch"
204205
},
205206
"packageManager": "yarn@3.6.1",
206207
"workspaces": [

0 commit comments

Comments
 (0)