k-g-a / jest-node-exports-resolver

MIT License
25 stars 7 forks source link

Add support for string and nested exports & self-referencing packages #6

Closed piranna closed 3 years ago

GrossDesignCo commented 3 years ago

I just copied this code into a custom resolver & it resolves the packages!

But then Jest seems to crash on the module syntax within the packages' files.

Specifically it gives me ReferenceError: exports is not defined when I run it via NODE_OPTIONS=--experimental-vm-modules npx jest

Tested with several packages in node_modules, some of which use only

"exports": {
  ".": "./dist/esm/index.js",
  "./package.json": "./package.json"
}

and one which uses

"exports": {
  ".": {
    "import": "./dist/esm/index.js",
    "require": "./dist/cjs/index.js",
  "./package.json": "./package.json"
},
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js"

and one which uses

"exports": {
  "import": "./dist/esm/index.js",
  "require": "./dist/cjs/index.js",
},
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js"
piranna commented 3 years ago

Specifically it gives me ReferenceError: exports is not defined when I run it via NODE_OPTIONS=--experimental-vm-modules npx jest

I also use that flag... Maybe you copied wrong the code? Have you tried to use the resolver as a devDependency? So far it works for me in all the projects I've checked it, there's only one that gives me an error about not able to resolve @testing-library/react/package.json but I suspect it's a monorepo, and in any case since I'm not using React it continue and the tests are still passing...

k-g-a commented 3 years ago

Thanks for the PR, merged and published. I've also added you as a collaborator, so it's easier for you to fix things :)

piranna commented 3 years ago

I've also added you as a collaborator, so it's easier for you to fix things :)

Thank you, because I've found another corner case where it doesn't work if package.json don't have a main nor exports has a package-level export.

piranna commented 3 years ago

I've also added you as a collaborator, so it's easier for you to fix things :)

Can you add me permissions to publish on npm? I've fixed the corner case.

k-g-a commented 3 years ago

Sure, whats you npm user name?

piranna commented 3 years ago

piranna

k-g-a commented 3 years ago

done

piranna commented 3 years ago

thank you :-)