Open wojtekmaj opened 1 year ago
Is there any update about this?
In my case, I was updating to React PDF v7 and ran into this issue. What I ended up doing was including the transitive dependency / 3rd party module in our Vite config to also optimize it when using the ?url
suffix. Worked like a charm!
export default defineConfig({
...
optimizeDeps: { include: ['pdfjs-dist'] },
})
I dived a bit into the bug and the problem is in this line: https://github.com/vitejs/vite/blob/e526573cae8a2fb073bf9a38bef17aeb9adbebc3/packages/vite/src/node/plugins/workerImportMetaUrl.ts#L154
If the file system resolve fails here, we just stay on the artificially produced path like: 'D:\Dev\myproj\src\vite-react\node_modules\.vite\deps\mylib.worker.mjs'
. This leads to two problems:
.vite\deps
https://github.com/vitejs/vite/blob/f8e0791e3f7c7c39c041a563e77396eca706d05e/packages/vite/src/node/optimizer/index.ts#L912-L921Hence a fix would mean: resolve at this location the correct path to the worker file of the 3rd party module in the node_modules directory.
It seems a code bit like this will work:
file = tryFsResolve(file, fsResolveOptions) ?? tryOptimizedDepResolve(url, id) ?? file;
function tryOptimizedDepResolve(url, depId, fsResolveOptions) {
if (depsOptimizer?.isOptimizedDepFile(depId)) {
const depFile = cleanUrl(depId);
const info = optimizedDepInfoFromFile(depsOptimizer.metadata, depFile);
const depSrc = info.src;
if (depSrc) {
const resolvedFile = path.resolve(path.dirname(depSrc), url)
return tryFsResolve(file, fsResolveOptions);
}
}
return undefined;
}
It is a similar flow like the existing tryOptimizedResolve but simplified.
I'll check if I can make a change to Vite and test it locally.
I prepared a fix which works nicely for my library at https://github.com/vitejs/vite/pull/16418 Would be great if some more people could give it a test. To test it locally:
pnpm install && pnpm build
)Running into the same issue, would love to see this fixed..
Describe the bug
Getting module URL in our own code like so:
works fine, but if that very same line is placed in
node_modules/module-1/index.js
, build crashes:At the same time, I was not able to find any mention regarding these suffixed working only on limited subset of modules when reading the following page: https://vitejs.dev/guide/assets.html
Reproduction
https://github.com/wojtekmaj/vite-url-suffix-bug
Steps to reproduce
yarn
yarn copy-packages
(to replace symlinks in node_modules with actual modules, since they come from internal workspace)yarn dev
System Info
Used Package Manager
yarn
Logs
No response
Validations