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?
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'"
}
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 rollup
s 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'
}
}
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.
© 2022 - 2024 — McMap. All rights reserved.