antfu-collective / package-manager-detector

Package manager detector
MIT License
36 stars 4 forks source link

feat: add types to exports #26

Open ieedan opened 1 day ago

ieedan commented 1 day ago

Clear and concise description of the problem

resolveCommand is really awesome for CLI apps but also can be really useful for building snippet blocks in front end applications when you want the user to be able to choose their package manager.

The problem is that they way this package is structured you must be in an environment that has access to node. So for instance in Cloudflare Pages there is no access to node so when using resolveCommand even if we aren't using detect it will fail when deploying because node doesn't exist in that environment.

Suggested solution

Would changing the way you import things make a difference to this?

The ideal structure would add package-manager-detector/types to the exports in package.json so that you could have all the types without having to load index.ts and therefore all modules.

This way you would import from package-manager-detector/types and package-manager-detector/commands and that would hopefully avoid needing node:fs since detect.ts would not be loaded.

Example:

- import { ... } from "package-manager-detector" -
+ import { ... } from "package-manager-detector/types" +
+ import { ... } from "package-manager-detector/commands" +

Alternative

No response

Additional context

It's pretty simple to prove/disprove this we would just need to add:

package.json:

{
    // ...
    "exports": {
        // ...
        "./types": {
          "import": "./dist/types.mjs",
          "require": "./dist/types.cjs"
        }
    }
}

and

build.config.ts:

import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
  entries: [
    'src/index.ts',
    'src/commands.ts',
    'src/detect.ts',
    'src/constants.ts',
    // +++
    'src/types.ts',
    // +++
  ],
  clean: true,
  declaration: true,
  rollup: {
    emitCJS: true,
    inlineDependencies: true,
  },
})

Validations

Willing to open a PR for this if you think this is a good idea!

benmccann commented 22 hours ago

when using resolveCommand even if we aren't using detect it will fail when deploying because node doesn't exist in that environment

In what way does it fail? What's requiring Node?

The ideal structure would add package-manager-detector/types to the exports in package.json so that you could have all the types without having to load index.ts and therefore all modules.

You want just the TypeScript types without any functionality? What are you trying to accomplish?

ieedan commented 22 hours ago

Basically I would need types to make use of the package without importing anything from index.ts. But I am not sure if this will allow me get around this.

The svelte-cloudflare adapter fails when resolving dependencies because node doesn't exist in that environment: https://github.com/huntabyte/shadcn-svelte/actions/runs/11856989238/job/33044398976?pr=1491

PR: https://github.com/huntabyte/shadcn-svelte/pull/1491

userquin commented 22 hours ago

@ieedan can you try with the code here https://github.com/antfu-collective/package-manager-detector/pull/27#issuecomment-2479967304 ?

ieedan commented 21 hours ago

@ieedan can you try with the code here https://github.com/antfu-collective/package-manager-detector/pull/27#issuecomment-2479967304 ?

Ah that looks like it makes more sense I just left for the day but I can try this on Monday!