This is the configuration I’m using for ES6 and Jest:
bsconfig.json
{
"name": "demo",
"version": "0.0.1",
"sources": [
{
"dir": "src",
"subdirs": true
},
{
"dir": "__tests__",
"type": "dev"
}
],
"package-specs": {
"module": "es6",
"in-source": true,
"suffix": ".mjs"
},
"namespace": true,
"bs-dependencies": [],
"bs-dev-dependencies": [
"@glennsl/bs-jest"
],
"warnings": {
"error": "+101"
}
}
jest.config.js
module.exports = {
testMatch: ["**/__tests__/*.?(m)js", "**/?(*.)+_test?(.bs).js?(x)"],
moduleFileExtensions: ["js", "jsx", "mjs"]
};
package.json (I'm using pnpm instead of npm, hence the pnpx call in the scripts section for the tests)
{
"name": "demo",
"version": "0.0.1",
"scripts": {
"build": "bsb -make-world",
"clean": "bsb -clean-world",
"start": "bsb -make-world -w",
"test": "NODE_OPTIONS=--experimental-vm-modules pnpx jest",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules pnpx jest --watch"
},
"keywords": [
"rescript"
],
"author": "",
"license": "MIT",
"devDependencies": {
"@glennsl/bs-jest": "^0.7.0",
"bs-platform": "*"
}
}
With Rescript 9.02 I was just having too much trouble trying to get Babel to work and since I really didn’t need Babel for what I what I was doing using the .mjs extension and the Node.js options in my package.json worked best for me. If you need Babel since you’re doing React you might have to drop back to an older version of ReScript and use the .bs.js extension.
I’ve only done a basic React set up with Vite and it takes well to the .mjs extension but I haven’t tried it out with any other 3rd party libraries yet. These are my configs for React with Vite (I found an old ReasonML Vite set up on Github and made changes for it to work with ReScript):
bsconfig.json
{
"name": "rescript-react-vite-starter",
"reason": {
"react-jsx": 3
},
"sources": {
"dir": "src",
"subdirs": true
},
"bsc-flags": [
"-bs-super-errors",
"-bs-no-version-header"
],
"package-specs": [
{
"module": "es6",
"in-source": true,
"suffix": ".mjs"
}
],
"namespace": true,
"bs-dependencies": [
"@rescript/react"
]
}
package.json
"name": "rescript-react-vite",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview",
"res:build": "bsb -make-world -clean-world",
"res:watch": "bsb -make-world -clean-world -w",
"res:clean": "bsb -clean-world"
},
"keywords": [
"rescript",
"rescript-react",
"vite"
],
"dependencies": {
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
"devDependencies": {
"@rescript/react": "^0.10.1",
"@vitejs/plugin-react-refresh": "^1.1.0",
"bs-platform": "9.0.2",
"vite": "^2.0.1"
}
}
vite.config.js
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()]
})
Again, these are using the latest version of ReScript. If you’re using Vite I’m guessing you shouldn’t need Babel because Vite uses ESBuild. But if you really need Babel there are solutions in the forum that have Jest+Babel set ups but you’d likely need to use one of the older versions of ReScript because of the changes made to get ReScript to work with the .mjs extension was breaking stuff that used non-ES6 libraries.
I couldn’t get everything working using the latest ReScript and Babel so the .mjs extension solution worked for me with Jest and Vite seems like it will take care of my frontend ES6 needs without the headache of trying to get Babel working again or going back to an older version of ReScript.