Skip to content
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

Mechanism for package authors to give auto-import hints #53037

Closed
Twipped opened this issue Feb 28, 2023 · 10 comments
Closed

Mechanism for package authors to give auto-import hints #53037

Twipped opened this issue Feb 28, 2023 · 10 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Twipped
Copy link

Twipped commented Feb 28, 2023

Apologies if this exists and I just couldn't find documentation on it. We have several in-house npm packages to offer common functionality across our codebases. Each of these packages defines subpath exports entries to directly target individual functions and components, in addition to getting everything from the root entry. To use an example with MUI, you can do either of these:

import { Grid } from '@mui/material';

or

import Grid from '@mui/material/Grid'`

The problem is, the auto-import completion rarely even lists the later, just the former, or sometimes, when it does go deeper, it tries to import the direct file (eg, @mui/material/node/Grid/Grid.js) instead of using the subpath export.

It would be very helpful if I, as a package author, could give weight to the exports so that I can tell vscode "this is actually the better path to use for suggesting this export."

@mjbvz mjbvz transferred this issue from microsoft/vscode Mar 1, 2023
@mjbvz mjbvz removed their assignment Mar 1, 2023
@DanielRosenwasser
Copy link
Member

Something in the same vein that I've been considering is the ability for package authors to specify a default name for namespace auto-imports (#52527, #23830).

@Andarist
Copy link
Contributor

Andarist commented Mar 1, 2023

Something in the same vein that I've been considering is the ability for package authors to specify a default name for namespace auto-imports (#52527, #23830).

Oh yes 🙏 this would be super awesome. In a similar vein... some consumers prefer namespace-based imports so it would be cool to have that configurable from the consuming side of things as well.

@DanielRosenwasser
Copy link
Member

Is there any reason that you prefer the subpath export?

@Andarist
Copy link
Contributor

Andarist commented Mar 1, 2023

Hm, I didn't exactly have subpath exports/imports in mind. Just that in some codebases import * as React from 'react' is preferred. It would be cool if auto-importing useState could result in this:

// pre
useStat/*cursor*/

// post
import * as React from 'react'
React.useState/*cursor*/

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 1, 2023

Sorry @Andarist, I meant that more as a question for @Twipped.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Mar 2, 2023
@Twipped
Copy link
Author

Twipped commented Mar 3, 2023

Wow, I was really confused for a moment there about how this ended up on the typescript repo.

@DanielRosenwasser Importing from root exports increases overhead in testing and build processes due to increased IO churn and tree-shaking. We actually have a linting rule to prevent importing from several root packages, like @mui/icons-material where the root has HUNDREDS of named exports. We saw a significant reduction in CI execution time after adding that rule to our codebases.

So vscode's auto-import always grabbing the root increases development time, since it immediately gets flagged by eslint.

@RyanCavanaugh
Copy link
Member

I sort of wonder if the package itself is the right place for this information. material-ui could have chosen to not ship a barrel, after all, so they're presumably at least somewhat agnostic on the question. For things on DT, this could definitely start some style flame wars as people debate which form is "correct".

Related, it looks like someone's already made a VS Code plugin for this exact situation.

Does #49578 fix this in your project? I'm not sure what the matching rules are but it seems like you could block imports from exactly @mui/material

@Twipped
Copy link
Author

Twipped commented Mar 7, 2023

@RyanCavanaugh ohh, that might indeed solve it. I'll take a look today

Edit: Unfortunately, this just removes MUI from the auto-import entirely, it doesn't make the deeper file available. Same happens for our own internal libs with subpath exports. The issue seems to be that vscode isn't even seeing those other options.

Material doesn't use subpath exports, they just rely on node-resolution to hit the deeper pathways, so it would seem vscode isn't traversing the exports when deciding what files to offer.

My own utils library does use subpath exports, however, and still has the same result. Like if I type isUndef, the menu offers me three options, and all are targeting the root export. If I block that path using autoImportFileExcludePatterns, the list is just empty.

@Twipped
Copy link
Author

Twipped commented Apr 24, 2023

I figured it out! I can actually solve for this, by defining a typesVersions entry in my package.json.

  "typesVersions": {
    "*": {
      "*": ["./dist/*.d.ts"]
    }
  },

This gets vscode to properly recommend the subpath export instead of the root export. Problem solved!

@Twipped Twipped closed this as completed Apr 24, 2023
@joshjg
Copy link

joshjg commented Oct 19, 2023

I figured it out! I can actually solve for this, by defining a typesVersions entry in my package.json.

  "typesVersions": {
    "*": {
      "*": ["./dist/*.d.ts"]
    }
  },

This gets vscode to properly recommend the subpath export instead of the root export. Problem solved!

I'm not sure how that would solve the issue (it doesn't for my case)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants