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.optimizeDeps.include with dep1 > dep2 syntax not working #16405

Open
7 tasks done
hi-ogawa opened this issue Apr 12, 2024 · 1 comment · May be fixed by #16408
Open
7 tasks done

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

hi-ogawa opened this issue Apr 12, 2024 · 1 comment · May be fixed by #16408

Comments

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Apr 12, 2024

Describe the bug

Spinned off from the comment here by @frandiox #16293 (comment), 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
$ 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
Copy link
Collaborator Author

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/[email protected]/node_modules/cookie/index.js?__vite_skip_optimization'

const resolve = createOptimizeDepsIncludeResolver(config, ssr)
for (const id of includes) {
// normalize 'foo >bar` as 'foo > bar' to prevent same id being added
// and for pretty printing
const normalizedId = normalizeId(id)
if (!deps[normalizedId]) {
const entry = await resolve(id)
if (entry) {
if (isOptimizable(entry, optimizeDeps)) {
if (!entry.endsWith('?__vite_skip_optimization')) {
deps[normalizedId] = entry
}
} else {
unableToOptimize(id, 'Cannot optimize dependency')

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

const skipOptimization =
(!options.ssrOptimizeCheck && depsOptimizer?.options.noDiscovery) ||
!isJsType ||
(importer && isInNodeModules(importer)) ||
exclude?.includes(pkgId) ||
exclude?.includes(id) ||
SPECIAL_QUERY_RE.test(resolved) ||
// During dev SSR, we don't have a way to reload the module graph if
// a non-optimized dep is found. So we need to skip optimization here.
// The only optimized deps are the ones explicitly listed in the config.
(!options.ssrOptimizeCheck && !isBuild && ssr) ||
// Only optimize non-external CJS deps during SSR by default
(ssr &&
isFilePathESM(resolved, options.packageCache) &&
!(include?.includes(pkgId) || include?.includes(id)))
if (options.ssrOptimizeCheck) {
return {
id: skipOptimization
? injectQuery(resolved, `__vite_skip_optimization`)
: resolved,
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant