Configuration
Put each package's build configuration in rescript.json. Upgrading from an older configuration? See Migrate to v13.
Browse the configuration schema for a reference. The sections below explain common settings and native build-system options.
name, namespace
name is the name of the library, used as its "namespace". You can activate namespacing through "namespace": true in your rescript.json. Namespacing is almost mandatory; we haven't turned it on by default yet to preserve backward-compatibility.
Explanation: by default, your files, once used as a third-party dependency, are available globally to the consumer. E.g. if you have a Util.res and the consumer also has a file of the same name, they will clash. Turning on namespace avoids this by wrapping all your own project's files into an extra module layer; instead of a global Util module, the consumer will see you as MyProject.Util. The namespacing affects your consumers, not yourself.
Aka, in ReScript, "namespace" is just a fancy term for an auto-generated module that wraps all your project's files (efficiently and correctly, of course!) for third-party consumption.
We don't do folder-level namespacing for your own project; all your own file names must be unique. This is a constraint that enables several features such as fast search and easier project reorganization.
Note: the rescript.json name should be the same as the package.json name, to avoid confusing corner-cases. However, this means that you can't use a camelCased names such as MyProject, since package.json and npm forbid you to do so (some file systems are case-insensitive). To have the namespace/module as MyProject, write "name": "my-project". ReScript will turn that into the camelCased name correctly.
Note on custom namespacing: if for some reason, you need a namespace that is different from what your name will produce, you can directly send a string to the namespace option. For example, if your package is a binding named bs-some-thing, you can use "namespace": "some-thing" to get SomeThing namespace instead of BsSomeThing.
sources
Your source files need to be specified explicitly (we don't want to accidentally drill down into some unrelated directories). Examples:
JSON{
"sources": ["src", "examples"]
}
JSON{
"sources": {
"dir": "src",
"subdirs": ["page"]
}
}
JSON{
"sources": [
"examples",
{
"dir": "src",
"subdirs": true // recursively builds every subdirectory
}
]
}
You can mark directories containing tests or development helpers as dev-only. Their modules can use dev-dependencies; they aren't compiled when the package is a third-party dependency or when you use --prod:
JSON{
"sources": {
"dir": "test",
"type": "dev"
}
}
You can also explicitly allow which modules can be seen from outside. This feature is especially useful for library authors who want to have a single entry point for their users.
Here, the file src/MyMainModule.res is exposed to outside consumers, while all other files are private.
JSON{
"sources": {
"dir": "src",
"public": ["MyMainModule"]
}
}
dependencies, dev-dependencies
List of ReScript dependencies. Just like package.json's dependencies, they'll be searched in node_modules.
Note that only sources marked with "type":"dev" will be able to resolve modules from dev-dependencies.
The legacy names bs-dependencies and bs-dev-dependencies are still accepted with deprecation warnings.
Use package names as strings to build all of a dependency's features. To select only some of its features, use the object form described below.
features
Features let a package include optional source directories, such as browser-specific modules. Tag a source directory with feature, and use the top-level features map when one feature should enable others:
JSON{
"name": "optional-ui",
"sources": [
{ "dir": "src" },
{ "dir": "src-dom", "feature": "dom" },
{ "dir": "src-browser", "feature": "browser" }
],
"features": {
"core": [],
"browser": ["dom"]
}
}
Without a feature selection, all source directories are built. Use --features with build or watch to select features for the package you're building:
SHrescript build --features core rescript build --features browser
The first command builds only the untagged src directory. The second also builds src-browser and src-dom, because browser enables dom. Feature implications are transitive, and cycles are rejected. Untagged directories are included with every selection; tagged directories require their feature to be active. Nested source directories inherit their parent's feature unless they declare their own.
--features takes a comma-separated list, such as --features core,browser. An empty selection is rejected. The flag only selects the current package's features; it does not select features for its dependencies.
Selecting a Dependency's Features
In the consuming package's rescript.json, list the features you need from a dependency:
JSON{
"dependencies": [{ "name": "optional-ui", "features": ["browser"] }]
}
The dependency's own feature map expands this selection, so optional-ui also builds its dom feature. The same object form works in dev-dependencies.
A string entry, or an object without
features, enables all of that dependency's features.An explicit
"features": []builds only its untagged source directories.If several packages consume the same dependency, their feature requests are combined. If any consumer requests all features, all are enabled.
Feature selection and --prod both apply: a dev-only directory is excluded from a production build even if its feature is enabled.
js-post-build
Hook that's invoked for every JavaScript file emitted by a build. It can be useful for JavaScript build-system interop, but use it sparingly. Starting another process for every recompiled file can noticeably slow down builds for both applications and libraries.
Example:
JSON{
"js-post-build": {
"cmd": "node ./postProcess.mjs"
}
}
The build system appends the absolute path to the generated JavaScript file as the command's final argument. The path points:
next to the source file when
"in-source": trueinto the corresponding
lib/<module>/output directory when"in-source": false
Since ReScript 12.2, the command runs from the directory containing the rescript.json where the hook is defined, and output written to stdout or stderr is forwarded to the build output. This makes relative paths predictable in nested packages and monorepos.
jsx
Controls how the compiler emits JSX and which runtime (if any) it delegates to. A minimal configuration looks like this:
JSON{
"jsx": {
"version": 4
}
}
version: JSX transform version.4enables the React 17+ transform and is the current supported version.module: Override the target module that receives JSX calls. Useful for generic JSX transforms; omit it when using the built-in React runtime.preserve: Whentrue, the compiler re-emits JSX syntax in the generated JavaScript so bundlers or other tooling can take over the transform later. The regenerated JSX might differ slightly from the original source but stays semantically equivalent. See Preserve mode for details.
All fields are optional; unspecified fields fall back to the defaults mentioned above. Combine them as needed for your project's JSX runtime.
package-specs
Choose JavaScript modules (esmodule, the default) or CommonJS (commonjs). By default, JavaScript is generated alongside the source files. For example:
JSON{
"package-specs": {
"module": "esmodule",
"in-source": true
}
}
"module": "esmodule"generates JavaScript modules and is the default whenpackage-specsis omitted."module": "commonjs"generates CommonJS output."in-source": trueis the default and generates output alongside source files. Set it tofalseto uselib/es6for JavaScript modules orlib/jsfor CommonJS.
To emit both formats, use distinct suffixes so the outputs don't overwrite each other:
JSON{
"package-specs": [
{ "module": "esmodule", "suffix": ".mjs" },
{ "module": "commonjs", "suffix": ".cjs" }
]
}
A suffix set on an individual package spec overrides the top-level suffix setting.
This configuration only applies to you, when you develop the project. When the project is used as a third-party library, the consumer's own rescript.json package-specs overrides the configuration here, logically.
suffix
Since 11.0: The suffix can be freely chosen. However, we still suggest you stick to the convention and use
one of the following:
".js"".mjs"".cjs"".res.js"".res.mjs"".res.cjs"
Design Decisions
Generating JS files with the .res.js suffix means that, on the JS side, you can do const myReScriptFile = require('./TheFile.res.js'). The benefits:
It's immediately clear that we're dealing with a generated JS file here.
It avoids clashes with a potential
TheFile.jsfile in the same folder.It avoids the need of using a build system loader for ReScript files. This + in-source build means integrating a ReScript project into your pure JS codebase basically doesn't touch anything in your build pipeline at all.
warnings
Selectively turn on/off certain warnings and/or turn them into hard errors. Example:
JSON{
"warnings": {
"number": "-44-102",
"error": "+5"
}
}
Turn off warning 44 and 102 (polymorphic comparison). Turn warning 5 (partial application whose result has function type and is ignored) into a hard error.
The warning numbers are shown in the build output when they're triggered. See Warning Numbers for the complete list.
compiler-flags
Extra flags to pass to the compiler. For advanced usages. The legacy name bsc-flags is still accepted with a deprecation warning.
-open ABCopens the moduleABCfor each file in the project.ABCcan either be a dependency, namespaced project or local module of the current project.
gentypeconfig
To enable genType, set "gentypeconfig" at top level in the project's rescript.json.
JSON{
"gentypeconfig": {
"module": "esmodule",
"moduleResolution": "node",
"generatedFileExtension": ".gen.tsx",
"debug": {
"all": false,
"basic": false
}
}
}
generatedFileExtension: File extension used for genType generated files (defaults to ".gen.tsx")
module: Module format used for the generated *.gen.tsx files (supports "esmodule" and "commonjs")
moduleResolution: Module resolution strategy used in genType outputs. This may be required for compatibility with TypeScript projects. Specify the value as the same in tsconfig.json.
"node"(default): Drop extensions in import paths."node16": Use TS output's extension. This provides compatibility with projects using"moduleResolution": "node16"and ES Modules."bundler": Use TS input's extension. This provides compatibility with projects using"moduleResolution": "bundler"and ES Modules. This also requires TS v5.0+ andcompilerOptions.allowImportingTsExtensionstotrue
debug: Enable debug logs.
Environment Variables
We heavily disrecommend the usage of environment variables, but for certain cases, they're justified.
Error Output Coloring: FORCE_COLOR
This is mostly for other programmatic usage of rescript where outputting colors is not desired.
When FORCE_COLOR is set to 1: rescript produces color.
When FORCE_COLOR is set to 0: rescript doesn't produce color.
When FORCE_COLOR is not set: rescript might or might not produce color, depending on a smart detection of where it's outputted.
Note that the underlying compiler will always be passed
-color always. See more details in this issue.