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 - automatically detecting usage

See original GitHub issue

Looking at the recommended usage

src/main.js

import Vue from 'vue'
import App from './App'
import { library } from '@fortawesome/fontawesome-svg-core'
import { faUserSecret } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

library.add(faUserSecret)

Vue.component('font-awesome-icon', FontAwesomeIcon)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})

src/App.vue

<template>
  <div id="app">
    <font-awesome-icon icon="user-secret" />
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

And then the tree shaking docs:

import { library } from '@fortawesome/fontawesome-svg-core'
import { faCoffee } from '@fortawesome/free-solid-svg-icons/faCoffee'
import { faSpinner } from '@fortawesome/pro-light-svg-icons/faSpinner'

library.add(faCoffee, faSpinner)

Is your feature request related to a problem? Please describe. Having to call library.add(faCoffee) is a pain. Using about 80 icons in a bigger project, keeping this up-to-date isn’t nice.

Describe the solution you’d like Shouldn’t the usage like <font-awesome-icon icon="user-secret" /> suffice to be able to dynamically import only those icons? Especially when using webpack/rollup to parse it.

Describe alternatives you’ve considered N/A

Additional context N/A

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
GTANAdamcommented, Oct 23, 2020

Okay, as promised, here’s my repository https://github.com/GTANAdam/vue-fontawesome-autogen

4reactions
GTANAdamcommented, Oct 17, 2020

Might just do that, it will at least help with the arrays everywhere, the only downside being having to pass through all the other props.

Excited to see what they have planned for 6, hopefully it tackles this.

To me the bigger pain is the imports and library call anywhere you want to use an icon. (Yes I could centralize it to a single file but then you have to do book keeping and if you change or delete an icon somewhere you have to do code base garbage collection making sure there’s no other references) so doing it in place everywhere it’s used is surprisingly the more manageable option.

Lmao, I was about to quote all of the above, thankfully I wasn’t lazy enough to edit the quotation.

Yeah well, you only have to set up the component once and make it global, it will act like a shim or proxy or whatever you want to call it. you can find the props here: https://github.com/FortAwesome/vue-fontawesome/blob/2.x/src/components/FontAwesomeIcon.js

You can have my little template, the rest you can complete from that file I have linked

<template >
  <font-awesome-icon :icon="getIcon" :border="border" :fixedWidth="fixedWidth" :flip="flip" :mask="mask"
    :listItem="listItem" :pull="pull" :pulse="pulse" :rotation="rotation" :swapOpacity="swapOpacity" :size="size"
    :spin="spin" :transform="transform" :symbol="symbol" :title="title" :inverse="inverse" />
</template>

As for manually importing icons for treeshaking, yeah well, it is indeed annoying but I have managed to successfully write an auto build script that would walk through all of my vue files, extract the icon names from them and autogenerate a fontawesome-autogen.js file which is basically importable.

The script is about 115 lines, I will have it published in my repo once I’m done with it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree Shaking
Clarifying tree shaking and sideEffects ... The sideEffects and usedExports (more known as tree shaking) optimizations are two different things. sideEffects is ...
Read more >
Tree shaking - MDN Web Docs Glossary
Tree shaking is a term commonly used within a JavaScript context to describe the removal of dead code.
Read more >
Tree-shaking in real world: what could go wrong?
Tree-shaking is the process of detecting and marking dead code in your bundle based on ES modules' usage of import and export statements....
Read more >
Tree Shaking - How to Clean up Your JavaScript
Tree shaking is a strategy web developers use to create leaner JavaScript bundles by getting rid of unused code. This guide will explain...
Read more >
Learn about tree shaking in Webpack 5
Tree shaking is a technique used by webpack to optimize the builds for production and reducing the build size by shaking off the...
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