baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript

Home Page:https://baconjs.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Converge ES6 module imports for browser and Node.js

semmel opened this issue · comments

With Node.js now supporting ES modules Bacon should be imported in the same fashion on both platforms the browser and Node.js. This allows to use Bacon in "isomorphic" (i.e. running on Node.js and the browser) source files.

The aim is to support import * as Bacon from 'baconjs' regardless if running in Node.js or the browser.

Using relative path import specifiers it is already possible to target dist/Bacon.mjs but that approach is not so flexible as using bare import specifiers.

Currently:

Node.js

// using bare specifiers
require('baconjs')  ---------┐ 
                             ├-> package.json["main"] -> dist/Bacon.js (ES5)
import Bacon from 'baconjs' -┘ 

ES6 Browser (all, except IE)

<script type="importmap-shim">
{
	"imports": { "baconjs": "../node_modules/baconjs/dist/Bacon.mjs" }
}                                                            ↑
</script>                                                    |
<script type="module-shim">                                  |
import * as Bacon from 'baconjs'  ---------------------------┘

Proposed change to package.json

"module": "./dist/Bacon.mjs", // <-- helps rollup
"exports": {
	"import": "./dist/Bacon.mjs",	// <-- helps isomorphic JS code
	"require": "./dist/Bacon.js"
},

ES6 browsers and Node.js

import * as Bacon from 'baconjs'  ---(Node.js)-→ package.json["exports"]["import"]
                           |                                                |
                        (Rollup)                                            |
                           ↓                                                ↓
                       package.json["module"]    ---------->          dist/Bacon.mjs
                           

If agreeable I'd assemble a small PR with a smoke test?

Sure, go ahead!