rescript-lang / syntax

ReScript's syntax as a standalone repo.
MIT License
254 stars 38 forks source link

Support for preprocessor #205

Closed varnerac closed 1 year ago

varnerac commented 3 years ago

Is it possible to get the Bucklescript preprocessor working in ReScript?

IwanKaramazow commented 3 years ago

It hasn't been implemented yet, but is possible. What is your use case?

varnerac commented 3 years ago

We have few in mind.

First, we are looking to conditionally add/remove functionality. Our project includes a large validation library as a dependency. We'd like to be able to IFDEF out calls to it for a "light" version, for folks who don't need that or in environments with tough network constraints. For example:

let validate = (s: string) =>
  #ifdef BIGLIB
  Biglib.validate(s)
  #endif
  #elif
  Ok(s)
  #endif

There are other cases where we'd like to bundle different features in different distributions. In other languages we'd use a plugin model. At first glance, a preprocessor seems natural in ReScript. We'd definitely try to isolate the features we switch on and off to a small module or collection of small modules.

TheSpyder commented 3 years ago

I began using preprocessing a couple of months ago to create a better debug implementation. When the -bs-g compiler flag is not present, my debug log methods become identity externals. The aim is to completely remove debug statements from the compiled JS and reduce bundle size.

I have since expanded it to greatly increase a constant that controls how many runs mu property tests execute. they finish in a few seconds while developing but then in CI we sed out the -bs-g flag and tests take 5 minutes to finish.

varnerac commented 3 years ago

That reminds me of another use case. Sometimes we need to run on NodeJS and replace something built-in the browser like DOMParser with a library.

TheSpyder commented 3 years ago

This came up today in Discord with a pretty good use case, adjusting code based on NODE_ENV=production https://github.com/yawaramin/dbc/blob/34e27d573539932e1a25aad7f679787e51ffe3d9/src/Yawaramin__Dbc.ml

woeps commented 3 years ago

For our usecase we need to swap the config module if we compile & bundle for production. Conditional compilation is the only reason why we need a ml file in our codebase.

stale[bot] commented 1 year ago

The rescript-lang/syntax repo is obsolete and will be archived soon. If this issue is still relevant, please reopen in the compiler repo (https://github.com/rescript-lang/rescript-compiler) or comment here to ask for it to be moved. Thank you for your contributions.