unjs/unimport

Opt-out for auto-import

antfu opened this issue · 1 comments

antfu commented

Describe the feature

The auto-import feature is convenient to use but sometimes could be confusing when users accidentally introduce circular auto-import references for accidentally introducing unnecessary references to some modules. Being about to opt-out auto-import for some modules could help to make some parts better organized.

Here are a few raw ideas:

1. Magic comments

We could do a quick RegExp check in the transforming phase to have fine-grained opt-out of some modules:

// @imports-disable
// @unimport-disable

2. Configuration

I guess it's already possible by setting the exclude options to exclude by globs.

Unimport({
  exclude: [
    'composables/**'
  ]
}

The issue we could need to solve might be to opt-out of the global TypeScript declarations for those modules.

3. Auto-injected magic comments

Taking it a bit further, we might do automatic import injection. We could have a comment marking the module as:

// @unimport-auto-inject
import { foo } from './foo'

And once you used a new import from the auto-import registry, let say bar:

// @unimport-auto-inject
import { foo } from './foo'

console.log(bar)

In development mode, we could inject the bar import automatically. Once we save the file, unimport will see the new usage communing and inject it like this:

// @unimport-auto-inject
+ import { bar } from './bar' // auto injected on save
import { foo } from './foo'

console.log(bar)

This way, it can help users make clear what and where the imports are auto-imported without worrying them too much.

Additional information

  • Would you be willing to help implement this feature?
antfu commented
  1. and 2. are supported in v1.2