oxc-project / oxc-resolver

Rust version of webpack/enhanced-resolve
https://oxc.rs/docs/guide/usage/resolver.html
MIT License
122 stars 18 forks source link

Code in README.md is wrong + possibly unsafe rust/javascript interaction #264

Closed sparecycles closed 5 days ago

sparecycles commented 5 days ago

The README example code under "Cache", reads

To support both CJS and ESM with the same cache:

const esmResolver = ResolverFactory({
  conditionNames: ["node", "import"]
});

const cjsResolver = esmResolver.cloneWithOptions({
  conditionNames: ["node", "require"]
});

I tried running this in node directly, the first call returns the global object (which presumably was partially bound as a ResolverFactory because that's what this was when running the constructor as a function).

> ResolverFactory({ conditionNames: ["node","import"] })
<ref *1> Object [global] {
  global: [Circular *1],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(nodejs.util.promisify.custom)]: [Getter]
  },
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
...

and subsequent calls fail because the rust code is not happy constructing onto an object twice.

> ResolverFactory({ conditionNames: ["node","require"] })
Uncaught:
[Error: Failed to initialize class `constructor`] { code: 'InvalidArg' }

The example should be corrected to be new ResolverFactory(...)? The rust code could also maybe do a better job of determining whether the ResolverFactory was called as a method or constructor.