diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ddebb640f..c1571cb6a0 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 +- Fix error when importing CommonJS runtime modules with `require()`. https://github.com/rescript-lang/rescript/pull/8194 #### :memo: Documentation 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/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/@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"} diff --git a/packages/artifacts.json b/packages/artifacts.json index 54f54b241c..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", @@ -366,6 +365,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", 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..fd04e4d6f2 --- /dev/null +++ b/tests/commonjs_tests/package.json @@ -0,0 +1,4 @@ +{ + "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) +} 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:."