# webpack-shebang-plugin

> This is an all-in-one webpack plugin which prepends hashbangs automatically to the generated bundle files and make it executable -- all revived from your entry source file.

Latest version **1.1.8** (published 2022-08-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install webpack-shebang-plugin
pnpm add webpack-shebang-plugin
yarn add webpack-shebang-plugin
bun add webpack-shebang-plugin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.8 |
| Published | 2022-08-03 |
| First published | 2021-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | qiangyizhou |
| Maintainers | shuangwhywhy |
| Keywords | shebang, hashbang, webpack, plugin, webpack-shebang-plugin, shebang-plugin, webpack-shebang, shebang-webpack, shebang-loader, shebang2-loader, loader, executable, bin, unix, #!, #!/, node, nodejs, npm, #!/usr/bin/env, #!/usr/bin/env node |

## Links

- npm: https://www.npmjs.com/package/webpack-shebang-plugin
- Repository: https://github.com/shuangwhywhy/webpack-shebang-plugin
- npm.io page: https://npm.io/package/webpack-shebang-plugin

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 1.1.8 (latest) — 2022-08-03
- 1.1.7 (prod-1.1.7) — 2021-10-16
- 1.1.6 — 2021-06-09
- 1.1.5 — 2021-06-09
- 1.1.4 — 2021-01-06
- 1.1.3 — 2021-01-06
- 1.1.2 — 2021-01-06
- 1.1.1 — 2021-01-05
- 1.1.0 — 2021-01-05
- 1.0.9 — 2021-01-05
- 1.0.8 — 2021-01-05
- 1.0.7 — 2021-01-05
- 1.0.6 — 2021-01-05
- 1.0.5 — 2021-01-05
- 1.0.4 — 2021-01-05
- … 4 more at https://npm.io/package/webpack-shebang-plugin/versions

## README

# Introduction

This is an all-in-one webpack plugin which prepends hashbangs automatically to the generated bundle files and make it executable -- all revived from your entry source file.

*You can actually use this plugin to do anything that BannerPlugin is able to do, by changing the default pattern and the mark in your source files. Please see **More Usage** demo for more details.*

This plugin embeds a simple loader which deals with the hashbang syntax OR any syntax you can specify as a regular expression. You don't need any other dependencies or libs, such as shebang-loader, BannerPlugin, nor do you need extra configurations.

# Requirements

webpack >= 4.0.0 is required.

# Installation

In npm:
```
npm install -D webpack-shebang-plugin
```
Or in yarn:
```
yarn add -D webpack-shebang-plugin
```

# Simple Usage

#### Entry JS file:

``` javascript

#!/usr/bin/env node

// The first line is the shebang you want be added.
// Don't worry about the spaces and line breaks around it.

// You can add shebang mark anywhere in any source file,
// but only if the first meaningful line of your entry file matches the pattern,
// it will be regarded as the shebang and will be prepended to the output bundle,
// all the other useless shebang marks will be removed in the output.
console.log('this is your entry JS file.');

```

---

#### webpack.config.js:

``` javascript

const ShebangPlugin = require('webpack-shebang-plugin');

// ...other webpack configuration

plugins: [
    // ...other webpack plugins

    new ShebangPlugin()

    // ...other webpack plugins
]

// ...other webpack configuration
```

---

#### The output bundle looks like:

``` javascript
#!/usr/bin/env node

/******/ (() => { // webpackBootstrap
/******/ 	var __webpack_modules__ = ({
/*
  ...... generated code ......
 */
```

# Configuration

``` javascript
new ShebangPlugin({
    // optional, you can specify a different regular expression here for your own pattern.
    // the pattern below is used by default, if unset. It matches syntax like:
    //      #!........
    // The regular expression should contain a group of the main shebang part as $1, in the above case,
    // the shebang part "#!........" will be grouped out.
    // * If you create one of your own, you should keep sure that the main part will be grouped out as $1,
    //   and it will be used as your shebang.
    // * If you are not sure how to write your regular expression, please just leave it unset.
    shebangRegExp: /[\s\n\r]*(#!.*)[\s\n\r]*/gm,

    // optional, you can specify r/w/e permissions in octal value.
    // The default value is 0o755, which makes the output bundle executable.
    // You can set the value to 0, if you want to keep the default permissions.
    chmod: 0o755,
})
```

# More Usage

### To use this plugin as a general banner plugin purpose:

Suppose you have two different entries, and you wish to have two output bundles:

- **dist/**
    - *bundle1.js*
    - *bundle2.js*
- **src/**
    - *entry1.js*
    - *entry2.js*
    - *import-in-first-bundle.js*
- *webpack.config.js*

#### src/entry1.js

``` javascript
/***
This is my custom banner
I want this block appear
in my first bundle.
***/

require('./required-by-entry1.js');

console.log('my first bundle');
```

#### src/import-in-first-bundle.js

``` javascript
/***
This block also matches the
custom banner pattern, but
because this file is not the entry
asset, so this block of content
will be abandoned.
***/

console.log('imported in first bundle');
```

#### src/entry2.js

``` javascript
console.log('my second bundle');
```

#### webpack.config.js

``` javascript
const path = require('path');
const ShebangPlugin = require('webpack-shebang-plugin');

module.exports = {
    mode: 'production',
    target: 'node',
    entry: {
        first: {
            import: ['./src/entry1.js', './src/import-in-first-bundle.js'],
            filename: 'bundle1.js'
        },
        second: {
            import: './src/entry2.js',
            filename: 'bundle2.js'
        },
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: [
                    /node_modules/
                ],
                use: [
                    {
                        loader: 'babel-loader'
                    }
                ]
            }
        ]
    },
    plugins: [
        new ShebangPlugin({
            shebangRegExp: /[\s\n\r]*(\/\*{3}[\s\S]*?\*{3}\/)[\s\n\r]*/gm
        })
    ]
}
```

### The dist files look like:

#### dist/bundle1.js

``` javascript
/***
This is my custom banner
I want this block appear
in my first bundle.
***/
// entry1.js code here
// import-in-first-bundle.js code here
```

#### dist/bundle2.js

``` javascript
// entry2.js code here
```

# Author

qiangyizhou@bilibili.com

---
_Source: https://npm.io/package/webpack-shebang-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
