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

documentation for server.deps.fallbackCJS is confusing #6352

Open
6 tasks done
dctalbot opened this issue Aug 16, 2024 · 2 comments
Open
6 tasks done

documentation for server.deps.fallbackCJS is confusing #6352

dctalbot opened this issue Aug 16, 2024 · 2 comments

Comments

@dctalbot
Copy link

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
Copy link
Author

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.

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Aug 17, 2024

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

export function guessCJSversion(id: string): string | undefined {
if (id.match(ESM_EXT_RE)) {
for (const i of [
id.replace(ESM_EXT_RE, '.mjs'),
id.replace(ESM_EXT_RE, '.umd.js'),
id.replace(ESM_EXT_RE, '.cjs.js'),
id.replace(ESM_EXT_RE, '.js'),
]) {
if (existsSync(i)) {
return i
}
}
}
if (id.match(ESM_FOLDER_RE)) {
for (const i of [
id.replace(ESM_FOLDER_RE, '/umd/$1'),
id.replace(ESM_FOLDER_RE, '/cjs/$1'),
id.replace(ESM_FOLDER_RE, '/lib/$1'),
id.replace(ESM_FOLDER_RE, '/$1'),
]) {
if (existsSync(i)) {
return i
}
}
}
}

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 thomasbrodusch/vitest-fail-on-console#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/vitest-fail-on-console@0.7.0 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"
    }
  }

thomasbrodusch added a commit to thomasbrodusch/vitest-fail-on-console that referenced this issue Sep 2, 2024
We're 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), we can only ship the default export as `./dist/vitest-fail-on-console.es.js`

Thanks to @hi-ogawa - vitest-dev/vitest#6352 (comment)
thomasbrodusch added a commit to thomasbrodusch/vitest-fail-on-console that referenced this issue Sep 2, 2024
We're 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), we can only ship the default export as `./dist/vitest-fail-on-console.es.js`

Thanks to @hi-ogawa - vitest-dev/vitest#6352 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants