Any possibility to use default export instead of named exports?

TL;DR: Is there any way to disable automatic named exports?

Looks like there are couple design decisions in GatbysJS that are making interop with rescript hard. At least harder than it seemed at the beginning :slight_smile:

One of those decisions is necessity to have only default export in page templates and no named ones:

[develop-gatsby] /home/kepi/dev/site/src/pages/index.js
[develop-gatsby]   17:1  warning  In page templates only a default export of a valid React
[develop-gatsby] component and the named exports of a page query, getServerData or config are
[develop-gatsby] allowed.
[develop-gatsby]         All other named exports will cause Fast Refresh to not preserve local
[develop-gatsby] component state and do a full refresh.
[develop-gatsby]
[develop-gatsby]         Please move your other named exports to another file. Also make sure
[develop-gatsby] that you only export page queries that use the "graphql" tag from "gatsby".
[develop-gatsby]   limited-exports-page-templates
[develop-gatsby]
[develop-gatsby] âś– 1 problem (0 errors, 1 warning)

In src/pages/index.js there is simple React component:

@react.component
let default = () => {
    <div>{React.string("Hello World")}</div>
}

Which is compiled to:

// Generated by ReScript, PLEASE EDIT WITH CARE

import * as React from "react";

function Index$default(Props) {
  return React.createElement("div", undefined, "Hello World");
}

var $$default = Index$default;

export {
  $$default ,
  $$default as default,
}
/* react Not a pure module */

Manually removing named exports from generated code and replacing them with export default $$default “resolves” the problem.

Working without Fast Refresh is really big pain, so I would like to find some
way how to get this working. Only workaround which comes to my mind is to have
page templates in JavaScript and load component from other ReScript files. It
might work (didn’t try it yet), but I would like to avoid having non-rescript
files.

I found similar problem with Storybook here in forums (Default export and storybook csf), but workaround there isn’t helpful in this situation.

I have been thinking about adding default export with %%raw, but but I didn’t
found any way how to disable automatic named exports. I supposed for normal
variables, it could be done with %%private, but it doesn’t look possible for
arrow functions.

Thanks for any idea.

2 Likes

I actually fixed this in Storybook but there are some subtle differences between export { x as default } and export default x which I also explain in that PR. It’d be great if ReScript could do the same thing (and also avoid exporting $$default for which there is an existing issue).

2 Likes