Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not tree shakable React Pure Component #12

Open
MariDani opened this issue Jul 1, 2021 · 1 comment
Open

Not tree shakable React Pure Component #12

MariDani opened this issue Jul 1, 2021 · 1 comment

Comments

@MariDani
Copy link

MariDani commented Jul 1, 2021

I am developping library of components with rollup and I am using agadoo tool to check if it is tree shakable.
Maybe this question is off the topic, but when I use the below code the library is not tree shakable. And I don't understand why.

import React from 'react';

class Welcome extends React.PureComponent {
    render() {
        return <h1>Hello, {this.props.name}</h1>;
    }
}

export default Welcome

On the other hand, when I don't use the React Component, it stays tree shakable, so what's wrong with the React component?

import React from 'react';

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

export default Welcome
@steveluscher
Copy link

This is because agadoo can't know that the member expression React.PureComponent has no side effect.

Consider that React is an object. When you access a property of an object (in this case PureComponent) it might be backed by a ‘getter’ – a function that runs to get you the value.

const React = {
  get PureComponent() {
    // Perform some awful side effect.
    window.__PURE_COMPONENT_WAS_USED = true;
    return /* ... */;
  }
}

Agadoo just throws up its hands, says ‘I can't possibly know that property access didn't result in a side effect,’ and does not tree-shake the code away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants