An open API service indexing awesome lists of open source software.

https://github.com/ahrefs/melange-from-scratch

Configure a melange project from scratch
https://github.com/ahrefs/melange-from-scratch

Last synced: 16 days ago
JSON representation

Configure a melange project from scratch

Awesome Lists containing this project

README

          

# Melange from scratch

![Melange from scratch](melange-from-scratch.png)

A step-by-step guide to configure a Melange project, starting from the [standard OCaml starter project](https://ocaml.org/docs/your-first-program) (`opam exec -- dune init proj hello`) and progressing through setup until you have a working React application compiled from Reason.

## Quick Start

```bash
make install # Install dependencies
make dev # Build and start dev server
```

Open http://localhost:8080 to see the React app!

## Melange configuration from scratch steps

### 1. Start with OCaml project ([commit](../../commit/56f7bf2))
```bash
opam exec -- dune init proj hello
```

### 2. Add Melange dependency ([commit](../../commit/f7df4b5))
Add to `dune-project`:
```diff
(depends
ocaml
dune
+ melange)
```

### 3. Configure Melange compilation ([commit](../../commit/eb98b02))

**dune-project:** Enable Melange
```diff
+(using melange 0.1)
```

**bin/dune:** Change from executable to melange.emit
```diff
-(executable
- (public_name hello-melange)
- (name main)
+(melange.emit
+ (target output)
(libraries hello_melange)
+ (module_systems commonjs))
```

**lib/dune:** Add melange mode
```diff
(library
(name hello_melange)
+ (modes melange))
```

**dune-project:** Add allow_empty to package
```diff
+ (allow_empty)
```
*Note: Required because `melange.emit` doesn't create installable artifacts like `executable` does*

### 4. Install Reason ([commit](../../commit/ae7576b))
Add to `dune-project`:
```diff
- (depends ocaml dune melange)
+ (depends ocaml dune melange reason)
```

### 5. Add Reason example file ([commit](../../commit/24bdd91))
Create `helloReason.re` with simple example

### 6. Install melange-webapi bindings ([commit](../../commit/b6e41a4))

**dune-project:** Add melange-webapi dependency
```diff
- (depends ocaml dune melange reason)
+ (depends ocaml dune melange reason melange-webapi)
```

**bin/dune:** Add melange-webapi library
```diff
(melange.emit
(target output)
- (libraries hello_melange)
+ (libraries hello_melange melange-webapi)
(module_systems commonjs))
```

### 7. DOM manipulation example ([commit](../../commit/c102b01))
Use melange-webapi to manipulate the DOM

### 8. Add esbuild bundler ([commit](../../commit/a54b34e))
Bundle JavaScript for browser with minification

### 9. Add development server ([commit](../../commit/070812a))
Install http-server and add `make dev` command

### 10. Add Day.js with bindings ([commit](../../commit/cc21888))
- Install Day.js npm package
- Create Reason bindings for Day.js
- Display formatted dates

### 11. Add Reason React ([commit](../../commit/5ae5c24))
- Install React and reason-react
- Create interactive counter component
- Full React app with hooks