Closed brianlenz closed 9 months ago
I figured out the issue. It was a misconfiguration. The key was to not use references
in tsconfig.json
when using Next.js and instead use compilerOptions.paths
to map them. Then, it seems transpilePackages
works, along with hot reloading.
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Link to the code that reproduces this issue
https://github.com/throughtheword/next-workspace-test
To Reproduce
After cloning repository:
yarn install
cd web-common
yarn build
oryarn dev
If, before step 3, you run
yarn tscompile
(which just does atsc -b
inweb-common
), that will fail due tonext-env.d.ts
not being present, so it can't resolve the CSS module import in theSharedComponent.tsx
:If you run
yarn tscompile
afteryarn build
, then it will work since thenext-env.d.ts
file exists. If you runyarn build
afteryarn tscompile
, then it will also work sincetsc
will build the referencedcommon
dependency.This is somewhat similar to this discussion: https://github.com/vercel/next.js/discussions/47010 It's a bit of a chicken-and-egg problem: does
tsc
come first ornext build
? Both fail when run first.The key, however, is that for some reason, Next.js doesn't honor
transpilePackages
. So, even ifnext-env.d.ts
always exists (to work around theyarn tscompile
chicken-and-egg problem), when doingyarn dev
, hot reloading of files in thecommon
package are not picked up (unsurprisingly since Next.js seems to know nothing about that package for some reason). The reference andtranspilePackages
must work to provide the optimal developer experience.Note that it doesn't make a difference whether you use
nodeLinker: node_modules
in.yarnrc.yml
or if you remove it and use Yarn Plug'n'Play; the same issue occurs either way.Current vs. Expected behavior
Current Behavior
Build errors out during type checking due to
@test/common
package not being transpiled, even though configured throughtranspilePackages
innext.config.js
:Expected Behavior
@test/common
package located in thecommon
directory should be transpiled before the Next.js build so that the@test/common
package can be found.Provide environment information
Which area(s) are affected? (Select all that apply)
TypeScript (plugin, built-in types)
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local)
Additional context
I've been trying to fix/workaround this issue for many hours. I've done a lot of research and haven't found any similarly reported issues in GitHub. I don't know if there's something I'm doing wrong or what, but it seems like a pretty simple example that should work. I originally discovered the issue when building a new app with
14.1.0
. I tried reverting to13.1.0
when Module Transpilation was first introduced, and it has the same issue. I've confirmed the issue still exists with the latest canary build.It's also worth noting that even if you enable eslint.ignoreDuringBuilds and typescript.ignoreBuildErrors (to skip the initial lint and type checking), the build will still fail:
FWIW, I've also tested with next-transpile-modules, and it seems to have the same issue? Is there something about this simple setup that isn't supported? 🤯