Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR dependency optimizer is only invoked once #14896

Closed
7 tasks done
alan-agius4 opened this issue Nov 7, 2023 · 1 comment
Closed
7 tasks done

SSR dependency optimizer is only invoked once #14896

alan-agius4 opened this issue Nov 7, 2023 · 1 comment

Comments

@alan-agius4
Copy link

alan-agius4 commented Nov 7, 2023

Describe the bug

Currently, the SSR dep optimizer is only invoked the first time ssrLoadModule is invoked. Subsequent calls will noop the dep optimization process even when additional dependencies have been push to the ssr.optimizeDeps.include array.

The problem appears to be caused by the below check, which removing it causes the optimizer to run and transform the dependency correctly.

if (getDepsOptimizer(config, true)) {
// ssr
return
}

Reproduction

https://github.com/alan-agius4/vite-dep-optimizer-ssr-once

Steps to reproduce

npm install
npm run dev

System Info

System:
    OS: Linux 6.5 Debian GNU/Linux rodete
    CPU: (24) x64 Intel(R) Xeon(R) CPU @ 2.20GHz
    Memory: 84.27 GB / 94.29 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.0/bin/yarn
    npm: 9.6.7 - ~/.nvm/versions/node/v18.16.0/bin/npm
    pnpm: 8.10.0 - ~/.nvm/versions/node/v18.16.0/bin/pnpm
  Browsers:
    Chrome: 119.0.6045.105
  npmPackages:
    vite: ^4.4.11 => 4.5.0 

Used Package Manager

npm

Logs

Expected

$ node server.js
cjs-dep-one/index.js
{ cjsDepOne: 'bar' }
performing file update in 10 seconds.
/cjs-dep-one/index.js
cjs-dep-two/index.js
8:52:11 AM [vite] page reload src/entry-server.mjs
{ cjsDepOne: 'bar', cjsDepTwo: 'bar' }

Actual

8:58:08 AM [vite] Error when evaluating SSR module /cjs-dep-two/index.js:
|- ReferenceError: exports is not defined
    at eval (//cjs-dep-two/index.js:3:14)
    at instantiateModule (file:////node_modules/vite/dist/node/chunks/dep-bb8a8339.js:56052:15)

8:58:08 AM [vite] Error when evaluating SSR module ./src/entry-server.mjs: failed to import "/cjs-dep-two/index.js"
|- ReferenceError: exports is not defined
    at eval (//cjs-dep-two/index.js:3:14)
    at instantiateModule (file:////node_modules/vite/dist/node/chunks/dep-bb8a8339.js:56052:15)

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

ReferenceError: exports is not defined
    at eval (//cjs-dep-two/index.js:3:14)
    at instantiateModule (file:////node_modules/vite/dist/node/chunks/dep-bb8a8339.js:56052:15)

Validations

@sapphi-red
Copy link
Member

This was intentionally set because there was not way to restart the module in SSR (like you can reload the window in browsers).
In Vite v6 beta, we made optimizeDeps.noDiscovery: false to opt-in this behavior (#18358). But this requires the framework author (the user calling runner.import) to handle the restart.

I'd be great if you can try it out and leave a feedback at #16358.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants