Generate ".resi" file from ".res"?

Does anyone know if there’s a CLI command to generate an interface file (.resi) from a ReScript source file (.res)? The VS Code plugin supports automatic generation of interface files for the old Reason syntax, but it doesn’t appear to work for ReScript files yet. I think it just calls out to refmt, though. Anyone know the command?

1 Like

There is the bsc -i option (print inferred interface). Suppose you have a file src/Index.res in the project, then it works like this:

bsc -i lib/bs/src/Index.cmi

This prints the interface in ML syntax to standard output. You can also use the -bs-re-out option to print in RE syntax. I don’t think there’s an option to print ReScript syntax though, but it should be easy to convert using bsc -format Index.rei >Index.resi.

EDIT: as per the PR linked below by @fham, ‘print inferred interface’ command will print in ReScript syntax by default in an upcoming release.

3 Likes

In the newest release, it should just be bsc lib/bs/src/Index.cmi

Edit: See https://github.com/rescript-lang/rescript-compiler/issues/5023

4 Likes

Just use it on the source file: https://rescript-lang.org/docs/manual/latest/try#quickly-compile-a-single-file

bsc -i MyFile.res

We’ll extract a dedicated CLI section once the new rescript binary stabilizes.

6 Likes

It appears this feature will be quite useful for hiding locals for reloading support with create-react-app fast-refresh!

I found adding -i in your bsconfig.json[“bsc-flags”] will output the types as it builds when using the watcher. Not quite sure the consequences, but this will help me fix a bunch of components quickly then I can remove the flag.

EDIT: I’ve tried bsc -i src/someComponent.res but it seems to freak out at the sight of JSX and any mention of React.* any tips for making that work?

1 Like

Hi, we provided native support for dumping interface on master branch, should be available for public in a couple of weeks

4 Likes

Is the dumping interface able to keep track of the @ annotations when generating an interface? (e.g. @react.component, @module etc.)

It cannot keep track of ppx transformations as they’re already done, and in general irreversible.

The attribute is preserved so would it be possible to special case it in interface generation?

@react.component changes the original code substantially. It would be possible with some effort to work around that. However, there are plans to simplify the react ppx later on, making this easier.

2 Likes

It seems that bsc -i no longer works:

$ bsc -i src/IDB__Utils.res
unknown option: '-i'.
Usage: bsc <options> <files>
Options are:
Options:
  -w                        <list>  Enable or disable warnings according to <list>:
                            +<spec>   enable warnings in <spec>
                            -<spec>   disable warnings in <spec>
                            @<spec>   enable warnings in <spec> and treat them as errors
                            <spec> can be:
                            <num>             a single warning number
                            <num1>..<num2>    a range of consecutive warning numbers
                            default setting is +a-4-9-20-40-41-42-50-61-102
  -bs-g                     Debug mode
  -bs-D                     Define conditional variable e.g, -D DEBUG=true
  -e                        (experimental) set the string to be evaluated in ReScript syntax
  -bs-uncurry               *internal
  -v                        Print compiler version and location of standard library and exit
  -version                  Print version and exit
  -warn-help                Show description of warning numbers
  -warn-error               <list>  Enable or disable error status for warnings according
                            to <list>.  See option -w for the syntax of <list>.
                            Default setting is -a+5+6+101+109

Is there a new way to do it?

If you are using VSCode; the ReScript: Create an interface file for this implementation file command is available via the extension.

Hi @illusionalsagacity

I usually don’t use VSCode, but Emacs (old habits die hard); but I can probably open it for this task. It’s not like I need to do it every day.

If that’s exposed through LSP, I could probably make it work with lsp-rescript.el :smiley:

Thanks!

Maybe you can translate this to lisp:

1 Like