How does explicit import work with tree shacking?
See original GitHub issuePer 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:
- Created 3 years ago
- Comments:10 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@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.