Shebang support in reasonML
Asked Answered
S

1

5

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?

Scarcity answered 19/3, 2018 at 3:31 Comment(1)
Could you compile to native code instead of JS? If it's a CLI, there are nice OCaml libraries which you can trivially reuse in Reason, e.g. github.com/janestreet/shexpJosejosee
F
6

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.

Funnyman answered 19/3, 2018 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.