vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
65.97k stars 5.88k forks source link

`ssr.optimizeDeps.include` with `dep1 > dep2` syntax not working #16405

Open hi-ogawa opened 2 months ago

hi-ogawa commented 2 months ago

Describe the bug

Spinned off from the comment here by @frandiox https://github.com/vitejs/vite/issues/16293#issuecomment-2049573464, it looks like ssr.optimizeDeps.include: ['dep1 > dep2'] is not working.

For example, when adding @remix-run/server-runtime > cookie, the deps optimizer seems to reject it by:

Cannot optimize dependency: @remix-run/server-runtime > cookie, present in 'ssr.optimizeDeps.include'

Reproduction

https://github.com/hi-ogawa/reproductions/tree/main/vite-ssr-optimizedeps-include-deep

https://stackblitz.com/github/hi-ogawa/reproductions/tree/main/vite-ssr-optimizedeps-include-deep

Steps to reproduce

npm i 
node repro.mjs
Show error logs ```sh $ node repro.mjs Forced re-optimization of dependencies Forced re-optimization of dependencies Cannot optimize dependency: @remix-run/server-runtime > cookie, present in 'ssr.optimizeDeps.include' 11:57:10 AM [vite] Error when evaluating SSR module /node_modules/cookie/index.js?v=e9d0870e: |- ReferenceError: exports is not defined at eval (/home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/cookie/index.js?v=e9d0870e:17:1) at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55071:15) 11:57:10 AM [vite] Error when evaluating SSR module /node_modules/@remix-run/server-runtime/dist/esm/cookies.js?v=e9d0870e: failed to import "/node_modules/cookie/index.js?v=e9d0870e" |- ReferenceError: exports is not defined at eval (/home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/cookie/index.js?v=e9d0870e:17:1) at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55071:15) 11:57:10 AM [vite] Error when evaluating SSR module /node_modules/@remix-run/server-runtime/dist/esm/index.js?v=e9d0870e: failed to import "/node_modules/@remix-run/server-runtime/dist/esm/cookies.js?v=e9d0870e" |- ReferenceError: exports is not defined at eval (/home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/cookie/index.js?v=e9d0870e:17:1) at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55071:15) 11:57:10 AM [vite] Error when evaluating SSR module /repro-entry.ts: failed to import "/node_modules/@remix-run/server-runtime/dist/esm/index.js?v=e9d0870e" |- ReferenceError: exports is not defined at eval (/home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/cookie/index.js?v=e9d0870e:17:1) at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55071:15) ReferenceError: exports is not defined at eval (/home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/cookie/index.js?v=e9d0870e:17:1) at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-ssr-optimizedeps-include-deep/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:55071:15) ```

In this reproduction, I have noExternal: true to see the clear error. When removing noExternal: true, cookie is externalized, so there's no error, but the same deps optimizer logs come up:

Cannot optimize dependency: @remix-run/server-runtime > cookie, present in 'ssr.optimizeDeps.include'

System Info

System:
    OS: Linux 6.8 Arch Linux
    CPU: (12) x64 AMD Ryzen 5 5625U with Radeon Graphics
    Memory: 7.31 GB / 14.98 GB
    Container: Yes
    Shell: 5.2.26 - /bin/bash
  Binaries:
    Node: 20.12.2 - ~/.volta/tools/image/node/20.12.2/bin/node
    Yarn: 1.22.19 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 10.5.0 - ~/.volta/tools/image/node/20.12.2/bin/npm
    pnpm: 8.12.0 - ~/.volta/bin/pnpm
    bun: 1.0.26 - ~/.volta/bin/bun
  Browsers:
    Chromium: 123.0.6312.105
  npmPackages:
    vite: 5.2.8 => 5.2.8

Used Package Manager

npm

Logs

No response

Validations

hi-ogawa commented 2 months ago

When I debug this locally, it looks like createOptimizeDepsIncludeResolver is returning with __vite_skip_optimization:

id: '@remix-run/server-runtime > cookie',
entry: '/home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/cookie@0.6.0/node_modules/cookie/index.js?__vite_skip_optimization'

https://github.com/vitejs/vite/blob/f8e0791e3f7c7c39c041a563e77396eca706d05e/packages/vite/src/node/optimizer/index.ts#L843-L856

which in turn is coming from the condition isInNodeModules(importer) = true where importer is @remix-run/server-runtime:

https://github.com/vitejs/vite/blob/f8e0791e3f7c7c39c041a563e77396eca706d05e/packages/vite/src/node/plugins/resolve.ts#L868-L890