graaljs icon indicating copy to clipboard operation
graaljs copied to clipboard

Add support for `package.json` `exports` field

Open sgammon opened this issue 9 months ago • 4 comments

If a module package.json specifies exports, GraalJs will now read them and prefer exports over standard resolution; export types can be registered by the developer as preferred. In lieu of these types (and as a default), the following export types are preferred, in order:

  • graaljs
  • import (in ESM)
  • require
  • default

Fixes and closes oracle/graaljs#903

This is quick and dirty and probably not mergeable as expressed, but we are using it effectively downstream.

cc / @woess

sgammon avatar Mar 24 '25 21:03 sgammon

I don't know how to add tests to GraalJs yet. I'm happy to figure it out and add tests if there is any desire to merge this

sgammon avatar Mar 24 '25 21:03 sgammon

Happy to accept contributions in that area. The commonjs support has been a bit neglected tbh...

We will need some test coverage for it.

I think you can find most of the tests here: graal-js/src/com.oracle.truffle.js.test/src/com/oracle/truffle/js/test/builtins/CommonJSRequireTest.java graal-js/src/com.oracle.truffle.js.test/commonjs graal-js/src/com.oracle.truffle.js.test/src/com/oracle/truffle/js/test/builtins/CommonJSWithCustomFsTest.java

woess avatar Mar 26 '25 18:03 woess

@woess Thanks for the review. I'll take a look at adding some tests

sgammon avatar Mar 26 '25 20:03 sgammon

This is great, especially Conditional exports support. Could also support patterns

  "exports": {
    "./features/*.js": "./src/features/*.js"
  },

for

import featureX from 'es-module-package/features/x.js';

0c3d7r4 avatar Dec 08 '25 04:12 0c3d7r4