• Stars
    star
    164
  • Rank 230,032 (Top 5 %)
  • Language
    OCaml
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✨🐒 Dead simple tool for seamlessly integrating ReasonML into existing JavaScript projects.

add-reason

Simple tool for seamlessly integrating ReasonML into your existing JavaScript projects.

This is an easy to use cli tool that helps you seamlessly interop ReasonML code with an existing JavaScript project. You tell us where your ReasonML code is, and we'll handle the rest!

Installation

$ npm install -g add-reason

or

$ yarn global add add-reason

Usage

The most basic use case would be to simply add an interop between your JavaScript and ReasonML code. In this case, you'd probably want to go with the setup command and specify the location of your ReasonML code and optionally include the name of a package you'd want to give it.

Let's assume you had file structure like this, where all your ReasonML code is located in a single directory called reasonCode or something:

myCoolProject/
β”œβ”€β”€ reasonCode/
β”‚   β”œβ”€β”€ index.re
β”‚   β”œβ”€β”€ file1.re
β”‚   β”œβ”€β”€ file2.re
β”‚   └── utils/
β”‚       β”œβ”€β”€ fs.re
β”‚       └── path.re
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ main.js
β”‚   β”œβ”€β”€ example1.js
β”‚   β”œβ”€β”€ example2.js
β”‚   └── example3.js
└── etc/

You'd simply go to your project root and type:

$ add-reason setup ./reasonCode

or, since setup is the default command, you can even write

$ add-reason ./reasonCode

and that's it! Now you can compile you ReasonML code with

$ npm run build-reason
# or
$ bsb -make-world

and then import your compiled ReasonML code with

const reasonCode = require('reasonCode');
// ...

If you don't specify a package name you want to give your code, we just use the directory name. You can choose any name you want though, so long as it doesn't collide with the name of a package you already have in your node_modules/!

$ add-reason setup ./reasonCode my-cool-code
const myCoolCode = require('my-cool-code');
// ...

You can do a variety of other things too, the more basic usage is:

Usage: add-reason [command] [options]

  Commands:

    setup <directory> [package-name]  set up Reason directory, config files, and symlink
    link <directory> [package-name]   create a symlink with the given package name
    linter <directory>                create a merlin file for linting your ReasonML code
    config <directory>                create a bsconfig file for building your ReasonML code

  Options:

    -h, --help     output usage information
    -V, --version  output the version number
    --no-linking   don't create the symlink to your compiled ReasonML code
    --in-source    change target to in-source rather than a compiled directory

How It Works

This is a very simple tool. All it does is make sure you have the typical basic config related files for writing ReasonML and creates a symlink in your node_modules/ targeting your compiled ReasonML code so you can easily import and use it within your JavaScript.

There are a few steps that happen when you call setup:

  1. Prepare target directory
  2. Make sure we have a config file
  3. Make sure we have a linting file
  4. Create symlink
  5. Create build command
  6. Create postinstall
  7. Check for bs-platform

Preparing the target directory just makes sure that the path ./lib/js/[your ReasonML directory name] exists. This is where BuckleScript will put your compiled ReasonML/OCaml code, so this is the spot where we want to create a symlink later to down the line.

Next we just want to make sure you're all set up with at least the most basic of configurations. We check to see if you have a .merlin file for linting and a bsconfig.json for ReasonML configuration. If you don't, no worries! We will just create a basic file for each one that you don't have and set you up with it.

Then we just create that symlink that hooks in your compiled ReasonML code to your node_modules/ for easy access. We also create a postinstall script that will add this symlink every time someone installs your project, given that the link doesn't already exist. You always have the option to skip this symlinking step with the --no-linking flag too if you want.

We also add a script to your package.json called build-reason that simply uses Bucklescript to compile your code. So you can then call npm run build-reason after calling setup to have your ReasonML code compiled and ready to use!

The last thing that we do is check to make sure that you have bs-platform integrated into your project. If you don't have it globally installed and linked to your current project, you're prompted to install and link it with

$ npm install -g bs-platform
$ npm link bs-platform

FAQ

I ran setup and everything worked fine, but when I try to import my code I get Error: Cannot find module

This is almost always because your ReasonML directory doesn't have an index.re file. Normally, this isn't a necessary thing, but if you want to be able to write

const reasonCode = require('reasonCode');
// ...

your directory needs to have the index file for node to grab! You don't need to have an index.re file though, instead you can just access the files you want directly.

const reasonCode = require('reasonCode/file1.js');
// ...

I ran setup but my code isn't compiled!

Don't forget to compile your code using bs-platform! You can use the build-reason command that should have been added to your package.json after using the setup command.

$ npm run build-reason

Remember that add-reason doesn't actually compile your code or deal with that bit. Alternatively, you can compile your code yourself manually with BuckleScript something like

$ bsb -make-world

License

This software is free to use under the MIT License. See this reference for license text and copyright information.

More Repositories

1

chs

β™β™ž Play chess against the Stockfish engine in your terminal.
Python
200
star
2

meteorite

✨🌱 Smarter GitHub notifications. Organize and score notifications based on importance and relevance.
JavaScript
111
star
3

notion-draw

Simple drawing app for doodling within Notion.
TypeScript
56
star
4

infrared

βœ¨πŸš€ Blazing fast, inferred static type checker for JavaScript.
OCaml
47
star
5

chalk

πŸ–οΈπŸŽ¨Composable and simple terminal highlighting package for OCaml.
OCaml
30
star
6

needle

πŸ“ŒπŸ“š An extensive standalone data structure library for JavaScript.
JavaScript
25
star
7

lets-make-a-compiler

✨🌿 Compiler written from scratch for JavaScript-like language to x86-64 assembly.
OCaml
10
star
8

levenshtein-lite

πŸš„ πŸ’¨ A lightweight implementation of the Levenshtein distance algorithm.
JavaScript
9
star
9

cross-wordle

✨ πŸ„ A daily crossword game – Like a tiny game of Bananagrams or Scrabble, generated each day.
TypeScript
9
star
10

markup

✨🐒 Create & share TeX snippets with rich math typesetting and markdown support.
JavaScript
7
star
11

BitVector

C++ implementation of a bit vector data structure
C++
5
star
12

subtle

A subtle theme for VSCode.
4
star
13

silk-scroll

πŸš€πŸ¦Cross browser responsive smooth scrolling for entire web applications.
JavaScript
4
star
14

pulse

Synchronous function queuing to organize event firing.
JavaScript
3
star
15

typed-lite

A lightweight module for typing animations in vanilla javascript.
JavaScript
2
star
16

blindfold

Reduce self imposed anxiety by hiding LeetCode's difficulty, acceptance, and question stats.
JavaScript
2
star
17

jaz

πŸŽ·πŸ”—Seamlessly traverse web pages and control page rendering intermission on the client.
JavaScript
2
star
18

trumpit

🎺 Generate topical and favorable political speeches.
CSS
2
star
19

material-paper

React Component of paper material inspired by Google's Material Design.
JavaScript
2
star
20

twitter-forensics

πŸ₯ πŸ”Ž Record and track some Twitter related information regarding your followers.
JavaScript
1
star
21

TypeSoundnessCertifier

Certified Type Soundness, automatically!
AMPL
1
star
22

doxide

βš‘πŸ”§A tool for transforming documentation in JavaScript files to markdown.
JavaScript
1
star
23

LRUCache

A doubly linked hashmap implementation of a Least Recently Used (LRU) cache in C++
C++
1
star
24

scrambled

🍳 πŸ₯š A daily crossword game – Like a tiny game of Bananagrams or Scrabble, generated each day.
TypeScript
1
star