You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because I have written some esbuild plugins in the project, I need to keep consistency between the build and dev modes. Therefore, I enabled Optimizing dependencies in build mode.
Actually happen
I executed the command "vite build" some exceptions occurred(The logs section can be seen below), which worked fine in dev mode.
After debugging the vite source code, I found the following:
My dependencies are dynamic import another dependency, but the "optimizedDepInfoFromFile" function cannot find any information and eventually throws 'Something unexpected happened while optimizing' error.
The reason is that the file to be loaded has not been processed by "flattenId," but the files in "depInfoList" have been processed by "flattenId," leading to the inability to locate the file.
In dev mode, when the optimizedDepsPlugin fails to find info, it will fallback and load the file from the cache. However, the optimizedDepsBuildPlugin has a similar logic, but it immediately throws an error before using the fallback logic.
I have found a workaround, which is to add the following configuration in the vite.config.ts file:
exportdefaultdefineConfig({optimizeDeps: {disabled: false,// NOTE: workaround, react-cropper is my nest depsinclude: ['dynamic-import-pkg > react-cropper'],},plugins: [react()],build: {// Avoid @rollup/plugin-commonjscommonjsOptions: {include: [],},},});
By doing this, not only init deps with flattenId, but also inesbuild.contextentryPoints will get the flatId, which allows them to be correctly loaded.
Expect
Using vite build works fine when I enabled Optimizing dependencies in build mode.
Other contexts
I would like to attempt to resolve this issue by aligning the fallback load logic of optimizedDepsBuildPlugin load hook with the logic used in optimizedDepsPlugin.
Besides, I have other doubts (which might belong to a separate discussion on another issue). I noticed that the file field for the chunks in the .vite/deps/_metadata.json is processed with flattenId. However, the chunk files generated by esbuild on the disk do not undergo the flattenId process. Is this behavior expected?
vite v4.4.6 building for production...
Forced re-optimization of dependencies
transforming (1) index.html vite:optimize-deps load /home/projects/vitejs-vite-vttiy9/node_modules/.vite/deps_build-dist/dynamic-import-pkg.js +0ms
✓ 4 modules transformed.
✓ built in 316ms
[vite:optimized-deps-build] Could not load /home/projects/vitejs-vite-vttiy9/node_modules/.vite/deps_build-dist/react-cropper.es-I3LSTXMD.js (imported by node_modules/.vite/deps_build-dist/dynamic-import-pkg.js): Something unexpected happened while optimizing "/home/projects/vitejs-vite-vttiy9/node_modules/.vite/deps_build-dist/react-cropper.es-I3LSTXMD.js".
error during build:
Error: Could not load /home/projects/vitejs-vite-vttiy9/node_modules/.vite/deps_build-dist/react-cropper.es-I3LSTXMD.js (imported by node_modules/.vite/deps_build-dist/dynamic-import-pkg.js): Something unexpected happened while optimizing "/home/projects/vitejs-vite-vttiy9/node_modules/.vite/deps_build-dist/react-cropper.es-I3LSTXMD.js".
at Object.load (file:///home/projects/vitejs-vite-vttiy9/node_modules/.pnpm/vite@4.4.6/node_modules/vite/dist/node/chunks/dep-a8e37fae.js:40461:23)
at eval (file:///home/projects/vitejs-vite-vttiy9/node_modules/.pnpm/rollup@3.26.3/node_modules/rollup/dist/es/shared/node-entry.js:25361:40)
at async PluginDriver.hookFirstAndGetPlugin (file:///home/projects/vitejs-vite-vttiy9/node_modules/.pnpm/rollup@3.26.3/node_modules/rollup/dist/es/shared/node-entry.js:25261:28)
at async eval (file:///home/projects/vitejs-vite-vttiy9/node_modules/.pnpm/rollup@3.26.3/node_modules/rollup/dist/es/shared/node-entry.js:24430:75)
at async Queue.work (file:///home/projects/vitejs-vite-vttiy9/node_modules/.pnpm/rollup@3.26.3/node_modules/rollup/dist/es/shared/node-entry.js:25471:32)
ELIFECYCLE Command failed with exit code 1.
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
Describe the bug
Background
Because I have written some esbuild plugins in the project, I need to keep consistency between the build and dev modes. Therefore, I enabled Optimizing dependencies in build mode.
Actually happen
I executed the command "vite build" some exceptions occurred(The logs section can be seen below), which worked fine in dev mode.
After debugging the vite source code, I found the following:
My dependencies are dynamic import another dependency, but the "optimizedDepInfoFromFile" function cannot find any information and eventually throws 'Something unexpected happened while optimizing' error.
The reason is that the file to be loaded has not been processed by "flattenId," but the files in "depInfoList" have been processed by "flattenId," leading to the inability to locate the file.
In dev mode, when the
optimizedDepsPlugin
fails to find info, it will fallback and load the file from the cache. However, theoptimizedDepsBuildPlugin
has a similar logic, but it immediately throws an error before using the fallback logic.I have found a workaround, which is to add the following configuration in the vite.config.ts file:
By doing this, not only init deps with flattenId, but also in
esbuild.context
entryPoints
will get the flatId, which allows them to be correctly loaded.Expect
Using
vite build
works fine when I enabled Optimizing dependencies in build mode.Other contexts
I would like to attempt to resolve this issue by aligning the fallback load logic of
optimizedDepsBuildPlugin
load hook with the logic used inoptimizedDepsPlugin
.Besides, I have other doubts (which might belong to a separate discussion on another issue). I noticed that the
file
field for thechunks
in the.vite/deps/_metadata.json
is processed withflattenId
. However, the chunk files generated by esbuild on the disk do not undergo theflattenId
process. Is this behavior expected?Reproduction
https://stackblitz.com/edit/vitejs-vite-vttiy9?file=package.json
Steps to reproduce
pnpm i
pnpm run build
Then can see throw Error.
System Info
Used Package Manager
pnpm
Logs
Click to expand!
Validations
The text was updated successfully, but these errors were encountered: