Skip to content

Feature: Add report Function for Import Analysis Output #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

daiyanze
Copy link

Summary

This PR introduces a new report function to the unimport package, enabling users to output, format, and persist matched import analysis results. The function supports multiple output formats, printing to the console, and writing to files, with customizable options.

Implementation

  • Added a report function that accepts matched imports and an options object.
  • Supports output formats: table, json, and compact.
  • Allows printing to the console (printOut), writing to a file (outputToFile), or both.
  • Output formatting is customizable via printFormat and outputFormat options.
  • Includes error handling for file operations and fallback formatting.
  • Provides a printFn option for custom output handling.
  • Refactored related code for maintainability and extensibility.

Tests

  • Added unit tests for the report function, covering:
    • All output formats and combinations of options
    • Edge cases (empty imports, invalid formats, file write errors)
    • Custom print functions and path resolution
  • Added tests for integration with reportMatched in the unimport context
  • Achieved high test coverage for all new and affected code paths

resolves #449

Copy link
Member

@antfu antfu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, but I don't think the printing logic belongs to unimport. Ideally, we should have an API to retrieve the matched data in a structured format, and leave it to the users to decide how to consume the data. Printing to the terminal and a very niche and opinionated need, we don't want to introduce extra dependencies/logics for that either.

@daiyanze
Copy link
Author

@antfu

Thanks for the review. Now I learnt that unimport as a Nuxt dependency would have to stayed streamlined to provide limited functions. (Correct me if I'm wrong...)

Honestly, I held doubt that the additional dependency of table would end up with benefits, as the package size isn't small (roughly 300kb).

we should have an API to retrieve the matched data in a structured format, and leave it to the users to decide how to consume the data

I'd like to hear your voices over the APIs to provide for the data consumption before proceeding with further changes.

  1. This PR provides the matched imports in Json array format for simplicity and personal-flavor. Do you think it better to keep the original data structure of Object { from: "", import: "", ... } to retain intuitive contexts instead?

  2. About the usage, the options now is an Object. Maybe we could turn it into a method to with the matched imports.

Unimport.vite({
  dirs: ['./composables/**/*'],
  reportMatched(imports) {
    // your own process here
  },
})

@antfu
Copy link
Member

antfu commented May 15, 2025

  1. Yeah I think it's easier and straightforward to keep the data structure as is
  2. Callback is a good idea, we could call it onMatched. But we need to decide on the timing of when it been called, would that be per code module, or per import item (because we need to consider the on-demand situation in dev mode)

Another way to do that might be:

Unimport.vite({
  dirs: ['./composables/**/*'],
  onInited(unimport) {
    // users's own logic to the instance, for example
    setTimeout(() => {
		console.log(unimport.getMatchedImports())
    }, 1000)
  },
})

@daiyanze
Copy link
Author

daiyanze commented May 17, 2025

Yeah I think it's easier and straightforward to keep the data structure as is

Okay. No issue with keeping it verbose.

Callback is a good idea, we could call it onMatched. But we need to decide on the timing of when it been called, would that be per code module, or per import item (because we need to consider the on-demand situation in dev mode)

For my part, a list generated after all imported modules being resolved would be enough. Thus, the "report" I implemented was put under the buildEnd hook.

I assume the Callback in this discussion to be only available with interacting with hooks? (Otherwise, it'll return empty due to invoked before any of the hooks)

For instance, when-detected / when-resolved / when-inited / when-deduped. It makes me feel that your suggestion is a debugging function instead of reporting.

However, unimport didn't provide the hooks yet... (Don't think the Vite hook would be the targeted ones...) Is hooks design already on the roadmap?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add report Function for Import Analysis Output
2 participants