Skip to content

how to resolve absolute imports? #1017

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

Closed
eddjnr opened this issue Jan 13, 2023 · 7 comments
Closed

how to resolve absolute imports? #1017

eddjnr opened this issue Jan 13, 2023 · 7 comments

Comments

@eddjnr
Copy link

eddjnr commented Jan 13, 2023

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
Copy link
Collaborator

rschristian commented Jan 13, 2023

Are you setting the --alias flag?

Edit: You could do something like this I believe:

$ ROOT=$PWD microbundle build ... --alias @=$ROOT

@eddjnr
Copy link
Author

eddjnr commented Jan 13, 2023

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
Copy link
Collaborator

It'd help if you could provide a reproduction.

@eddjnr
Copy link
Author

eddjnr commented Jan 13, 2023

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
Copy link
Collaborator

rschristian commented Jan 13, 2023

So there's a few issues here:

  • You've swapped $pwd for the recommended $PWD
    • These are different things! Environment Variables are case sensitive. Swap that to $PWD please and it'll work.
    • As $pwd is non-existent, your alias resolves to <empty>/src, aka, /src. This is what your error messages were trying to tell you.
  • TS is it's own beast
  • You're not setting the JSX transform
    • You haven't ran into this one yet, but Microbundle defaults to h as the JSX pragma. As you're using React, and I don't spot any import React from 'react' statements, I'm guessing you're intending to use the runtime JSX transform. If so, you'll want to append the following to your build command: --jsxImportSource react. This will ensure your JSX is properly transformed to the target you intend.

Hope this helps!

@eddjnr
Copy link
Author

eddjnr commented Jan 13, 2023

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.

@eddjnr eddjnr closed this as completed Jan 13, 2023
@rschristian
Copy link
Collaborator

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

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

2 participants