Skip to content

Commit 1957356

Browse files
committed
Add external stdlib configuration page
1 parent 90806a3 commit 1957356

File tree

3 files changed

+68
-41
lines changed

3 files changed

+68
-41
lines changed

_blogposts/2021-02-09-release-9-0.mdx

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,8 @@ Our compiler comes with a set of stdlib modules (such as `Belt`, `Pervasives`, e
3535

3636
In previous versions, users couldn't ship their compiled JS code without defining a `package.json` dependency on `bs-platform`. Whenever a ReScript developer wanted to publish a package just for pure JS consumption / lean container deployment, they were required to use a bundler to bundle up their library / stdlib code, which made things way more complex and harder to understand.
3737

38-
To fix this problem, we now publish our pre-compiled stdlib JS files as a separate npm package called [`@rescript/std`](https://www.npmjs.com/package/@rescript/std). Each new `bs-platform` release has a matching `@rescript/std` release for runtime compatibility.
3938

40-
We also introduced a new configuration within our `bsconfig.json` file to tell the compiler to use our pre-compiled package instead:
41-
42-
```json
43-
{
44-
/* ... */
45-
"external-stdlib" : "@rescript/std"
46-
}
47-
```
48-
49-
With this configuration set, compiled JS code will now point to the defined `external-stdlib` path:
50-
51-
<CodeTab labels={["ReScript", "JavaScript"]}>
52-
53-
```res
54-
Belt.Array.forEach([1, 2, 3], (num) => Js.log(num))
55-
```
56-
57-
```js
58-
// Note the import path starting with "@rescript/std"
59-
import * as Belt_Array from "@rescript/std/lib/es6/belt_Array.js";
60-
61-
Belt_Array.forEach([
62-
1,
63-
2,
64-
3
65-
], (function (num) {
66-
console.log(num);
67-
68-
}));
69-
```
70-
71-
</CodeTab>
72-
73-
The JavaScript output above was compiled with an `es6` target, but will also work with `commonjs`.
74-
75-
**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!
76-
77-
To prevent unnecessary complications, only use this feature when...
78-
- You want to ship a library for JS / TS consumers without making them depend on `bs-platform`
79-
- You can't depend on `bs-platform` due to toolchain size (docker containers, low-storage deployment devices, etc)
39+
To fix this problem, we introduced an `external-stdlib` configuration that allows specifying a pre-compiled stdlib npm package (`@rescript/std`). More details on how to use that feature can be found in our [External Stdlib](/docs/manual/latest/build-external-stdlib) documentation.
8040

8141
### Less Bundle Bloat when Adding ReScript
8242

data/sidebar_manual_latest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"build-overview",
5454
"build-configuration",
5555
"build-configuration-schema",
56+
"build-external-stdlib",
5657
"build-monorepos",
5758
"interop-with-js-build-systems",
5859
"build-performance"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)