Open Mathdrquinn opened 1 year ago
TL;DR, adding resolve: { preserveSymlinks: true }
to your Vite configuration appears to resolve the issue.
I've gone down a giant rabbit hole on this issue. This has to do with preserveSymlinks
(which finds its history in the --preserve-symlinks
Node.js flag). No, it's not build.rollupOptions.preserveSymlinks
even though the error states RollupError, but rather resolve.preserveSymlinks
which must override this option for Rollup & the other build tools (see esbuild#preserve-symlinks). Perhaps that could be clarified in the documentation.
Apparently, Node.js doesn't have a very good history with symlinking (see nodejs/node#10107), and so that's what we're dealing with here, I believe. I wish this flag wasn't needed, because apparently there are some nasty side-effects. I'm pretty sure it only happens with CJS, as I originally set up a repro, and when I was using ESM, the symlink seemed to resolve correctly.
Hope this helps. I feel your pain. I ran into this with a simple npm
workspace project.
@jameschensmith Thank you for the swell reply. I'll give it a try and report back.
That does appear to solve my conundrum @jameschensmith . I appreciate you taking the time to give a thoughtful response.
For those finding this later on, I've updated my sandbox app to demonstrate the solution: https://stackblitz.com/edit/vitejs-vite-3mgrsj?file=apps%2Fapp1%2Fvite.config.js
Another solution is to use https://pnpm.io/package_json#dependenciesmetainjected, see: https://stackblitz.com/edit/vitejs-vite-nzqcwp?file=apps/app1/package.json
@lsdsjy Your solution helped in my case. preserveSymlinks
made a bunch of other packages error out...
However with this change I have to completely nuke node_modules
for vite to pick up changes in the shared package.
In @rollup/plugin-commonjs
, there's this docs:
So include and exclude paths should handle real paths rather than symlinked paths (e.g. ../common/node_modules/ instead of node_modules/).:
So commonjsOptions.include
should be configured with /packages\/util/
instead of /@local-package\/util/
. I think we can improve this in our docs.
Another solution is to use https://pnpm.io/package_json#dependenciesmetainjected, see: https://stackblitz.com/edit/vitejs-vite-nzqcwp?file=apps/app1/package.json
That did the trick for me, thanks!
Describe the bug
Running
vite build
on a pnpm workspace application cannot prebundle and identify local package definitions/Repo structure
app1/vite.config.js
app1/package.json
util/package.json
Version:
"vite": ^4.0.4"
Reproduction
https://stackblitz.com/edit/vitejs-vite-3mgrsj?file=apps/app1/vite.config.js
Steps to reproduce
System Info
Used Package Manager
pnpm
Logs
Validations