rescript-lang / rescript-core

A drop-in standard library for ReScript. Intended to be familiar for JavaScript developers, easy to use, and be rich enough (without being bloated) so that you don't need to reach for anything else for typical ReScript development.
MIT License
162 stars 28 forks source link

@rescript/core

ReScript's new standard library. Intended to be familiar for JavaScript developers, easy to use, and be rich enough (without being bloated) so that you don't need to reach for anything else for typical ReScript development.

In ReScript 11, it is shipped as a separate npm package @rescript/core that is added to your project as per the installation instructions. In future ReScript versions, it will be included with the rescript npm package itself.

Versioning and Compatibility

@rescript/core ReScript
1.3.0 11.1+, uncurried mode
1.0.0-1.2.0 11.0+
0.x 10.1+

Background

Goals:

  1. Adding a rich (enough) standard library that's familiar to JavaScript developers
  2. Clearing up the current confusing situation with regards to t-first vs t-last, OCaml pervasives, Js.Array2 etc
  3. Prepare for "uncurried by default" by only shipping compatible APIs

In practice, the proposed standard library is a fusion of rescript-js, rescript-promise, and Belt - all battle tested and well liked, with a few minor additions sprinkled in. So while we're calling this a new standard library, the parts it's made up of isn't new.

It ships as a separate package, so you can try it out and start migrating to it if you want at your convenience. Eventually, if the community likes it and it stands the test of time, it might make its way into the compiler. And, at that point it would replace the current Js namespace.

Versioning and current status

You're encouraged to start using this today. We don't anticipate nor plan for larger changes to any of the APIs, but please be aware that changes may still happen as this project stabilizes. We're of course also interested in the community's feedback on the APIs. The DX of using Core is going to gradually improve as we add doc strings, documentation and so on.

Guiding principles

We've been following a few guiding principles as we developed this:

Acknowledgements

Installation

ReScript >=10.1 is required.

$ npm install @rescript/core

Then add @rescript/core to your bsconfig.json's bs-dependencies:

 {
   "bs-dependencies": [
+    "@rescript/core"
   ]
 }

Open it so it's available in the global scope.

 {
   "bsc-flags": [
+    "-open RescriptCore",
   ]
 }

ESM and CJS

To use this library with CommonJS modules you should set the file extension in your project's rescript.json to .res.js, .js, .res.cjs, or .cjs. This will work with older versions of Node and most web projects using a bundler.

To use this library with EcmaScript modules you should set the file extension in your project's rescript.json to .res.mjs, or .mjs. This will work with newer versions of Node and web projects using a modern bundler like Vite.

What it looks like

All relevant standard library modules are designed to be available directly in the global scope, just like you expect them to.

Console.log("Hello world!")

let timeout = setTimeout(() => {
  Console.log("Hello!")
}, 100)

clearTimeout(timeout)

let array = [1, 2, 3]

let sum = array
  ->Array.map(x => x * 2)
  ->Array.reduce(0, (acc, item) => acc + item)

let maybeValidFloats = ["1", "1.5", "some random string"]

let validFloats = maybeValidFloats
  ->Array.filterMap(v => v->Float.fromString)

Documentation

Documentation will be added successively to this repository. We're looking for help in producing good, high quality docstrings for all APIs. More information coming on how you can help out with this.

OCaml compat

During the transition phase to this standard library you might find yourself needing to access the current global Array/List etc modules that originate from OCaml. These will be removed eventually, but in the transition phase you'll be able to access them by adding this open at the top of any file:

open OCamlCompat

Differences to rescript-js

This standard library is based on rescript-js, but with the tweaks and modifications. Some of the changes are listed below. There are also no uncurried versions of anything in the standard library. This is because uncurried by default will be coming soon in ReScript 11, which will remove the need for explicitly uncurried functions.

Array

Float

String

Promise

The Promise module is inlined from https://github.com/ryyppy/rescript-promise, with these additions:

Option, List, Result

window, document

Migration

Things are added to this section on migration gradually.

Migrating to the new standard library should be easy to do gradually. In this section we'll gather information that's intended to help migrating as painlessly as possible.

In general, we suggest you take the following gradual approach:

  1. Install the stdlib and open it in the global scope
  2. Fix any inconsistencies that uncovers. That will be things like module name clashes (more details below), someArr[0] now returning an option, and so on.
  3. Run the semi-automated migration below. This will uncover more things to fix.
  4. Do a final search for Js. in your project, and replace any leftovers that the migration script did not catch.

Check out this issue on the GitHub tracker if you get stuck. We'll do what we can to help out when migrating there.

Semi-automated migration

We've prepared a script you can run with comby.dev that will do parts of the migration for you automatically. The migration script is located in migration.toml. Here's an example of how you can run it:

# Run in your project root. Assumes `migration.toml` has been copied in place to your project root.
comby -config migration.toml -f .res -matcher .re -exclude-dir node_modules,__generated__ -i

# You should run it twice - once for .res files, and once for .resi. The command below runs for .resi, and the one above for .res.
comby -config migration.toml -f .resi -matcher .re -exclude-dir node_modules,__generated__ -i

The migration script is a set of instructions that Comby runs in sequence. You're encouraged to take migration.toml and tweak it so it fits your needs. Comby is powerful. It can do interactive rewriting and numerous other useful stuff. Check it out, but please note it's not intended to cover all of the migration necessary. You'll still likely need to do a few manual fixes after running the migration scripts.

Name clashes

Since the standard library is designed to live in the global scope, you might have your own modules or modules from one of your dependencies whose names collide with the modules from the standard library.

This is a side effect of shipping the standard library as its own package, meaning it won't be a problem if the standard library is adopted and ships with the compiler. At that point you'll be able to have your own modules shadowing builtin modules.

In the meantime, as a workaround: