Skip to content

optimizeDeps.include accepts a project source file and pre-bundles it, but nothing ever resolves to it #23430

Description

@Artur-

Describe the bug

Listing a project source file (not in node_modules) in optimizeDeps.include is accepted: the dep optimizer runs on it and writes the pre-bundle into node_modules/.vite/deps, and it shows up in _metadata.json under optimized. But no import ever resolves to it — the file keeps being served as source, module by module — and no warning is printed. The pre-bundle is simply dead output.

The reason looks structural: in vite:resolve (and in vite:pre-alias) the call to tryOptimizedResolve sits behind if (bareImportRE.test(id)), and tryOptimizedResolve matches the optimizer metadata ​by specifier string​, not by resolved file path. A relative import that resolves to the very file listed in include therefore can never hit the optimized dep.

What I'd like clarified: is accepting a source path in optimizeDeps.include intended (in which case relative imports resolving to it arguably ought to be redirected), or is it out of scope (in which case Vite could warn, the way it already warns for include entries it cannot resolve)?

The use case for this is that we have a "library" included in the project as ~ 100 source files. The library code won't change and doesn't need HMR etc support so it would be efficient to pre-bundle it. It is however not an npm package.

Reproduction

https://github.com/Artur-/vite-optimizedeps-repro

Steps to reproduce

npm install && npx vite

System Info

System:
    OS: macOS 26.6.2
    CPU: (18) arm64 Apple M5 Max
    Memory: 666.73 MB / 128.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 26.7.0
    Yarn: 1.22.22
    npm: 11.19.0
    bun: 1.3.13
    Deno: 2.8.2
  Browsers:
    Chrome: 152.0.7977.75
    Firefox: 151.0.2
    Firefox Developer Edition: 153.0
    Safari: 26.6.2
  npmPackages:
    vite: ^7.3.6 => 7.3.6

Used Package Manager

npm

Logs

No response

Validations

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions