npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@schemcraft/remove

v1.8.0

Published

A schematic to remove unused imports and variables from an angular project

Downloads

410

Readme

Remove Unused Imports and Variables - Angular Schematic

This Angular schematic cleans up your TypeScript files by automatically removing unused imports and variables. It also analyzes Angular HTML templates to ensure that variables used in templates are preserved, making it a complete solution for optimizing your Angular codebase.


Features

  • Automatically removes unused imports from .ts files.
  • Removes unused variables from .ts files.
  • Analyzes Angular templates (both inline and external) to detect variables used in the HTML.
  • Traverses your Angular project to ensure a clean, optimized, and maintainable codebase.

Installation

  1. Install the schematic package in your Angular project:

    npm install @schemcraft/remove  
  2. Run the schematic using Angular CLI:

    ng generate @schemcraft/remove:remove-unused  

Usage

To execute the schematic on your Angular project:

ng generate @schemcraft/remove:remove-unused  

This command processes all .ts files in your project, identifies unused imports and variables, and removes them while ensuring compatibility with variables used in Angular templates.


Examples

Input File: example.component.ts

import { Component } from '@angular/core';  
import { UnusedService } from './unused.service';  

@Component({  
  selector: 'app-example',  
  templateUrl: './example.component.html',  
})  
export class ExampleComponent {  
  title = 'This is used';  
  unusedVariable = 'This is not used';  

  constructor() {  
    console.log('ExampleComponent initialized');  
  }  
}  

Input File: example.component.html

<h1>{{ title }}</h1>  

Output File: example.component.ts (After Running the Schematic)

import { Component } from '@angular/core';  

@Component({  
  selector: 'app-example',  
  templateUrl: './example.component.html',  
})  
export class ExampleComponent {  
  title = 'This is used';  

  constructor() {  
    console.log('ExampleComponent initialized');  
  }  
}  

How It Works

  1. File Traversal: The schematic scans all TypeScript files (.ts) and associated HTML templates (.html) in your Angular project.
  2. Analysis: It identifies:
    • Unused imports: Imports that are not referenced in the code.
    • Unused variables: Variables declared but not used in either TypeScript files or Angular templates.
  3. Cleanup: Automatically removes unused imports and variables while retaining any variables referenced in templates.

Limitations

  • Complex Dynamic Usage: Variables or imports referenced through dynamic techniques (like eval, dynamic object property access, or reflection) may not be detected. Such cases require manual verification.

Best Practices

  • Backup Your Code: Always back up your code or use version control before running the schematic.
  • Review Changes: After running the schematic, review the changes to ensure no critical code is removed.

Contributing

We welcome contributions! If you have ideas for new features, encounter a bug, or want to improve this project, feel free to:

  • Open an issue on GitHub.
  • Submit a pull request.

Let’s make this project even better together! 🚀


License

This project is licensed under the MIT License.


Author

Developed by Maruthupandian M ([email protected]).

Feel free to reach out for feedback, suggestions, or contributions. Happy coding! 😊