vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Dependencies are not inlined when using ssr.noExternal with globs

Open brandonroberts opened this issue 1 year ago • 6 comments

Describe the bug

The docs state that anything in the ssr.noExternal array in the Vite config is automatically added to the test.server.deps.inline array.

Globs do not seem to be supported.

Repo: https://github.com/eduardoRoth/analog-ionic-capacitor-template

Reproduction

  1. Clone the repo - https://github.com/eduardoRoth/analog-ionic-capacitor-template
  2. Install the dependencies - npm i
  3. Go into the vite.config.ts
  4. Remove @ionic/angular from the test.server.deps.inline array. @ionic/** is already in the ssr.noExternal array.
  5. Run npx ng test
  6. Note the error, stop the tests
  7. Go into the ssr.noExternal array
  8. Add @ionic/angular
  9. Run npx ng test
  10. Note the tests pass

System Info

System:
    OS: macOS 12.6.8
    CPU: (10) arm64 Apple M1 Pro
    Memory: 231.17 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v18.19.1/bin/npm
    pnpm: 9.3.0 - ~/Library/pnpm/pnpm
    bun: 1.0.35 - ~/.bun/bin/bun
  Browsers:
    Chrome: 126.0.6478.61
    Safari: 15.6.1
  npmPackages:
    vite: ~5.2.11 => 5.2.13 
    vitest: ^1.3.1 => 1.6.0

Used Package Manager

npm

Validations

brandonroberts avatar Jun 17 '24 20:06 brandonroberts

Indeed, glob doesn't seem to be supported for server.deps.inline.

Vite uses createFilter utility https://github.com/vitejs/vite/blob/ec16a5efc04d8ab50301d184c20e7bd0c8d8f6a2/packages/vite/src/node/ssr/ssrExternal.ts#L38-L41 to support glob, but Vitest (vite-node) has a different logic to match patterns https://github.com/vitest-dev/vitest/blob/f969fb0f9f0247a7daa2afee8f70de25ea5e123f/packages/vite-node/src/externalize.ts#L157-L162

I created a repro with simpler dependencies https://github.com/hi-ogawa/reproductions/tree/main/vitest-5911-deps-inline-glob

hi-ogawa avatar Jun 18 '24 03:06 hi-ogawa

Thanks @hi-ogawa. I can work on a PR to support this behavior if that's whats recommended

brandonroberts avatar Jun 18 '24 15:06 brandonroberts

vite-node processes ssr.noExternal on resolved file paths, Vite processes it on bare imports, so we cannot apply the same filter.

sheremet-va avatar Jun 18 '24 15:06 sheremet-va

vite-node processes ssr.noExternal on resolved file paths, Vite processes it on bare imports, so we cannot apply the same filter.

Ok. What would be the recommended approach with globs?

brandonroberts avatar Jun 18 '24 19:06 brandonroberts

Ok. What would be the recommended approach with globs?

If I knew what to do with this, we would've already implemented it 😞

I don't know how to solve these, they were always resolved differently in Vite and Vitest. Vite can also resolve monorepo packages while Vitest expects that the package is in moduleDirectories and the name and the folder are the same.

If it's fine to keep this difference, we can just apply the glob with the same rule - svelte/* will be equal to node_modules/svelte/.+

sheremet-va avatar Jun 23 '24 15:06 sheremet-va

Thanks @sheremet-va. I think it's fine to keep this difference, and note that globs are not expected to be inlined also

brandonroberts avatar Jun 24 '24 14:06 brandonroberts

Vitest 4 actually supports globs by turning them to regexp.

sheremet-va avatar Nov 06 '25 14:11 sheremet-va