vitest icon indicating copy to clipboard operation
vitest copied to clipboard

documentation for `server.deps.fallbackCJS` is confusing

Open dctalbot opened this issue 2 years ago • 4 comments

Describe the bug

In the main documentation:

When a dependency is a valid ESM package, try to guess the cjs version based on the path. This might be helpful, if a dependency has the wrong ESM file.

I'm not sure what this means. If a dependency is valid ESM, why would it look for a CJS version? What does it mean to "have the wrong ESM file"?

The IDE documentation:

Try to guess the CJS version of a package when it's invalid ESM

This seems to contradict the previous statement. Does this setting take affect when an ESM dependency is valid or invalid?

Reproduction

View the documentation

System Info

N/A

Used Package Manager

npm

Validations

dctalbot avatar Aug 16 '24 05:08 dctalbot

Asking because I'm having an issue with vitest-fail-on-console, and it's unclear to me what the expected behavior of vitest is.

dctalbot avatar Aug 16 '24 05:08 dctalbot

When a dependency is a valid ESM package, try to guess the cjs version based on the path. This might be helpful, if a dependency has the wrong ESM file.

I think the only way to correctly describe what this does is to understand this code https://github.com/vitest-dev/vitest/blob/85fb94a3081558b01f47bd763ccdaeb5df1b98cb/packages/vite-node/src/externalize.ts#L29-L54

And probably it's a typo of "invalid ESM package", but note that this fallback guessing is only a heuristics, so we cannot really say what is "invalid ESM package" or not.

I think nowadays fallbackCJS is probably not necessary for many cases. If you need to workaround wrong esm packaging issues, then you might be able to target it better with server.deps.inline https://vitest.dev/config/#server-deps-inline


Regarding your main issue on https://github.com/thomasbrodusch/vitest-fail-on-console/issues/59, I think their packaging is not ideal and goes wrong with fallbackCJS: true unfortunately. You can take a look at this https://publint.dev/[email protected] and they are shipping both ./dist/vitest-fail-on-console.es.js and ./dist/vitest-fail-on-console.umd.js with "type": "module". Since Vitest only runs esm in the test environment (doesn't use require), I think they can only ship something like:

  "exports": {
    ".": {
      "type": "./dist/index.d.ts",
      "default": "./dist/vitest-fail-on-console.es.js"
    }
  }

hi-ogawa avatar Aug 17 '24 00:08 hi-ogawa

Hi! I'd like to try working on this issue if possible. Would it be okay to assign it to me? Thank you!

KyeongJooni avatar Dec 12 '25 05:12 KyeongJooni

Hi! I’d like to work on clarifying the server.deps.fallbackCJS docs for both v3.vitest.dev and the current config docs. I’m planning to align the text with the actual guessCJSversion behavior (path-based heuristic for CJS/UMD builds) and remove the confusing “valid/invalid ESM” wording. If this sounds good, I’ll open a small docs-only PR. Thanks!

gilverse-icn avatar Jan 17 '26 07:01 gilverse-icn