rescript-association / genType

Auto generation of idiomatic bindings between Reason and JavaScript: either vanilla or typed with TypeScript/FlowType.

Home Page:https://rescript-lang.org/docs/gentype/latest/introduction

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Curry.js error when exporting a rescript function with more than 1 parameter

searleser97 opened this issue · comments

When exporting a function with more than 1 parameter (2 or more) it throws the following error, which basically says there is an issue with the way we import curry.js. I am attaching an example and package versions below.

Error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: path/to/node_modules/rescript/lib/es6/curry.js
require() of ES modules is not supported.
require() of /path/to/node_modules/rescript/lib/es6/curry.js from /path/to/src/demo.gen.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename curry.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /path/to/node_modules/rescript/lib/es6/package.json.

at new NodeError (node:internal/errors:329:5)
    at Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
    at Object.require.extensions.<computed> [as .js] (/path/to/node_modules/ts-node/src/index.ts:851:44)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/path/to/src/demo.gen.ts:6:1)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Module.m._compile (/path/to/node_modules/ts-node/src/index.ts:858:23)

Example

demo.res
@genType
let helloWord = (firstName: string, lastName: string): unit => {
  Js.log("Hello " ++ firstName ++ " " ++ lastName)
}
demo.bs.js
// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';


function helloWord(firstName, lastName) {
  console.log("Hello " + firstName + " " + lastName);
  
}

exports.helloWord = helloWord;
/* No side effect */
demo.gen.ts
/* TypeScript file generated from demo.res by genType. */
/* eslint-disable import/first */


// @ts-ignore: Implicit any on import
import * as Curry__Es6Import from 'rescript/lib/es6/curry.js';
const Curry: any = Curry__Es6Import;

// @ts-ignore: Implicit any on import
import * as demoBS__Es6Import from './demo.bs';
const demoBS: any = demoBS__Es6Import;

export const helloWord: (firstName:string, lastName:string) => void = function (Arg1: any, Arg2: any) {
  const result = Curry._2(demoBS.helloWord, Arg1, Arg2);
  return result
};
package.json
{
    "ts-node": "^10.2.1",
    "gentype": "^4.1.0",
    "rescript": "^9.1.4",
    "typescript": "^3.9.6"
}

Node Version

$ node -v
v15.11.0

works if we use curry.js from /lib/js/curry.js instead of /lib/es6/curry.js

import * as Curry__Es6Import from "rescript/lib/js/curry.js";

this might be a workaround, although it is awful to change this manually every time :/

After taking a look at this file in genType: https://github.com/rescript-association/genType/blob/fb6201266558a64d62441f7ac0d8d6652456397a/src/Config_.ml

I found this option

"gentypeconfig": {
  "module": "commonjs",
}

setting that solves the issue, however I believe you should add documentation for each available setting in the documentation site: https://rescript-lang.org/docs/gentype/latest/getting-started