# prescript-webpack-plugin

> Webpack plugin for running scripts before build

Latest version **0.2.0** (published 2020-09-05) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-09-05 |
| First published | 2018-05-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 102.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sonny Johannesson |
| Maintainers | tellios |
| Keywords | webpack, plugin, prescript |

## Links

- npm: https://www.npmjs.com/package/prescript-webpack-plugin
- Repository: https://github.com/Tellios/prescript-webpack-plugin
- Homepage: https://github.com/Tellios/prescript-webpack-plugin#readme
- Issues: https://github.com/Tellios/prescript-webpack-plugin/issues
- npm.io page: https://npm.io/package/prescript-webpack-plugin

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2020-09-05
- 0.1.3 — 2019-06-05
- 0.1.2 — 2019-06-05
- 0.1.1 — 2018-06-07
- 0.1.0 — 2018-05-03

## README

# prescript-webpack-plugin

Prescript is intended for those rare cases where you want to run scripts before transpiling/bundling your code with webpack.

A typical example usage is to auto-generate e.g typescript typings based on often changing json files like language files. On every run of webpack the scripts will be run before any transpiling or bundling takes place.

# Installing

```shell
npm install prescript-webpack-plugin
```

# Usage

```js
const PrescriptWebpackPlugin = require('prescript-webpack-plugin');
```

Add the plugin to your webpack `plugins` section:

```js
plugins: [
    new PrescriptWebpackPlugin({
        // Scripts are executed in the order they are added
        // The different types of scripts are listed further down in the documentation
        scripts: [
            {
                type: 'node',
                args: ['arg1'],
                scriptFile: path.join(__dirname, 'prescript.js')
            },
            {
                type: 'node',
                args: [1, 'param2'],
                script: (param1, param2) => {
                    console.log(param1, param2);
                }
            }
        ]
    })
];
```

# Prescript types

## node

Node scripts are plain javascript that can be executed either as an inline script or as a separate process using a .js file as input.

| Property         | Description                                                                          |
| ---------------- | ------------------------------------------------------------------------------------ |
| type             | Must be set to 'node'                                                                |
| interpreter      | Optional. Specify an alternative node executable if required when using `scriptFile` |
| scriptFile       | Absolute path to script file that should be run, can't be combined with `script`     |
| script           | A function that should be run, can't be combined with scriptFile                     |
| args             | Optional. Must be strings when using `scriptFile`                                    |
| throwOnError     | If a build should fail on an error or continue silently                              |
| workingDirectory | The working directory used when executing `scriptFile`, defaults to webpack's        |

### Examples

Using `scriptFile`:

```js
{
    type: 'node',
    args: ['arg1'],
    scriptFile: path.join(__dirname, 'prescript.js'),
    interpreter: '/some/path/to/node',
    workingDirectory: '/work/dir/for/node',
    throwOnError: true
}
```

Using `script`:

```js
{
    type: 'node',
    args: [1, 'param2'],
    script: (param1, param2) => {
        console.log(param1, param2);
    }
}
```

## ts-node

Similar to `type: 'node'` but executes a script file using [ts-node](https://www.npmjs.com/package/ts-node) instead which allows you to use TypeScript.

| Property         | Description                                                                   |
| ---------------- | ----------------------------------------------------------------------------- |
| type             | Must be set to 'ts-node'                                                      |
| interpreter      | Optional. Specify an alternative ts-node executable if required               |
| args             | Optional. Must be strings                                                     |
| throwOnError     | If a build should fail on an error or continue silently                       |
| workingDirectory | The working directory used when executing `scriptFile`, defaults to webpack's |

### Examples

Using `scriptFile`:

```js
{
    type: 'ts-node',
    args: ['arg1'],
    scriptFile: path.join(__dirname, 'prescript.ts'),
    interpreter: '/some/path/to/ts-node',
    workingDirectory: '/work/dir/for/ts-node',
    throwOnError: true
},
```

## shell

Run any shell command, this could for example enable you to run python scripts or anything else that could be needed.

| Property         | Description                                                                |
| ---------------- | -------------------------------------------------------------------------- |
| type             | Must be set to 'shell'                                                     |
| command          | The shell command you want to execute                                      |
| args             | Optional. Must be strings                                                  |
| throwOnError     | If a build should fail on an error or continue silently                    |
| workingDirectory | The working directory used when executing `command`, defaults to webpack's |

### Examples

```js
{
    type: 'shell',
    command: 'echo',
    args: ['hello', 'world'],
    workingDirectory: '/work/dir/for/command',
    throwOnError: true
},
```

# Common issues

Since prescripts are executed just before webpack's build triggers it can cause issues when modifying files that webpack works with during building in combination with webpack's watchers. To accomodate this, you can tweak the amount milliseconds that need to elapse before executing prescripts again.

```js
plugins: [
    new PrescriptWebpackPlugin({
        millisecondsBetweenRuns: 500, // 500 ms is the default
        scripts: [
            /* ... */
        ]
    })
];
```

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