developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8k stars 361 forks source link

how to resolve absolute imports? #1017

Closed JuniorAlbuquerque closed 1 year ago

JuniorAlbuquerque commented 1 year ago

in build the files imported with absolute are being generated like this

import { Colors, ThemeKey } from '@/styles/themes/types';

Any idea how this can be resolved?

rschristian commented 1 year ago

Are you setting the --alias flag?

Edit: You could do something like this I believe:

$ ROOT=$PWD microbundle build ... --alias @=$ROOT
JuniorAlbuquerque commented 1 year ago

Hi, thank's for the response!

I tried to use the alias, but without success, now I get an error when trying to import the file

$ microbundle --no-compress --format modern,cjs --alias @=$pwd/src
Error: Could not load /src/utils/strings
rschristian commented 1 year ago

It'd help if you could provide a reproduction.

JuniorAlbuquerque commented 1 year ago

Thank you very much for your help!

I created a project that reproduces the problem.

https://github.com/JuniorAlbuquerque/microbundle-react-lib-example/blob/main/package.json

I created two scripts,

 yarn build

that generated the dist, but without resolving the absolute import

// Text.types.d.ts
import { SomeType } from "@/types";
export type TextType = 'h1' | 'h2' | 'h3' | 'h4' | 'p' | 'span';
export type TextProps = {
    renderAs: TextType;
    text: SomeType;
};

and the script:

yarn build:alias

with the --alias flag it gives me the import error

Error: Could not load /src/utils/strings (imported by src/components/Text/Text.component.tsx): ENOENT: no such file or directory, open '/src/utils/strings'
rschristian commented 1 year ago

So there's a few issues here:

Hope this helps!

JuniorAlbuquerque commented 1 year ago

Thanks for the help, indeed the $pwd was wrong, and the capitalization worked. About TS, I will test other approaches and the use of rollup-plugin-typescript2. And thanks for pointing out the use of react flags in the script for transform JSX.

Thank you so much, Best regards.

rschristian commented 1 year ago

Good luck! Certainly feel free to comment here if you run into any further issues.