Skip to content

Commit

Permalink
Reduce bundle size after the fontawesome icons changes (aframevr#707)
Browse files Browse the repository at this point in the history
* Use our own AwesomeIcon component instead of FontAwesomeIcon, this reduces minified build by 66 kB

* Remove @fortawesome/react-fontawesome dependency

* Replace classNames dependency by clsx
  • Loading branch information
vincentfretin authored Feb 26, 2024
1 parent 5d75aae commit 5036eda
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 99 deletions.
68 changes: 14 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
"license": "MIT",
"dependencies": {
"@fortawesome/free-solid-svg-icons": "^6.5.1",
"@fortawesome/react-fontawesome": "^0.2.0",
"classnames": "^2.3.2",
"clipboard-copy": "^4.0.1",
"clsx": "^2.1.0",
"lodash.debounce": "^4.0.8",
"prop-types": "^15.8.1",
"react": "^18.2.0",
Expand Down
53 changes: 53 additions & 0 deletions src/components/AwesomeIcon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Use <AwesomeIcon icon={faEye} /> instead of <FontAwesomeIcon icon={faEye} /> from @fortawesome/react-fontawesome
Using FontAwesomeIcon component adds 66 kB minified to the bundle.
Our AwesomeIcon does the same but less than 2 kB minified.
svg-inline--fa class has been added to lib.styl
*/
import React from 'react';
import PropTypes from 'prop-types';

function asIcon(icon) {
const width = icon[0];
const height = icon[1];
const vectorData = icon[4];
let element;

if (Array.isArray(vectorData)) {
element = (
<g>
{vectorData.map((pathData, index) => (
<path key={index} fill="currentColor" d={pathData} />
))}
</g>
);
} else {
element = <path fill="currentColor" d={vectorData} />;
}

return {
width: width,
height: height,
icon: element
};
}

export class AwesomeIcon extends React.Component {
static propTypes = {
icon: PropTypes.object.isRequired
};

render() {
const { width, height, icon } = asIcon(this.props.icon.icon);
return (
<svg
role="img"
className={`svg-inline--fa fa-${this.props.icon.iconName}`}
xmlns="http://www.w3.org/2000/svg"
viewBox={`0 0 ${width} ${height}`}
>
{icon}
</svg>
);
}
}
6 changes: 3 additions & 3 deletions src/components/Collapsible.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import clsx from 'clsx';

export default class Collapsible extends React.Component {
static propTypes = {
Expand Down Expand Up @@ -37,9 +37,9 @@ export default class Collapsible extends React.Component {
if (this.props.className) {
rootClassNames[this.props.className] = true;
}
const rootClasses = classNames(rootClassNames);
const rootClasses = clsx(rootClassNames);

const contentClasses = classNames({
const contentClasses = clsx({
content: true,
hide: this.state.collapsed
});
Expand Down
26 changes: 21 additions & 5 deletions src/components/EntityRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@ import {
faFont,
faLightbulb
} from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { AwesomeIcon } from './AwesomeIcon';

const ICONS = {
camera: <FontAwesomeIcon icon={faCamera} title="camera" />,
mesh: <FontAwesomeIcon icon={faCube} title="mesh" />,
light: <FontAwesomeIcon icon={faLightbulb} title="light" />,
text: <FontAwesomeIcon icon={faFont} title="text" />
camera: (
<i title="camera">
<AwesomeIcon icon={faCamera} />
</i>
),
mesh: (
<i title="mesh">
<AwesomeIcon icon={faCube} />
</i>
),
light: (
<i title="light">
<AwesomeIcon icon={faLightbulb} />
</i>
),
text: (
<i title="text">
<AwesomeIcon icon={faFont} />
</i>
)
};

export default class EntityRepresentation extends React.Component {
Expand Down
6 changes: 3 additions & 3 deletions src/components/Main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { faPlus } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { AwesomeIcon } from './AwesomeIcon';
import Events from '../lib/Events';
import ComponentsSidebar from './components/Sidebar';
import ModalTextures from './modals/ModalTextures';
Expand Down Expand Up @@ -122,7 +122,7 @@ export default class Main extends React.Component {
}}
title="Show components"
>
<FontAwesomeIcon icon={faPlus} />
<AwesomeIcon icon={faPlus} />
</a>
</div>
);
Expand All @@ -140,7 +140,7 @@ export default class Main extends React.Component {
}}
title="Show scenegraph"
>
<FontAwesomeIcon icon={faPlus} />
<AwesomeIcon icon={faPlus} />
</a>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/components/CommonComponents.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { faClipboard } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { AwesomeIcon } from '../AwesomeIcon';
import { InputWidget } from '../widgets';
import DEFAULT_COMPONENTS from './DefaultComponents';
import PropertyRow from './PropertyRow';
Expand Down Expand Up @@ -115,7 +115,7 @@ export default class CommonComponents extends React.Component {
copy(getEntityClipboardRepresentation(this.props.entity));
}}
>
<FontAwesomeIcon icon={faClipboard} />
<AwesomeIcon icon={faClipboard} />
</a>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions src/components/components/Component.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { faClipboard, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { AwesomeIcon } from '../AwesomeIcon';
import PropertyRow from './PropertyRow';
import Collapsible from '../Collapsible';
import copy from 'clipboard-copy';
Expand Down Expand Up @@ -133,14 +133,14 @@ export default class Component extends React.Component {
);
}}
>
<FontAwesomeIcon icon={faClipboard} />
<AwesomeIcon icon={faClipboard} />
</a>
<a
title="Remove component"
className="button"
onClick={this.removeComponent}
>
<FontAwesomeIcon icon={faTrashAlt} />
<AwesomeIcon icon={faTrashAlt} />
</a>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/components/PropertyRow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-prototype-builtins */
import React from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import clsx from 'clsx';

import BooleanWidget from '../widgets/BooleanWidget';
import ColorWidget from '../widgets/ColorWidget';
Expand Down Expand Up @@ -119,7 +119,7 @@ export default class PropertyRow extends React.Component {
const title =
props.name + '\n - type: ' + props.schema.type + '\n - value: ' + value;

const className = classNames({
const className = clsx({
propertyRow: true,
propertyRowDefined: props.isSingle
? !!props.entity.getDOMAttribute(props.componentname)
Expand Down
4 changes: 2 additions & 2 deletions src/components/modals/ModalTextures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { faSearch } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { AwesomeIcon } from '../AwesomeIcon';
import Events from '../../lib/Events';
import Modal from './Modal';
import { insertNewAsset } from '../../lib/assetsUtils';
Expand Down Expand Up @@ -327,7 +327,7 @@ export default class ModalTextures extends React.Component {
value={this.state.filterText}
onChange={this.onChangeFilter}
/>
<FontAwesomeIcon icon={faSearch} />
<AwesomeIcon icon={faSearch} />
</div>
<ul ref={this.registryGallery} className="gallery">
{this.renderRegistryImages()}
Expand Down
Loading

0 comments on commit 5036eda

Please sign in to comment.