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

dynamic import with vars help #2221

Closed
The-Code-Monkey opened this issue May 3, 2022 · 3 comments
Closed

dynamic import with vars help #2221

The-Code-Monkey opened this issue May 3, 2022 · 3 comments

Comments

@The-Code-Monkey
Copy link

Hi There, loving the speed of esbuild but im having an issue with my dynamic import that has variables in it.

export const getIcon = (name: IconTypes) => {
  return lazy(() => import(`./icons/${name}`));
};

so the icons folder never gets generated in my repo so this fails, which breaks the icon component completely.

Is there any way to get this to import correctly.

@hyrious
Copy link

hyrious commented May 3, 2022

Dynamic import/require calls cannot be statically analysed, so esbuild cannot find corresponding files to bundle them. You can instead manually write them down:

const Icons = {
  name1: () => import('./icons/name1'),
};
export const getIcon = (name: IconTypes) => {
  return lazy(Icons[name]);
};

@The-Code-Monkey
Copy link
Author

@hyrious Yeah problem with that is there is like 300 icons so not really a good thing to do.

i did fix it by doing this

const getEntryPoint = () => new Promise<Array<string>>((resolve) => {
        glob("./src/**/*.ts*x").then(res => resolve(res.filter(filename => !filename.includes("__"))));
    })

    return {
        splitting: true,
        tsconfig: paths.tsconfigJson, // default
        entryPoints: await getEntryPoint(),
        outdir: 'dist',
        format: "esm",
        minify: shouldMinify,
    }

problem is now my .d.ts file arent generating for some reason.

@evanw
Copy link
Owner

evanw commented May 7, 2022

Run-time file system emulation like this is out of scope, which is documented here: https://esbuild.github.io/api/#non-analyzable-imports. Closing this issue as out of scope.

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