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

Glob-style Imports in .tsx Files Not Bundled Correctly #3319

Closed
vinsonchuong opened this issue Aug 17, 2023 · 2 comments
Closed

Glob-style Imports in .tsx Files Not Bundled Correctly #3319

vinsonchuong opened this issue Aug 17, 2023 · 2 comments

Comments

@vinsonchuong
Copy link

I have a function within a .tsx file that looks something like:

const components = new Map<string, ComponentType>();
function getComponent(type: string) {
  const Component =
    components.get(type) ??
    lazy(async () => {
      try {
        return await import(`./components/${type}.tsx`);
      } catch {
        return { default: () => null };
      }
    });
  components.set(type, Component);
  return Component;
}

When bundled, I see that the import is replaced by a call to globImport_..., but that variable is not defined anywhere in the bundle.

If I move the above function into a .js file, it works correctly.

@2767mr
Copy link

2767mr commented Aug 30, 2023

This also does not work for regular typescript files.
Playground link. Notice that adding --loader:.ts=js will cause it to work at the cost of type support.

As a workaround it is possible to put the import in a .js file and export a wrapper. Example

@Fuzzyma
Copy link

Fuzzyma commented Sep 15, 2023

I have the exact same problem. I converted by js file to ts and the import stopped working. I assume it has something to do with esbuild not knowing if the import is a type import or an actual import (because it cant statically analyze it) when loading from a ts file (loading typescript files from a js file is fine because you cant ever import types into js)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants