question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tree shaking and ESM Support not useful

See original GitHub issue

I’ve been trying to get tree shaking to work with heroicons in our app using webpack.

When importing I would expect unused icons to be tree shaken, but in order to do so the esm version has to be explicitly imported.

import { MenuIcon } from '@heroicons/react/solid'; // no tree shaking
import { MenuIcon } from '@heroicons/react/solid/esm'; // tree shaking works

This is not expected as other libraries manage to make this work. This is a real issue in that the runtime/bundler can’t decide for itself which loader to use. In our app we have a browser bundle, a SSR bundle and we run our code via jest, all these environments need to dynamically choose which loader to use and they can’t do that if we have to hard code one in the code.

After some research I believe the package.json needs a main and module key for the bundler to be able to select which build to use https://github.com/rollup/rollup/wiki/pkg.module

I don’t think it’s that easy though as you can’t specify multiple entrypoints and heroicons has 2, solid/esm/index.js and outline/esm/index.js

In the meantime we have to import specific icons to avoid loading the entire library. This isn’t ideal as it’s hard to enforce and IDEs will often “help” by collapsing them.

import MenuIcon from '@heroicons/react/solid/MenuIcon';

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
adamwathancommented, Sep 28, 2021

Unfortunately just not a priority right now, happy to accept a PR otherwise will get to it when we get to it. Sorry 😞

1reaction
HarshaVardhanNakkinacommented, Feb 23, 2022

@adamwathan is this related to package.json file in esm folders? the contents of the package.json file in esm folder after running npm run build-react are like this

{
  "type": "module",
  "sideEffects": false
}

but for tree-shaking to work, I guess, there should be an entry called "module": "<path-to-entry-file>" if this is the problem, I would like to contribute 😊.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree-Shaking: A Reference Guide - Smashing Magazine
Tree -shaking” is a must-have performance optimization when bundling JavaScript. In this article, we dive deeper on how exactly it works and ...
Read more >
Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >
How does ESM tree-shaking / dead code elimination work?
Tree-shaking is most useful on browsers, to avoid downloading code that is not useful. Lambdas have access to a very high bandwidth network,...
Read more >
Reduce JavaScript payloads with tree shaking - web.dev
Tree shaking is a form of dead code elimination. The term was popularized by Rollup, but the concept of dead code elimination has...
Read more >
Tree shaking and code splitting in webpack - LogRocket Blog
ESM allows for the most optimal tree shaking because CommonJS, AMD, and UMD can all be non-deterministic and thus, impossible to statically ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found