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

fix: apply Vite resolving algorithm to node_modules libraries #1673

Merged

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Jul 19, 2022

This PR adds experimental loader and conditions, so native Node resolving algorithm knows about Vite's resolving algorithm.

This is a giant breaking change, but still should not affect a lot of people. This fixes issues when people applied custom conditions and they were working in Vite, but didn't work in Vitest, because it was imported from another library. This PR also allows applying alias to external libraries - this is useful for preact, for example.

We still don't allow mocking external libraries within another external library!

Closes #1638

@sheremet-va sheremet-va force-pushed the fix/aliasing-works-inside-node_modules branch from 121bce4 to 77d5c4b Compare July 24, 2022 07:19
@sheremet-va sheremet-va requested a review from antfu July 24, 2022 07:22
@sheremet-va sheremet-va marked this pull request as ready for review July 24, 2022 07:22
@antfu
Copy link
Member

antfu commented Jul 24, 2022

Can we have a flag to toggle this behavior?

@sheremet-va
Copy link
Member Author

Can we have a flag to toggle this behavior?

How would you call it? I personally don't think it should be toggable. From my experience with Vitest issues for the past several months, is that people expect aliasing to work as it works in Jest (also inside node_modules)

@antfu
Copy link
Member

antfu commented Jul 24, 2022

Ok, we could try and see how it works

@sheremet-va sheremet-va merged commit b971ede into vitest-dev:main Jul 26, 2022
@sheremet-va sheremet-va deleted the fix/aliasing-works-inside-node_modules branch July 26, 2022 08:51
@thebuilder
Copy link

After upgrading to v0.20.1, most of my tests are now failing with:

SyntaxError: Named export '__assign' not found. The requested module 'tslib' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'tslib';
const { __assign } = pkg;

It seems to be caused by framer-motion using tslib. The package does include an exports field and module support, so a bit weird why this is popping up. Thinking it's related to how node modules are resolved now?

@sheremet-va
Copy link
Member Author

After upgrading to v0.20.1, most of my tests are now failing with:

SyntaxError: Named export '__assign' not found. The requested module 'tslib' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'tslib';
const { __assign } = pkg;

It seems to be caused by framer-motion using tslib. The package does include an exports field and module support, so a bit weird why this is popping up. Thinking it's related to how node modules are resolved now?

Please, open a separate issue with minimal reproduction. Thank you! 😄

@cameronbraid
Copy link

This change breaks projects using yarn pnp

0.19.1 works, when upgrading to 0.20 I get

Serialized Error: {
  "errors": [
    [Error: Cannot find module '/workspace/octane/.yarn/__virtual__/vitest-virtual-b7339852d0/0/cache/vitest-npm-0.20.2-9730ec2bf1-eea156689f.zip/node_modules/vitest/dist/loader.mjs' imported from /workspace/octane/
Did you mean to import /workspace/octane/.yarn/__virtual__/vitest-virtual-b7339852d0/0/cache/vitest-npm-0.20.2-9730ec2bf1-eea156689f.zip/node_modules/vitest/dist/loader.mjs?],
  ],
}

@cameronbraid
Copy link

Looks like its already reported #1758

@vitest-dev vitest-dev locked as resolved and limited conversation to collaborators Aug 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unable to alias modules using Vitest/Vite
4 participants