Code Monkey home page Code Monkey logo

ionic-angular-collect-icons's Introduction

Ionic Angular Collect Icons

What is this?

This library is used to uniquely group the ionIcons in a project, and generate for export ionIcons file. In small projects, it is difficult to manage addIcons() of ionIcons each time, so we automated it.

  • development: Stress-free development by add all icons at addIcons.
  • Production: Automatically collect and update the ionIcon used in the template prior to build.

Of course, to maximize bundle size reduction, it is important to load a minimum number of icons at each Component lazy loading. This is a compromise to speed up development.

This project is based ionic-team/ionic-angular-standalone-codemods .

Initialize

npm install @rdlabo/ionic-angular-collect-icons --save-dev

๐Ÿค– Automatic Configuration

npx @rdlabo/ionic-angular-collect-icons --initialize true

๐Ÿ“ Manual Configuration

1. Run the CLI

npx @rdlabo/ionic-angular-collect-icons

This will generate src/use-icons.ts.

2. Import the generated file in your main.ts ( or app.config.ts ) file:

+ import { addIcons } from 'ionicons';
+ import * as allIcons from 'ionicons/icons';
+ import * as useIcons from '../use-icons';

  if (environment.production) {
    enableProdMode();
  }

+  addIcons(environment.production ? useIcons : allIcons);

3. Remove other addIcons calls in class constructor

  @Component(/* ... */)
  export class ExampleComponent {
    constructor() {
-     addIcons(useIcons);
    }
  }

Usage

npx @rdlabo/ionic-angular-collect-icons

Let's automate run

It is inefficient to run commands each time before running a production build, so put them in an npm script to automate the process. Example:

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
+   "prebuild": "npx @rdlabo/ionic-angular-collect-icons",

Warning

This method cannot be used for production builds without using the npm script.

Optional

--interactive [boolean]

If you want to set all CLI option using the prompts, set true. This can be used to check only the results in a Dry run. The default is false.

npx @rdlabo/ionic-angular-collect-icons --interactive true

--initialize [boolean]

If you want to initialize addIcons automatically, you can use the --initialize flag. The default is false. The CLI add lines:

+ import { addIcons } from 'ionicons';
+ import * as allIcons from 'ionicons/icons';
+ import * as useIcons from '../use-icons';

  if (environment.production) {
    enableProdMode();
  }

+  addIcons(environment.production ? useIcons : allIcons);

the CLI will add lines at the file that has enableProdMode(). Of course, it can also be set manually.

And remove other addIcons calls in class constructor.

  @Component(/* ... */)
  export class ExampleComponent {
    constructor() {
-     addIcons(useIcons);
    }
  }
npx @rdlabo/ionic-angular-collect-icons --initialize true

--project-path [string]

If you want to specify the path to the project, you can use the --project-path flag. The default is the current directory.

npx @rdlabo/ionic-angular-collect-icons --project-path /path/to/project

Target files are under the src directory from the specified path.

  • path/to/project + src/**/*.ts
  • path/to/project + src/**/*.html
  • path/to/project + src/**/*.scss

--icon-path [string]

Default create file is (path/to/project +) src/use-icons.ts. If you want to specify the file name, you can use the --icon-path flag.

npx @rdlabo/ionic-angular-collect-icons  --icon-path src/other-use-icons.ts

FAQ

  • Can run addIcons in main.ts?

Yes. Please check this issue: ionic-team/ionic-framework#28445 (comment)

You're more than welcome to register them in main.ts or app.component.ts. You can then use them anywhere in your application. However, the initial bundle size may increase because the icons need to be loaded up front.

  • Support Unit Test?

Unit test at ChromeHeadless don't read main.ts. So, you need to add addIcons in each test, or add addIcons in src/test.ts.

  • Support binding icon name?

No, and we do not plan to support this program. For example, this kind of code is difficult to follow until it is displayed.

@Component({
  selector: "app-example",
  template: ` <ion-icon [name]="iconName"></ion-icon> `,
})
export class ExampleComponent {
  iconName = "add";

  ionViewWillEnter() {
    setTimeout(() => {
      this.iconName = "remove";
    }, 1000);
  }
}

If you are doing this kind of complex processing, please import manually.

Alternatively, if you have a limited number of icons you're binding to, you can add a block in your template as a "hint."

<!-- This is a trick to get ionic-angular-collect-icons
     to include the icons, but it will never render. -->
@if(false) {
<ion-icon name="home"></ion-icon>
<ion-icon name="people"></ion-icon>
}

It's not ideal, but it will help to maintain the automation.

  • Why not addIcons in each component?

This is to minimize diffs by libraries. I did not like to have every component change on every run. I wanted to keep the diff as small as possible.

Developing

  1. Clone this repository.
  2. Run pnpm install to install dependencies
  3. Run pnpm run dev to start the dev server, this will watch for changes and rebuild the project
  4. Run pnpm run start --filter=cli to start the CLI and test the code mods

Testing

This project uses Vitest for unit testing.

Command Description
pnpm run test Run all tests
pnpm run test:watch Run all tests in watch mode

Formatting

This project uses Prettier for code formatting.

Run pnpm run format to format all files in the project.

Additional Resources

ionic-angular-collect-icons's People

Contributors

rdlabo avatar sean-perkins avatar dtarnawsky avatar turbobot-temp avatar walkingriver avatar

Stargazers

J. Degand avatar Jim avatar ippei avatar  avatar

Watchers

 avatar

ionic-angular-collect-icons's Issues

feat: support for nx monorepos

Describe the Feature Request

I've tried to execute the tool in an Nx monorepo by specifying as project-path the path to the Ionic Angular app. The changes in the main.ts are applied, but the file use-icons is not generated.

Describe the Use Case

Being able to execute all the steps described in the guidelines with an Ionic Angular app in an Nx monorepo.

Describe Preferred Solution

No response

Describe Alternatives

No response

Related Code

No response

Additional Information

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.