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

Absolute imports / TS Paths aliases get treated as externals #528

Closed
hornetDC opened this issue Mar 2, 2020 · 6 comments · Fixed by #707
Closed

Absolute imports / TS Paths aliases get treated as externals #528

hornetDC opened this issue Mar 2, 2020 · 6 comments · Fixed by #707
Labels
solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: TS Paths Aliases Related to using aliases with TypeScript paths

Comments

@hornetDC
Copy link

hornetDC commented Mar 2, 2020

Hi. I am trying to create a small components lib + styleguidist playground.

Playground works fine (it uses webpack), but my build is broken. I created a dist folder, npm pack-ed it, installed locally in another project.

import { Button } from "components-lib";

Module not found: Can't resolve 'components/Button'

My index.ts looks like this:

export { default as Button } from "components/Button";
export { default as Checkbox } from "components/Checkbox";
export { default as Icon } from "components/Icon";
export { default as SideMenu } from "components/SideMenu";

image

I get no errors during build.

My tsconfig:

{
  "include": ["src", "@types", "test"],
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "lib": ["dom", "esnext"],
    "importHelpers": true,
    "declaration": true,
    "sourceMap": true,
    "rootDir": "./",
    "strict": true,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "*": ["src/*", "node_modules/*"]
    },
    "jsx": "react",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "isolatedModules": true,
  }
}
@hornetDC
Copy link
Author

hornetDC commented Mar 2, 2020

I also don't see any static assets 😔

// tsdx.config.js
const image = require('@rollup/plugin-image');

module.exports = {
  rollup(config) {
    config.plugins = [
      image({ incude: ['**/*.png', '**/*.jpg', '**/*.svg'] }),
      ...config.plugins,
    ];
    return config;
  },
};

@lpolito
Copy link
Contributor

lpolito commented Mar 2, 2020

Module not found: Can't resolve 'components/Button'

This could be related with how externals are configured (which I found recently):
#6 (comment)

  1. Marks all imports as external if they are not relative / are absolute (seen here).

Try using relative imports, e.g.

export { default as Button } from "./components/Button";
export { default as Checkbox } from "./components/Checkbox";
export { default as Icon } from "./components/Icon";
export { default as SideMenu } from "./components/SideMenu";

Same goes for your static assets, assuming you're importing them.

@hornetDC
Copy link
Author

hornetDC commented Mar 3, 2020

Seems to be the case for components, assets (svg) are still missing. Maybe they are included inline in build? Edit: they are

Also how can I fix imports? I tried with aliases and it still didn't work. I don't want to look at '../../'

@lpolito
Copy link
Contributor

lpolito commented Mar 3, 2020

Yeah unfortunately the rollup config has a lot to be desired in that way.

For rollup aliases appear to be possible, see: https://github.com/rollup/plugins/tree/master/packages/alias

@hornetDC
Copy link
Author

hornetDC commented Mar 3, 2020

Many thanks!

@agilgur5
Copy link
Collaborator

agilgur5 commented Mar 4, 2020

Yep as @lpolito said, non-relative imports are treated as externals.

Regarding aliases, you can check out the HOWTO on that at #379 (comment) .

Potentially related, there was a PR for TS paths -> alias support, but the TypeScript team's position is that paths shouldn't be used like that (only for type information and not for aliasing), so it's currently only unofficially supported by various workarounds.
TSDX could have it built-in but I'm not sure that's a rabbit hole worth digging into. There are a lot of issues around it however -- this sounds like a duplicate of #91
Can read more at #374 (comment)

@agilgur5 agilgur5 changed the title [Question] Can't import component Absolute imports / TS Paths aliases get treated as externals Mar 9, 2020
@agilgur5 agilgur5 added the solution: duplicate This issue or pull request already exists label Mar 9, 2020
@agilgur5 agilgur5 closed this as completed Mar 9, 2020
@agilgur5 agilgur5 added the topic: TS Paths Aliases Related to using aliases with TypeScript paths label Mar 10, 2020
@agilgur5 agilgur5 added the solution: workaround available There is a workaround available for this issue label Aug 25, 2020
Repository owner locked as resolved and limited conversation to collaborators Aug 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: TS Paths Aliases Related to using aliases with TypeScript paths
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants