sheremet-va / vitest-react-native

62 stars 5 forks source link

resolve.conditions doesn't seem to do the trick #11

Open wojtekmaj opened 1 year ago

wojtekmaj commented 1 year ago

When importing recoil package, which clearly has a specified react-native entry point, the following error appears:

Error: Cannot find module 'react-dom'
Require stack:
- /Users/wmaj/Projekty/Rewardo/app-native/node_modules/recoil/cjs/index.js
 ❯ node_modules/recoil/cjs/index.js:8:32
      6| 
      7| var react = _interopDefault(require('react'));
      8| var reactDom = _interopDefault(require('react-dom'));
       |                                ^
      9| 
     10| /**

By adding the following to the config:

  resolve: {
    alias: [{ find: 'recoil', replacement: 'recoil/native/index.js' }],
  },
  test: {
    deps: {
      inline: ['recoil'],
    },
  },

everything works again.

Looking at https://vitejs.dev/config/shared-options.html#resolve-conditions docs, it seems like this would extend the list of conditions, so react-native would still not be prioritized. Or, perhaps, it works for exports only.

sheremet-va commented 1 year ago

Or, perhaps, it works for exports only.

resolve.conditions only works for exports field. If you need top level field, use resolve.mainFields (but it will only work for dependencies imported in inlined code)

wojtekmaj commented 8 months ago

Is there anything stopping us from defining it as

mainFields: ['react-native', 'browser', 'module', 'jsnext:main', 'jsnext'],

in this plugin?

sheremet-va commented 8 months ago

Don't forget that mainFields is only applied to imports in your source code. We would need to implement our own resolution for require. Conditions are supported out of the box. And I don't have the energy and interest to implement and support this myself.