syntax icon indicating copy to clipboard operation
syntax copied to clipboard

Extend `@module` annotation for `external` to allow import assertions

Open Kingdutch opened this issue 3 years ago • 0 comments

As of version 17, Node.js requires import assertions (according to the import assertions proposal) when you want to import non-JS files (such as JSON).

In ReScript we can currently make an import statement for a JSON module using @val @module("mdn-data/css/types.json") external types : types = "default" but this will cause a type error: TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///mdn-data/css/types.json" needs an import assertion of type "json"

The generated import statement is import TypesJson from "mdn-data/css/types.json"; but the correct format would be import TypesJson from "mdn-data/css/types.json" assert { type: "json" };. There's currently no way that I'm aware of to make this happen.

My proposal is to add a second optional argument to @module() that contains the assertion object. Please note that the specification chose for an object over a string on purpose because other assertions may be added in the future. So we should follow their example (i.e. rather than allowing only the string "json").

Kingdutch avatar Jul 28 '22 08:07 Kingdutch