From 8900876f0d1ecd55dcb108a532f9282b0669874e Mon Sep 17 00:00:00 2001 From: Shulhi Sapli <913103+shulhi@users.noreply.github.com> Date: Mon, 19 Jan 2026 11:45:59 +0800 Subject: [PATCH 1/7] Explicitly add type commonjs for commonjs runtime --- packages/@rescript/runtime/lib/js/package.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 packages/@rescript/runtime/lib/js/package.json diff --git a/packages/@rescript/runtime/lib/js/package.json b/packages/@rescript/runtime/lib/js/package.json new file mode 100644 index 0000000000..e6ec81ea07 --- /dev/null +++ b/packages/@rescript/runtime/lib/js/package.json @@ -0,0 +1 @@ +{"type" : "commonjs"} From ada25967f72093395c5133e66b6955c34aed1d56 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli <913103+shulhi@users.noreply.github.com> Date: Mon, 19 Jan 2026 14:10:41 +0800 Subject: [PATCH 2/7] Update artifacts.json --- packages/artifacts.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/artifacts.json b/packages/artifacts.json index 54f54b241c..b4a752f479 100644 --- a/packages/artifacts.json +++ b/packages/artifacts.json @@ -366,6 +366,7 @@ "lib/js/Stdlib_Uint8ClampedArray.js", "lib/js/Stdlib_WeakMap.js", "lib/js/Stdlib_WeakSet.js", + "lib/js/package.json", "lib/ocaml/Belt.cmi", "lib/ocaml/Belt.cmj", "lib/ocaml/Belt.cmt", @@ -1240,4 +1241,4 @@ "lib/ocaml/Stdlib_WeakSet.res", "package.json" ] -} \ No newline at end of file +} From f77ee97c24cf7ac9ea8892547f88e25389a21967 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli <913103+shulhi@users.noreply.github.com> Date: Mon, 19 Jan 2026 16:09:01 +0800 Subject: [PATCH 3/7] Remove es6 package.json because it's already the default --- packages/@rescript/runtime/lib/es6/package.json | 1 - packages/artifacts.json | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 packages/@rescript/runtime/lib/es6/package.json diff --git a/packages/@rescript/runtime/lib/es6/package.json b/packages/@rescript/runtime/lib/es6/package.json deleted file mode 100644 index 658eb9d163..0000000000 --- a/packages/@rescript/runtime/lib/es6/package.json +++ /dev/null @@ -1 +0,0 @@ -{"type" : "module"} \ No newline at end of file diff --git a/packages/artifacts.json b/packages/artifacts.json index b4a752f479..403b32f83c 100644 --- a/packages/artifacts.json +++ b/packages/artifacts.json @@ -191,7 +191,6 @@ "lib/es6/Stdlib_Uint8ClampedArray.js", "lib/es6/Stdlib_WeakMap.js", "lib/es6/Stdlib_WeakSet.js", - "lib/es6/package.json", "lib/js/Belt.js", "lib/js/Belt_Array.js", "lib/js/Belt_Float.js", @@ -1241,4 +1240,4 @@ "lib/ocaml/Stdlib_WeakSet.res", "package.json" ] -} +} \ No newline at end of file From ef6924b632182dba1059d8249b4d1aec924ae9a4 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli <913103+shulhi@users.noreply.github.com> Date: Mon, 19 Jan 2026 16:15:42 +0800 Subject: [PATCH 4/7] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ddebb640f..e4deea9e42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Reanalyze: fix reactive/server stale results when cross-file references change without changing dead declarations (non-transitive mode). https://github.com/rescript-lang/rescript/pull/8173 - Add duplicate package detection to rewatch. https://github.com/rescript-lang/rescript/pull/8180 - Rewatch: do not warn about "reanalyze" config field. https://github.com/rescript-lang/rescript/pull/8181 +- Override module type for CommonJS runtime. https://github.com/rescript-lang/rescript/pull/8194 #### :memo: Documentation From 39882369714a04e308eb9e6d303aaecbc242983e Mon Sep 17 00:00:00 2001 From: Shulhi Sapli <913103+shulhi@users.noreply.github.com> Date: Tue, 20 Jan 2026 08:22:22 +0800 Subject: [PATCH 5/7] WIP: Add tests --- biome.json | 1 + lib_dev/paths.js | 5 +++++ package.json | 1 + scripts/test.js | 12 ++++++++++++ tests/commonjs_tests/package.json | 1 + tests/commonjs_tests/rescript.json | 18 ++++++++++++++++++ tests/commonjs_tests/src/belt_import.js | 23 +++++++++++++++++++++++ tests/commonjs_tests/src/belt_import.res | 7 +++++++ 8 files changed, 68 insertions(+) create mode 100644 tests/commonjs_tests/package.json create mode 100644 tests/commonjs_tests/rescript.json create mode 100644 tests/commonjs_tests/src/belt_import.js create mode 100644 tests/commonjs_tests/src/belt_import.res diff --git a/biome.json b/biome.json index 39077df201..7a2d0db459 100644 --- a/biome.json +++ b/biome.json @@ -55,6 +55,7 @@ "!**/.yarn", "!**/tests/analysis_tests/**/src", "!**/tests/build_tests/**/src", + "!**/tests/commonjs_tests/src", "!**/tests/docstring_tests", "!**/tests/gentype_tests", "**/tests/gentype_tests/typescript-react-example/**", diff --git a/lib_dev/paths.js b/lib_dev/paths.js index 6f01c7f7c4..f83de72e3e 100644 --- a/lib_dev/paths.js +++ b/lib_dev/paths.js @@ -52,6 +52,11 @@ export const buildTestDir = path.resolve(testDir, "build_tests"); */ export const docstringTestDir = path.resolve(testDir, "docstring_tests"); +/** + * path: `/tests/commonjs_tests/` + */ +export const commonjsTestDir = path.resolve(testDir, "commonjs_tests"); + /** * path: `/compiler/common/bs_version.ml` */ diff --git a/package.json b/package.json index 493c16d03a..cd5fc2eb80 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "tests/docstring_tests", "tests/gentype_tests/**", "tests/tools_tests", + "tests/commonjs_tests", "scripts/res" ], "packageManager": "yarn@4.10.3", diff --git a/scripts/test.js b/scripts/test.js index 9e62182b71..3ea55f02fe 100644 --- a/scripts/test.js +++ b/scripts/test.js @@ -106,6 +106,18 @@ if (mochaTest) { cwd: projectDir, stdio: "inherit", }); + + // CommonJS tests + const commonjsTestDir = path.join(projectDir, "tests/commonjs_tests"); + await execBuild([], { + cwd: commonjsTestDir, + stdio: "inherit", + }); + + await node("tests/commonjs_tests/src/belt_import.js", [], { + cwd: projectDir, + stdio: "inherit", + }); } if (buildTest) { diff --git a/tests/commonjs_tests/package.json b/tests/commonjs_tests/package.json new file mode 100644 index 0000000000..f6559b6cab --- /dev/null +++ b/tests/commonjs_tests/package.json @@ -0,0 +1 @@ +{"name": "rescript-commonjs-tests", "type": "commonjs"} diff --git a/tests/commonjs_tests/rescript.json b/tests/commonjs_tests/rescript.json new file mode 100644 index 0000000000..07961edebb --- /dev/null +++ b/tests/commonjs_tests/rescript.json @@ -0,0 +1,18 @@ +{ + "name": "rescript-commonjs-tests", + "sources": [ + { + "dir": "src", + "subdirs": true + } + ], + "package-specs": { + "module": "commonjs", + "in-source": true, + "suffix": ".js" + }, + "compiler-flags": [ + "-w -3-6-26-27-29-30-32..40-44-45-52-60-9-106+104", + "-warn-error A" + ] +} diff --git a/tests/commonjs_tests/src/belt_import.js b/tests/commonjs_tests/src/belt_import.js new file mode 100644 index 0000000000..27e9b7ed5c --- /dev/null +++ b/tests/commonjs_tests/src/belt_import.js @@ -0,0 +1,23 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +'use strict'; + +let Belt_MapInt = require("@rescript/runtime/lib/js/Belt_MapInt.js"); + +let f = Belt_MapInt.get; + +function g() { + return Belt_MapInt.get(Belt_MapInt.fromArray([ + [ + 1, + "hello" + ], + [ + 2, + "world" + ] + ]), 1); +} + +exports.f = f; +exports.g = g; +/* No side effect */ diff --git a/tests/commonjs_tests/src/belt_import.res b/tests/commonjs_tests/src/belt_import.res new file mode 100644 index 0000000000..5f3190e5a5 --- /dev/null +++ b/tests/commonjs_tests/src/belt_import.res @@ -0,0 +1,7 @@ +let f = (xs: Belt.Map.Int.t, idx: int) => { + Belt.Map.Int.get(xs, idx) +} + +let g = () => { + Belt.Map.Int.fromArray([(1, "hello"), (2, "world")])->f(1) +} From 0e5ecd80633481c1cc9b6a1b26de547535ab415a Mon Sep 17 00:00:00 2001 From: Shulhi Sapli <913103+shulhi@users.noreply.github.com> Date: Tue, 20 Jan 2026 15:44:05 +0800 Subject: [PATCH 6/7] commit yarn.lock --- tests/commonjs_tests/package.json | 5 ++++- yarn.lock | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/commonjs_tests/package.json b/tests/commonjs_tests/package.json index f6559b6cab..fd04e4d6f2 100644 --- a/tests/commonjs_tests/package.json +++ b/tests/commonjs_tests/package.json @@ -1 +1,4 @@ -{"name": "rescript-commonjs-tests", "type": "commonjs"} +{ + "name": "rescript-commonjs-tests", + "type": "commonjs" +} diff --git a/yarn.lock b/yarn.lock index 1569da60f1..8960aae37c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2454,6 +2454,12 @@ __metadata: languageName: node linkType: hard +"rescript-commonjs-tests@workspace:tests/commonjs_tests": + version: 0.0.0-use.local + resolution: "rescript-commonjs-tests@workspace:tests/commonjs_tests" + languageName: unknown + linkType: soft + "rescript@workspace:., rescript@workspace:^": version: 0.0.0-use.local resolution: "rescript@workspace:." From 25d23156683d98f359d5b2643b45a0954e9d1608 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli <913103+shulhi@users.noreply.github.com> Date: Tue, 20 Jan 2026 19:41:37 +0800 Subject: [PATCH 7/7] Update CHANGELOG.md Co-authored-by: Christoph Knittel --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4deea9e42..c1571cb6a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ - Reanalyze: fix reactive/server stale results when cross-file references change without changing dead declarations (non-transitive mode). https://github.com/rescript-lang/rescript/pull/8173 - Add duplicate package detection to rewatch. https://github.com/rescript-lang/rescript/pull/8180 - Rewatch: do not warn about "reanalyze" config field. https://github.com/rescript-lang/rescript/pull/8181 -- Override module type for CommonJS runtime. https://github.com/rescript-lang/rescript/pull/8194 +- Fix error when importing CommonJS runtime modules with `require()`. https://github.com/rescript-lang/rescript/pull/8194 #### :memo: Documentation