Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/patricklafrance/rac-webpack-tree-shaking

POC to test React Aria Components tree shaking with webpack
https://github.com/patricklafrance/rac-webpack-tree-shaking

Last synced: about 1 month ago
JSON representation

POC to test React Aria Components tree shaking with webpack

Awesome Lists containing this project

README

        

# rac-webpack-tree-shaking

This is a POC to test if React Aria Components can be tree-shaken when using Webpack 5.

## Getting Started

Install the dependencies with:

```bash
pnpm install
```

Build the code:

```bash
pnpm build
```

If you want to serve the build:

```bash
pnpm serve-build
```

## What's in this repository?

The `index.jsx` file render a simple React application with an header and a `Button` component from `react-aria-components`. As we only import a single `Button` component from `react-aria-components`, we expect that only RAC utils/shared code and the `Button` component would be added to the production bundle.

## Conclusion

Tree-shaking does work but it requires to install the `TerserPlugin` and having the `optimization.usedExports: true` configuration.

To test it, first only import the `Button` component from `react-aria-components`:

```jsx
import { Button } from "react-aria-components";

...

{
alert("You clicked me!");
}}
>
Click me

```

Then, open the [dist/main.js](./dist/main.js) file and search for "listBox". **You should find no result.**

Then, also import the `Listbox` component from `react-aria-components`:

```jsx
import { Button, ListBox, ListBoxItem } from "react-aria-components";

...

{
alert("You clicked me!");
}}
>
Click me

Aardvark
Cat
Dog
Kangaroo
Panda
Snake

```

Open the [dist/main.js](./dist/main.js) file and search for "listBox" again. **This time, you should find at least one result!**

### Development mode

There is one issue thought.. Since RAC is released as a single file and the [Terser](https://webpack.js.org/plugins/terser-webpack-plugin/) plugin isn't executed when in development, RAC doesn't really benefit from tree shaking when in development.

## Webpack bundle analyzer

You can't use the [webpack-bundle-analyzer](https://www.npmjs.com/package/webpack-bundle-analyzer) plugin to test this as it isn't able to consider tree shaking in the stats that it shows: https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/161

## Notes

Next time, use the sourcemaps to identify what's end up in the bundle as suggested by [Devon](https://github.com/adobe/react-spectrum/discussions/5204#discussioncomment-7795915).