Skip to content

Plugin to generate shebang / hashbang for Parcel v1.x.x.

License

Notifications You must be signed in to change notification settings

cyntler/parcel-plugin-shebang

Repository files navigation

npm-version npm-download

parcel-plugin-shebang

#!/usr/bin/env node

Do you want your bundles generated by Parcel to have a shebang and it was easy to start from the console? This plugin allows this. You can build CLI applications in a simple way!

Installing

To install the plugin simply download the module.
You can use npm for this.

npm i -D parcel-plugin-shebang

Getting Started

After successful installation, the plugin should work.
If you use this plugin without configuration (which is described below) it will generate for you shebang automagically. However, the plugin only works when it finds a shebang line in source files that are entry points of Parcel.

Without this plugin, if you place a shebang line in the source file, Parcel will leave it in code and treat it the same as normal JavaScript code. Unfortunately, after running this file you will get SyntaxError. This plugin is designed to move the shebang line to the very top of the file so that it does not cause any syntax problems.

Here you can find a problem that tries to solve this plugin:
parcel-bundler/parcel#2381

Configuration

The plugin has an optional configuration that allows you to generate shebang in bundle files. This is useful if you want your source files to not have a shebang.

There are two ways to configure the plugin:

  • You can use a dedicated configuration .shebangrc file.
  • Alternatively, you can save the configuration in the package.json file.

The configuration of the plugin is an array of objects describing the name of the interpreter and the array of files for which this interpreter is to be used.

An array can contain many objects.
If the path to the file repeats in the next object, the interpreter from the first passed object containing the given file will be used.

Name Type Description
interpreter string Name of interpreter.
files string[] Array of relative paths to the files in which the interpreter is to be used.

.shebangrc

[
  {
    "interpreter": "node",
    "files": [
      "./cli.ts"
    ]
  }
]

package.json

"shebang": [
  {
    "interpreter": "node",
    "files": [
      "./cli.ts"
    ]
  }
]

Now if you run the command:

parcel build ./cli.ts

The resulting file should contain a shebang with a node interpreter.