Logo

dev-resources.site

for different kinds of informations.

Setting Up Webpack for ReScript

Published at
3/24/2021
Categories
bucklescript
reason
rescript
webpack
Author
webbureaucrat
Author
13 person written this
webbureaucrat
open
Setting Up Webpack for ReScript

As much as I strongly prefer ES6 modules, using them with ReScript (formerly BuckleScript / ReasonML) and ServiceWorkers just isn't practical right now. I'm writing this article so that I can easily grab the configuration the next time I need it. This is a beginner's guide because I am a webpack beginner, and, well, everyone is a ReScript beginner right now.

Some basic setup

  1. Open your bsconfig.json and set the module property of the package-specs object to "commonjs" if it is not already set.
  2. Install webpack locally by running npm i webpack webpack-cli.

Configuring webpack

It's important to note for your configuration where your javascript .bs.js files are output, and this is controlled by the in-source property of the same package-specs object in bsconfig.json. This guide assumes in-source is false (because, quite frankly, that's my preference) but it means that the .bs.js outputs get buried in a deeply nested folder structure.

This is a sample webpack.config.js file based on those assumptions.

const path = require('path');
module.exports = 
{ 
    entry: 
    { 
        index: "./lib/js/src/index.bs.js", 
        about: "./lib/js/src/about.bs.js" 
    }, 
    output: 
    { 
        filename: "[name].js", 
        path: path.resolve(__dirname, "dist/"), 
    }
};
Enter fullscreen mode Exit fullscreen mode

This configuration assumes that we should process two output files index.bs.js and about.bs.js (and their dependencies) and then outputs each bundled file by their name ("index" and "about") into the folder called dist/. The resulting bundles are dist/index.js and dist/about.js.

Including webpack in the build

You're welcome to run npx webpack any time you want to regenerate your bundled files, but it's a good automation practice to add it to your build command like so:

"scripts": 
{ 
    "build": "npx bsb -make-world && npx webpack", 
    "start": "npx bsb -make-world -w", 
    "clean": "npx bsb -clean-world"
}
Enter fullscreen mode Exit fullscreen mode

In conclusion

I'm still not a fan of script bundlers and avoid them wherever possible, but when it's not possible, it's nice to have a configuration pasta on hand. In a future article, I'll talk about my main use for webpack: ServiceWorkers.

reason Article's
30 articles in total
Favicon
Top 6 Reasons to Partner with Experts in Mobile App Design and Backend Development
Favicon
South Delhi Real Estate: Unveiling the Charm of Residential Houses
Favicon
How Reading Can Polish Your Learning skills
Favicon
Reason and React Meta-Frameworks
Favicon
NextJS, the App Router and ReasonReact
Favicon
ReasonReact, Auth0 and 3rd Party React Components
Favicon
Getting started with ReasonReact and Melange
Favicon
Top 9 JavaScript Flavours
Favicon
React Memory Leaks: what, why, and how to clean them up!
Favicon
The 3 Main Reasons Test Automation Projects Fail
Favicon
Comparison of Type Systems in Front-end Languages: Algebraic data types
Favicon
Editor Support for ReasonML in VSCode with Melange
Favicon
Writing Elm Ports in ReScript - 0.3
Favicon
5 Reasons That Make React Native Better Than Flutter
Favicon
Async await like syntax, without ppx in Rescript !!!
Favicon
Why React Needs Keys, Why It Matters
Favicon
Using `let.opt` in Rescript with latest Reason/OCaml
Favicon
From Reason/React to Rescript/React, Guaranteed Uncurrying
Favicon
Awesome list in rescript
Favicon
JavaScript file watching with Reason & Rescript in Dune
Favicon
Hooray!
Favicon
Getting started with ReScript and parcel
Favicon
Reasons Why Digital Healthcare Startups Meet Failures
Favicon
The strongest hearts of Rwandans
Favicon
Setting Up Webpack for ReScript
Favicon
npx resyntax
Favicon
Ethicode Projects: Contributio #1
Favicon
Displaying Notifications in ReScript
Favicon
How does ReScript affect me?
Favicon
Beating the Drom

Featured ones: