atomicojs / atomico

Atomico a micro-library for creating webcomponents using only functions, hooks and virtual-dom.
https://atomicojs.dev
MIT License
1.16k stars 43 forks source link

CommonJS entry #20

Closed dy closed 4 years ago

dy commented 4 years ago

Optional module detection is only possible via require mechanism: https://stackoverflow.com/questions/33987742/optional-imports-in-es2015-node-js That is used by any-hooks and unihooks. The problem is that with ES modules support in node https://nodejs.org/api/esm.html, the atomico exports nothing, being imported as commonjs:

let atomico = require('atomico')
// {} - empty object here

That makes it impossible to test/run in nodejs.

That would be useful to provide a separate commonjs entry, eg as showed here.

dy commented 4 years ago

Actually nvm, ES modules support in node seems to be still not reliable enough.

dy commented 4 years ago

Actually, considering https://github.com/matthewp/haunted/issues/158, maybe you'd also like to provide commonjs support @UpperCod?

UpperCod commented 4 years ago

Good idea, it will be available in the next update 0.16.0, I will comment again when it is available

UpperCod commented 4 years ago

Atomico assumes the system of modules stable and recommended for web-oriented projects. indifferent to the state of this in node.js, since the Atomico target is the browser.

The use of cjs as a module will not be included in the package declaration, but if it exists in the cjs folder of the package, this to support environments based on cjs, but this requires the user to declare atomico/cjs/[module], eg:

import { useState } from "atomico/cjs/core";

I hope you find it useful

dy commented 4 years ago

Oh, that's nice. Since you provide commonjs exports, it would be enough to add "exports" to package.json as

{
...
  "exports": {
    "require": "./cjs/core.js",
    "default": "./core.js"
  }
...
}

Ref: https://nodejs.org/api/esm.html#esm_exports_sugar

UpperCod commented 4 years ago

Ready added in version 0.16, https://github.com/atomicojs/atomico/blob/master/package.json#L68-L71, thanks for the recommendation