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

Support wildcard imports #6

Closed
foges opened this issue Oct 31, 2023 · 4 comments
Closed

Support wildcard imports #6

foges opened this issue Oct 31, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@foges
Copy link

foges commented Oct 31, 2023

Currently you need to specify the alias for each module, which can be quite cumbersome if you have multiple modules. It would be great if you could support wildcard imports. Something as easy as replacing

  const isAliasInSpecifier = (path, alias) => {
    return path.indexOf(alias) === 0
      && (path.length === alias.length || path[alias.length] === '/');
  };

with this works

  const isAliasInSpecifier = (path, alias) => {
    return path.indexOf(alias) === 0
  };
@euberdeveloper
Copy link
Owner

Can you give me an example, in the esm code, of wildcard imports that gives an error with the current solution?

@foges
Copy link
Author

foges commented Oct 31, 2023

My resolve code looks like this

export const resolve = loadAliases({
  "@/": "dist/src/",
})

In my esm code I then have

import client from "@/client"

esm-module-alias then fails to substitute the alias because path = "@/client" and alias = "@/".

I suppose the code works if you have

export const resolve = loadAliases({
  "@": "dist/src/",
})

But from a semantic point of view I don't think that's particularly clean, since I wouldn't want @client to resolve to dist/src/client, which is what it reads like. An alternative could be to change it to

 const isAliasInSpecifier = (path: string, alias: string) => {
    return (
      path.indexOf(alias) === 0 &&
      (path.length === alias.length || path[alias.length] === "/"  || path[alias.length - 1] === "/")
    )
  }

@euberdeveloper euberdeveloper self-assigned this Oct 31, 2023
@euberdeveloper euberdeveloper added the enhancement New feature or request label Oct 31, 2023
@euberdeveloper
Copy link
Owner

The purpose of this lib was having a way to basically keep the same code of a project using module-alias and commonjs but passing to esm. Since this seems to me to be the behaviour of module-alias, I will not change it.

Maybe later I will think about a perfect behaviour for the path-matching-alias part.

What I will probably do now instead is allowing users to customize the matching behaviour, so that you can change the function as you wish.

I'll write a comment here when the feature is in production.

Thanks for the issue,
Eugenio

@euberdeveloper
Copy link
Owner

Fixed by PR #7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

No branches or pull requests

2 participants