ioBroker.js-controller icon indicating copy to clipboard operation
ioBroker.js-controller copied to clipboard

Compact Mode should be supported for npm modules wich have type 'module'

Open jogibear9988 opened this issue 2 years ago • 9 comments

When a NPM Package (a iobroker Adapter) now uses full ES6, then it could not use the compact mode code :

  if (require.main !== module) {
      // Export the constructor in compact mode
      /**
       * @param {Partial<utils.AdapterOptions>} [options={}]
       */
      module.exports = (options) => new WebUi(options);
  } else {
      // otherwise start the instance directly
      new WebUi();
  }

cause require is not defined.

Maybe a ES6 package should always export it's adapter class, and never instantiate it. And the Controller then imports it and creates the instance (as compact or as extra process)

like this maybe?

  export default WebUi

jogibear9988 avatar Jan 12 '22 08:01 jogibear9988

We need something backward comatible ... so if then we would need to detect an instance vs "Just the exported class"

Apollon77 avatar Jan 12 '22 09:01 Apollon77

What needs to be backward compatible? The Adapter or the controller? The controller could look for the "type" field in the package.json. And the Adapter could only work with new controllers wich support this.

jogibear9988 avatar Jan 12 '22 09:01 jogibear9988

Like I described in Telegram/Discord: require-ing ES modules is not possible, meaning the controller would have to be migrated to ESM first. But existing adapters require parts of the controller (at least adapter and tools, which are used everywhere inside the controller itself).

The only practical way to solve this is by turning the relevant JS-Controller parts into ESM/CommonJS hybrid packages. And the only practical way to solve that IMO is by converting the controller parts to TypeScript that compiles to ESM and using something like https://github.com/AlCalzone/esm2cjs/ to convert that to CommonJS in a separate step.

We also need to exclude Node.js versions that don't support the exports fields in package.json without which loading the hybrid packages doesn't work.

Related: https://github.com/ioBroker/ioBroker.js-controller/issues/1379

AlCalzone avatar Jan 12 '22 09:01 AlCalzone

Side note: I don't think "next version" is a good target for this mammoth project 🙈 @Apollon77

AlCalzone avatar Jan 12 '22 09:01 AlCalzone

Already removed it :-) I would put the "prepare" ticket into backlog for more like" plan a road to it ... or not" :-)

Apollon77 avatar Jan 12 '22 09:01 Apollon77

related to #1379

foxriver76 avatar Jan 12 '22 12:01 foxriver76

related to #1379

Indeed image

🤣

AlCalzone avatar Jan 12 '22 12:01 AlCalzone

related to #1379

Indeed image

🤣

Oops ;-)

Doppelt hält besser

foxriver76 avatar Jan 12 '22 12:01 foxriver76

What seems to be very hard on a first glance is the decaching done at https://github.com/ioBroker/ioBroker.js-controller/blob/5c68c593a86f333597a4506cf6e8c9c301a8ebe0/packages/controller/src/main.ts#L4708 it seems like there is no possibility currently to decache an imported ESM module.

foxriver76 avatar Jan 03 '24 07:01 foxriver76

Go to would be to create a dummy adapter with ESM support and then see if it generally starts and then check if we can do something about compact mode.

foxriver76 avatar Apr 01 '24 09:04 foxriver76

If you need a sample, my adapter here: https://github.com/iobroker-community-adapters/ioBroker.webui/blob/master/dist/backend/main.js is a ESM one

jogibear9988 avatar Apr 01 '24 11:04 jogibear9988

Thanks. And generally is working correctly with controller just compact mode is not working?

foxriver76 avatar Apr 01 '24 11:04 foxriver76

@jogibear9988

If you need a sample, my adapter here: https://github.com/iobroker-community-adapters/ioBroker.webui/blob/master/dist/backend/main.js is a ESM one

If compact does not work please set compact to false at io-package!

mcm1957 avatar Apr 01 '24 11:04 mcm1957

Thanks. And generally is working correctly with controller just compact mode is not working?

Think so. But my adapter does not use many features, but I think it's workin.

jogibear9988 avatar Apr 01 '24 19:04 jogibear9988