rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Avoid outputting an empty object for nested module with external bindings

Open nojaf opened this issue 1 year ago • 4 comments

Hi there,

Could it be possible to not output any JS code when doing external only bindings inside a nested module. Example

type any

type listener

type eventTarget = {}

module EventTarget = {
  @send
  external addEventListener: (eventTarget, string, listener) => unit =
    "addEventListener"
}

leads to

var $$EventTarget = {};

export {
  $$EventTarget ,
}

The existence of $$EventTarget seems unnecessary. Could this be possible?

If so, and this is deemed acceptable I'd be happy to try and contribute this if I get some pointers.

nojaf avatar Nov 02 '24 20:11 nojaf

I think that makes sense, IIRC I started looking at this at some point... I don't immediately see a problem with not emitting empty modules at all, regardless of whether they're empty because of externals or not. @cristianoc any thoughts?

zth avatar Nov 02 '24 20:11 zth

Never emitting an empty module is a bit like never emitting an empty list.

cristianoc avatar Nov 03 '24 06:11 cristianoc

Never emitting an empty module is a bit like never emitting an empty list.

Not sure if I follow, an empty list could have meaning where an empty module has none after compilation right?

nojaf avatar Nov 05 '24 07:11 nojaf

Already requested this six years ago, this was Bob's response at the time: https://github.com/rescript-lang/rescript/issues/3498#issuecomment-483897516 🙂

I think the problem is that as that module is exported, code in another module could in theory use it (as input to a functor or whatever, although that doesn't make much sense ofc).

cknitt avatar May 10 '25 17:05 cknitt