Using js-post-build to rename custom files to page.js for NextJs Projects

Hi Everyone!!

So I wanted to build a project using NextJs + Rescript, but the issue is that in rescript you can’t have the same file name, which conflicts with the NextJs folder structure (wherein inside each folder you are needed to have a page.js file to help define the pages). To solve this issue we can have a mix of Js and res files, wherein we are importing the rescript generated components in the page.js file. But I wanted to be consistent and have all my logic in res file, so when I was going though the docs I came across js-post-build config wherein you can define a post build script and so I thought to run a script that renames the compile Something_Page.res.js file to page.res.js (i.e., add a condition saying if the file name has a substring _Page then rename it to page.res.js file). But I don’t know how the filename inside the script to check and rename. And I’m not even able to run a simple script, I guess there’s some issue with the path. Can someone please help me with this. There’s no much docs about the js-post-build config, and if any alternate work around also works.

Thanks!!

Found a thread with a similar idea: Using React server/client components with Rescript (React 18/NextJS 13) - #2 by PolyDevil

Thanks, but I had already gone through this.

I’m getting an error when I try to add the js-post-build config. When I’m removing this config then its compiling successfully. Can you please help me with this?

rescript version : "^11.1.4"

rescript.json file

{
“name”: “test”,
“jsx”: { “version”: 4 },
“sources”: [
{
“dir”: “src”,
“subdirs”: true
},
{
“dir”: “apps/test/app”,
“subdirs”: true
}
],
“package-specs”: {
“module”: “esmodule”,
“in-source”: true
},
“js-post-build”: {
“cmd”: “echo build finished”
},
“suffix”: “.res.js”,
“bs-dependencies”: [“@rescript/core”, “@rescript/react”],
“bsc-flags”: [“-bs-super-errors”, “-open RescriptCore”]
}

FAILED: src/screens/HomePage.cmj
can not handle multiple files
FAILED: cannot make progress due to previous errors.
Finish compiling (exit: 1)

An update, noticed this problem is happening only on windows. I tried this in wsl and masOS and it was working fine.

And the script to rename the file to page.res.js:

#!/ bin/bash

#Check if the filename is provided
if [ -z “$1” ]; then
echo “Usage: $0 <filename>”
exit 1
fi

filename=“$1”

if [[ “$filename” == *“_Page.res.js”* ]]; then
mv “$filename” “page.res.js”
fi

What kind of script did you use on windows? Because yes, bash does not work there. I think the few ppl that use it use it with node scripts.