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

non-ts package referencing ts package causes error (dev mode) #484

Open
iamyoki opened this issue Jul 19, 2022 · 2 comments
Open

non-ts package referencing ts package causes error (dev mode) #484

iamyoki opened this issue Jul 19, 2022 · 2 comments

Comments

@iamyoki
Copy link

iamyoki commented Jul 19, 2022

  1. I have a monorepo structure like this
create-react-app
└── src
    ├── ...
    └── index.jsx
packages
└── shared
    └── src
        ├── index.ts
        └── helpers.ts
  1. shared is a dependency of create-react-app

shared/src/index.ts

export * from "./helpers"

create-react-app/src/index.jsx

import {xxxx} from "shared"
  1. Error cause (Simplified)
Module not found: Error: Can't resolve './helpers' in 'shared/src'
  1. Situations (✅no cause, ❌cause error)
  • ✅ Both packages are ts based
  • ✅ Both packages are js based
  • ❌ One js package referecing ts package, which ts package have internal relative imports.
  1. My guess
    preconstruct dev mode creates symlink for module so that if dependent cannot resolve ts file then error causes.
@emmatown
Copy link
Member

emmatown commented Jul 21, 2022

From trying things myself, it looks like this issue is more "preconstruct dev doesn't work with create-react-app". I'm guessing you'll need to use one of the forks of react-scripts and make it compile files outside of the create-react-app project as well as what it already does compile (though only outside the directory and not in node_modules).

@iamyoki
Copy link
Author

iamyoki commented Jul 21, 2022

@mitchellhamilton Thank you for your answer~
I find out that the problem was caused because of non-ts create-react-app cannot compile a ts file from dependency.
In dev mode, the dist file xxx.esm.js is basically a symlink to source file, so that non-ts CRA compiler can't work on that.

My temp solution (turn on ts for CRA):

  1. Add @craco/craco and ts-loader and typescript
  2. Config ts-loader as a webpack loader
  3. Add a tsconfig.json at root

But that's not perfect, I have an idea, use a hook loader or something to pre compile the symlink dist file, like what did on cjs file, is that possible?

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