tailwindlabs / heroicons

A set of free MIT-licensed high-quality SVG icons for UI development.
https://heroicons.com
MIT License
21.03k stars 1.27k forks source link

Tree-shakable issues when building a library on top of heroicons and vite #1171

Closed PedroAugustoRamalhoDuarte closed 2 weeks ago

PedroAugustoRamalhoDuarte commented 1 month ago

Hello, thanks for this library!!

The problem

I am building a component library on top of heroicons and headlessui, but I am having some problem to make my component library tree-shakable. The components that using heroicons are not tree-shakable.

I am using this tool (agadoo) to check tree-shakable config, but I am not sure if we can trust 100% in it.

I saw in the source code that you guys annotate the with PURE annotations, but when I run agadoo with this component its fails

const React = require("react");
function AdjustmentsHorizontalIcon({
  title,
  titleId,
  ...props
}, svgRef) {
  return /*#__PURE__*/React.createElement("svg", Object.assign({
    xmlns: "http://www.w3.org/2000/svg",
    fill: "none",
    viewBox: "0 0 24 24",
    strokeWidth: 1.5,
    stroke: "currentColor",
    "aria-hidden": "true",
    "data-slot": "icon",
    ref: svgRef,
    "aria-labelledby": titleId
  }, props), title ? /*#__PURE__*/React.createElement("title", {
    id: titleId
  }, title) : null, /*#__PURE__*/React.createElement("path", {
    strokeLinecap: "round",
    strokeLinejoin: "round",
    d: "M10.5 6h9.75M10.5 6a1.5 1.5 0 1 1-3 0m3 0a1.5 1.5 0 1 0-3 0M3.75 6H7.5m3 12h9.75m-9.75 0a1.5 1.5 0 0 1-3 0m3 0a1.5 1.5 0 0 0-3 0m-3.75 0H7.5m9-6h3.75m-3.75 0a1.5 1.5 0 0 1-3 0m3 0a1.5 1.5 0 0 0-3 0m-9.75 0h9.75"
  }));
}
const ForwardRef = React.forwardRef(AdjustmentsHorizontalIcon);
module.exports = ForwardRef;

The fix

The fix, it's only add one more pure annotation in forwardRef function call.

// ....
const ForwardRef = /*#__PURE__*/ React.forwardRef(AdjustmentsHorizontalIcon);
module.exports = ForwardRef;
thecrypticace commented 2 weeks ago

If you're using Vite then it'll use the ESM version of Heroicons which from my testing doesn't have problems tree shaking even without the pure annotation on React.forwardRef(…).

Having said that, we should add the annotation anyway as React.forwardRef doesn't have side effects and it could help other build tools.

PedroAugustoRamalhoDuarte commented 2 weeks ago

@thecrypticace thanks for the PR