Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shebang support in reasonML

I'm trying to write a command line tool in reasonML. So I inserted a shebang(#! /usr/bin/env node) at the first line, but the compiler failed to compile it. How do I add a shebang to the compiled output?

like image 301
theJian Avatar asked Mar 19 '18 03:03

theJian


1 Answers

I know of two ways to accomplish this:

1. Use the js-post-build configuration option in bsconfig.json:

Here's an example that uses sed to insert the shebang at the top of the generated js file:

"js-post-build": {
  "cmd": "/usr/bin/sed -i '1 i\\#!/usr/bin/env node'"
}

Documentation

This will execute on just the files that actually. The downside to this is that shebang is not valid javascript, so if you need to parse it later, for example to bundle, it might fail (like rollup, for example). BSB will also behave a bit glitchy, but I had no serious issues with this, just a bit of console spam from the build triggering a few hundred times more than it should.

2. Use rollups banner option:

Webpack and other bundlers might have a similar feature, but I only know how to do this with rollup. Here's an example rollup.config.js configuration:

export default {
  input: `src/main.bs.js`,
  output: {
    file: `bin/main.js`,
    format: 'cjs',
    banner: '#!/usr/bin/env node'
  }
}

Documentation

The downside to this is of course that you have to use rollup, or some other tool that adds a build step you might not otherwise need.

like image 97
glennsl Avatar answered Sep 20 '22 12:09

glennsl