Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for modular javascript runtimes #429

Merged
merged 3 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,6 @@ For more information:

> `npm install --save fluture`

On older environments you may need to polyfill one or more of the following
functions: [`Object.create`][JS:Object.create],
[`Object.assign`][JS:Object.assign] and [`Array.isArray`][JS:Array.isArray].

### CommonJS Module

Although the Fluture source uses the EcmaScript module system,
the `main` file points to a CommonJS version of Fluture.

```js
const fs = require ('fs')
const Future = require ('fluture')

const getPackageName = function (file) {
return Future.node (function (done) { fs.readFile (file, 'utf8', done) })
.pipe (Future.chain (Future.encase (JSON.parse)))
.pipe (Future.map (function (x) { return x.name }))
}

getPackageName ('package.json')
.pipe (Future.fork (console.error) (console.log))
```

### EcmaScript Module

Fluture is written as modular JavaScript.
Expand All @@ -71,7 +48,8 @@ Fluture is written as modular JavaScript.
- On Node versions below 12, you can use the [esm loader][esm]. Alternatively,
you can use the [CommonJS Module](#commonjs-module).
- Modern browsers can run Fluture directly. If you'd like to try this out,
I recommend installing Fluture with [Pika][] or [Snowpack][].
I recommend installing Fluture with [Pika][] or [Snowpack][]. You can also
try the [bundled module](#global-bundle-cdn) to avoid a package manager.
- For older browsers, you can use a bundler such as [Rollup][] or WebPack.
Fluture doesn't use ES5+ language features, so the source does not have to
be transpiled. Alternatively, you can use the
Expand All @@ -93,10 +71,44 @@ getPackageName ('package.json')

### Global Bundle (CDN)

Fluture is hosted in full with all of its dependencies at
https://cdn.jsdelivr.net/gh/fluture-js/Fluture@12.2.1/dist/bundle.js
To load Fluture directly into a browser, a code pen, or [Deno][], use one of
the following downloads from JSDelivr. They are single files that come with all
of Fluture's dependencies pre-bundled.

- [Fluture Script][]: A JavaScript file that adds `Fluture` to the global
scope. Ideal for older browsers and code pens.
- [Fluture Script Minified][]: The same as above, but minified.
- [Fluture Module][]: An EcmaScript module with named exports. Ideal for Deno
or modern browsers.
- [Fluture Module Minified][]: A minified EcmaScript module without TypeScript typings. Not recommended for Deno.

This script will add `Fluture` to the global scope.
[Fluture Script]: https://cdn.jsdelivr.net/gh/fluture-js/Fluture@12.2.1/dist/bundle.js
[Fluture Script Minified]: https://cdn.jsdelivr.net/gh/fluture-js/Fluture@12.2.1/dist/bundle.min.js
[Fluture Module]: https://cdn.jsdelivr.net/gh/fluture-js/Fluture@12.2.1/dist/module.js
[Fluture Module Minified]: https://cdn.jsdelivr.net/gh/fluture-js/Fluture@12.2.1/dist/module.min.js
Avaq marked this conversation as resolved.
Show resolved Hide resolved

### CommonJS Module

Although the Fluture source uses the EcmaScript module system,
the `main` file points to a CommonJS version of Fluture.

On older environments you may need to polyfill one or more of the following
functions: [`Object.create`][JS:Object.create],
[`Object.assign`][JS:Object.assign] and [`Array.isArray`][JS:Array.isArray].

```js
const fs = require ('fs')
const Future = require ('fluture')

const getPackageName = function (file) {
return Future.node (function (done) { fs.readFile (file, 'utf8', done) })
.pipe (Future.chain (Future.encase (JSON.parse)))
.pipe (Future.map (function (x) { return x.name }))
}
Comment on lines +103 to +107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little strange to see const alongside function expressions. Is there a reason to avoid arrow functions here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. You're right. This example was meant to illustrate what Fluture usage looks like in older runtimes. I guess I missed the const.


getPackageName ('package.json')
.pipe (Future.fork (console.error) (console.log))
```

## Interoperability

Expand Down Expand Up @@ -1660,6 +1672,7 @@ by Fluture to generate contextual stack traces.
[Pika]: https://www.pikapkg.com/
[Snowpack]: https://www.snowpack.dev/
[esm]: https://github.com/standard-things/esm
[Deno]: https://deno.land/

[Guide:HM]: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch7.html
[Guide:constraints]: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch7.html#constraints
Expand Down
Loading