Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

webbureaucrat
webbureaucrat

Posted on • Originally published atwebbureaucrat.gitlab.io on

     

Setting Up Webpack for ReScript

As much as Istrongly 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 yourbsconfig.json and set themodule property of thepackage-specs object to"commonjs" if it is not already set.
  2. Install webpack locally by runningnpm i webpack webpack-cli.

Configuring webpack

It's important to note for your configurationwhere your javascript.bs.js files are output, and this is controlled by thein-source property of the samepackage-specs object inbsconfig.json. This guide assumesin-source isfalse (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 samplewebpack.config.js file based on those assumptions.

constpath=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 modeExit fullscreen mode

This configuration assumes that we should process two output filesindex.bs.js andabout.bs.js (and their dependencies) and then outputs each bundled file by their name ("index" and "about") into the folder calleddist/. The resulting bundles aredist/index.js anddist/about.js.

Including webpack in the build

You're welcome to runnpx 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 modeExit 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.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

functional programming enthusiast and civic tech nerd.
  • Location
    Chicago, IL
  • Education
    Bachelor of Science, Computer Science, DePaul University
  • Work
    Software Developer
  • Joined

More fromwebbureaucrat

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp