|
| 1 | +--- |
| 2 | +title: "External Stdlib" |
| 3 | +metaTitle: "External Stdlib" |
| 4 | +description: "Configuring an external ReScript stdlib package" |
| 5 | +canonical: "/docs/manual/latest/build-external-stdlib" |
| 6 | +--- |
| 7 | + |
| 8 | +# External Stdlib |
| 9 | + |
| 10 | +**Since 9.0** |
| 11 | + |
| 12 | +In a typical ReScript application, you'd ship a package with an npm dependency to `bs-platform` (`rescript`), which means downloading several tens of megabytes of executables. |
| 13 | + |
| 14 | +Whenever a ReScript developer wants to publish a package just for pure JS consumption / lean container deployment, they would be required to use a bundler to bundle up their library / stdlib code, which is not ideal. |
| 15 | + |
| 16 | +To fix this problem, you can just depend on our pre-compiled stdlib JS files, published as a separate npm package called [`@rescript/std`](https://www.npmjs.com/package/@rescript/std). Each new ReScript release has a matching `@rescript/std` release for runtime compatibility. |
| 17 | + |
| 18 | +## Configuration |
| 19 | + |
| 20 | +For example, let's assume we want to build a JS package that is built on ReScript 9.0. We'd first install our `@rescript/std` package: |
| 21 | + |
| 22 | +``` |
| 23 | +npm install [email protected] --save-dev |
| 24 | +npm install @rescript/[email protected] --save |
| 25 | +``` |
| 26 | + |
| 27 | +In your `bsconfig.json`, set up following configuration: |
| 28 | + |
| 29 | +```json |
| 30 | +{ |
| 31 | + /* ... */ |
| 32 | + "external-stdlib" : "@rescript/std" |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +With this configuration set, compiled JS code will now point to the defined external-stdlib path: |
| 37 | + |
| 38 | +<CodeTab labels={["ReScript", "JavaScript"]}> |
| 39 | + |
| 40 | +```res |
| 41 | +Belt.Array.forEach([1, 2, 3], (num) => Js.log(num)) |
| 42 | +``` |
| 43 | + |
| 44 | +```js |
| 45 | +// Note the import path starting with "@rescript/std" |
| 46 | +import * as Belt_Array from "@rescript/std/lib/es6/belt_Array.js"; |
| 47 | + |
| 48 | +Belt_Array.forEach([ |
| 49 | + 1, |
| 50 | + 2, |
| 51 | + 3 |
| 52 | + ], (function (num) { |
| 53 | + console.log(num); |
| 54 | + |
| 55 | + })); |
| 56 | +``` |
| 57 | + |
| 58 | +</CodeTab> |
| 59 | + |
| 60 | +The JavaScript output above was compiled with an `es6` target, but will also work with `commonjs`. |
| 61 | + |
| 62 | +**Important:** When using this option, you need to make sure that the version number of `bs-platform` and `@rescript/std` matches with the same version number in your `package.json` file, otherwise you'll eventually run into runtime problems due to mismatching stdlib behavior! |
| 63 | + |
| 64 | +To prevent unnecessary complications, only use this feature when... |
| 65 | +- You want to ship a library for JS / TS consumers without making them depend on `bs-platform` |
| 66 | +- You can't depend on `bs-platform` due to toolchain size (docker containers, low-storage deployment devices, etc) |
0 commit comments