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.

How does explicit import work with tree shacking?

See original GitHub issue

Per your doc:

import ReactDOM from 'react-dom'
import { library } from '@fortawesome/fontawesome-svg-core'
import { fab } from '@fortawesome/free-brands-svg-icons'
import { faCheckSquare, faCoffee } from '@fortawesome/free-solid-svg-icons'

library.add(fab, faCheckSquare, faCoffee)

fab contains all the font-awesome brand icons. Does that mean they’ll be loaded by the browser even if they’re not actually used?

I’m working with Next.js and I’m currently naming all icons one by one and it’s cumbersome, and I wish to import all icons and kinda hope tree shacking will remove those that aren’t actually used.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
robmadolecommented, Oct 26, 2020

Tree-shaking isn’t quite this smart. It doesn’t know if you are dynamically using an icon.

Right now we don’t have anything better than just importing the icons individually. It’s a pain, but that’s the cost of keeping the bundle size small at the moment. We’ve got plans for helping with this in the future.

0reactions
jimjefferscommented, Jul 29, 2022

@fullhdpixel I just set the "module" attribute in my tsconfig.json to compile my sibling packages as ES6 instead of commonJS. It’s been a few years so your mileage may vary with modern projects and FA6. But in my case I was compiling the packages in my monorepo as commonJS so that my jest tests wouldn’t complain about transpiling dependencies. Webpack can’t perform tree shaking on anything compiled in CommonJS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree Shaking - webpack
A "side effect" is defined as code that performs a special behavior when imported, other than exposing one or more exports. An example...
Read more >
Reduce JavaScript payloads with tree shaking - web.dev
In production builds, webpack can be configured to "shake" off exports from ES6 modules that weren't explicitly imported, making those production builds smaller ......
Read more >
JavaScript tree shaking, like a pro | by Daniel Brain - Medium
Using import and export is the first essential step to allow tree-shaking to happen. Most other module patterns, including commonjs and require.
Read more >
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 >
explicit import versus * sign in javascript - Stack Overflow
Using the second form allows bundlers like Webpack to optimize your bundle size using a process known as 'tree shaking'. Tree shaking is...
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