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.

Unable to reproduce fully treshakeable icon library

See original GitHub issue

I’ve been trying to follow the steps outlined in the README and the Guide on Medium, but I’m unable to get a fully tree-shakeable library.

All icons always end up in the main bundle, even when I clone the PoC repo .

I have been able to create a fully tree-shakeable icon lib, but it requires additional steps. I had to configure ng-packagr to treat each icon as a secondary entry point.

I moved each icon.ts file into its own directory with the following structure

my_icon_package
β”œβ”€β”€ src
|   β”œβ”€β”€ public_api.ts
|   └── icon.model.ts
β”œβ”€β”€ ng-package.json
β”œβ”€β”€ package.json
└── icons
    β”œβ”€β”€ svg-icon-home
    |   β”œβ”€β”€ src
    |   |   β”œβ”€β”€ public_api.ts
    |   |   └── svg-icon-home.ts
    |   └── package.json
    β”œβ”€β”€ svg-icon-back
    |   β”œβ”€β”€ src
    |   |   β”œβ”€β”€ public_api.ts
    |   |   └── svg-icon-back.ts
    |   └── package.json
    β”œβ”€β”€ svg-icon-forward
    |   β”œβ”€β”€ src
    |   |   β”œβ”€β”€ public_api.ts
    |   |   └── svg-icon-forward.ts
    |   └── package.json

public_api exports the single icon file and package json just contains

{
    "ngPackage": {}
}

Now when I try to build my lib, it actually builds each icon seperately.

Now I have to import each icon from its own secondary entry point Before: import { mySvgIcon } from '@myorg/shared/ui/icons';

now: `import { mySvgIcon } from β€˜@myorg/shared/ui/icons/my-svg-icon’;

This produces the same result you have gotten in your article.

Is this expected or has something changed?

Should support for a new generator β€œpackages” be added?

The setup I have used actually mimics what is being done in the @angular/components and @angular/angular repo

Sample build output:

> ng run shared-ui-icons:build:production 
Building Angular Package

------------------------------------------------------------------------------
Building entry point '@myorg/shared-ui-icons'
------------------------------------------------------------------------------
Compiling TypeScript sources through ngc
Bundling to FESM2015
Bundling to FESM5
Bundling to UMD
Minifying UMD bundle
Writing package metadata
Built @myorg/shared-ui-icons

------------------------------------------------------------------------------
Building entry point '@myorg/shared-ui-icons/src/icons/svg-access-point-network'
------------------------------------------------------------------------------
Compiling TypeScript sources through ngc
Bundling to FESM2015
Bundling to FESM5
Bundling to UMD
Minifying UMD bundle
Writing package metadata
Built @myorg/shared-ui-icons/src/icons/svg-access-point-network

------------------------------------------------------------------------------
Building entry point '@myorg/shared-ui-icons/src/icons/svg-access-point'
------------------------------------------------------------------------------
Compiling TypeScript sources through ngc
Bundling to FESM2015
Bundling to FESM5
Bundling to UMD
Minifying UMD bundle
Writing package metadata
Built @myorg/shared-ui-icons/src/icons/svg-access-point

------------------------------------------------------------------------------
Building entry point '@myorg/shared-ui-icons/src/icons/svg-account-alert'
------------------------------------------------------------------------------
Compiling TypeScript sources through ngc
Bundling to FESM2015
Bundling to FESM5
Bundling to UMD
Minifying UMD bundle
Writing package metadata
Built @myorg/shared-ui-icons/src/icons/svg-account-alert

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
glenngrcommented, Oct 4, 2020

@kreuzerk Thanks for answering. It works fine when the files from the dist folder. I’m not sure the ng-packagr converter I mentioned earlier is needed now. Let me know if you want to have a look anyway. It basically just creates one package subentry for each icon, but the build process is much slower compared to the way you do it now.

I did experiment with creating an angular builder for svg-to-ts, making you able to just run ng run icons:build to produce the tree-shakeable icon files.

The cool thing about it is that if you use it inside an Nx Workspace, nx can cache the build output and you can even configure nx to know that it has to rebuild the library if a new svg-file is added to the svg-directory or if the svg-to-ts configuration changes in package.json.

I think I also want to create some angular-cli schematics to set up the projects you need to create an icon library using svg-to-ts.

Do you think this belongs in a separate project, or would you be interested in having it as part of this repo?

1reaction
kreuzerkcommented, Oct 4, 2020

Hi, @glenngr I researched a bit why this doesn’t work for you and I figured out that there is still something wrong in the description of my post. Sorry for that πŸ˜₯

The issue is that the showcase I was using used the library from the projects folder instead of the dist folder. In such cases, the output is different because we are missing the library build step. The library is built as part of the showcase build an therefore the tree shaking is working.

However, I also used svg-to-ts in a big enterprise client, and there tree shakable icons are working. Basically you need to let svg-to-ts handle the icons and not ng-packagr. ng-packagr produces one big bundle with all icons that break tree shaking. To let svg-to-ts handle the icon compilation you have two options:

  1. Provide the icons as a standalone library. With this approach icons are provided standalone and can be used framework agnostic throughout the various projects. If you consider this approach I would recommend you to check out the starter project we added in the linked PR to the README svg-icon-library-starter.
  2. If you want to have your icons in an Angular workspace you should treat this folder as an assets folder. So basically, put your icons in there, exclude it from the ng build and use svg-to-ts to build the icons into the dist folder.
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create a fully tree shakable icon library in Angular
My previous article describes how to develop a tree shakeable icon library, however, they are not fully tree shakable. Fully tree shakable?
Read more >
Material Symbols and Icons - Google Fonts
Introducing Material Symbols. Material Symbols are our newest icons consolidating over 2,500 glyphs in a single font file with a wide range of...
Read more >
IBM Design Language – UI icons
Icons derive their valuable functionality from the cognitive shortcuts they provide. Find the perfect symbol in this library of IBM iconography.
Read more >
Snap Publisher Icon Library - Snapchat's Business Help Center
Snap Publisher features a number of icons to help you create the perfect video. You can reference this icon library when using the...
Read more >
Cybersecurity and Product Icons Library - Fortinet
Security Icons. The Cybersecurity and Fortinet Product Icons Library includes: Generic Cybersecurity and networking icons as well as Fortinet-specificΒ ...
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