fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.32k stars 204 forks source link

Add default export to entry point #903

Closed JoseLion closed 3 weeks ago

JoseLion commented 3 weeks ago

Hi,

Would adding a default export on the package entry point be possible?

While I prefer using named exports, some modules can benefit from importing a single object, for example, to avoid name collisions. But on Valibot, this must be done with namespace (wildcard) imports, which can be problematic when enforcing a convention. Specifically, it collides with ESLint rules like import/no-namespace. So instead of importing like:

import * as v from "valibot";

I'd prefer to import as:

import v from "valibot";

Or even a named v export would be great, but I'm not sure what the impact on tree shaking could be 🤔

import { v } from "valibot";

I can help with a PR if any of those options make sense 🙂

Cheers!

fabian-hiller commented 3 weeks ago

Hey, thank you for your feedback! Unfortunately, I think both solutions are not tree-shakable, and that is one of the main selling points of this library. Why not disable this rule?

JoseLion commented 2 weeks ago

@fabian-hiller, thanks for looking into it. If neither option is tree-shakable, I agree that adding them is not an option 🙂

The main reason for using the import/no-namespace rule is to enforce consistency across imports, so I'd prefer not to disable a rule that's making my code cleaner, easier to maintain, etc. However, we still have two options:

  1. Use Valibot's named exports, which is perfectly fine to me (though v.* is better 😬).
  2. Or add Valibot to the rule exceptions: "import/no-namespace": ["error", { ignore: ["valibot"] }]

Both options are reasonably valid to me, but I thought this could be solved from Valibot's side instead. Thanks again for taking the time to look into this.

Cheers!

fabian-hiller commented 2 weeks ago

I did a first test and it broke tree shakability when the named v export is imported and used to create a simple string schema. So adding Valibot to the rule exceptions is probably the best solution.