# rollup-plugin-write-output

> Inject output chunks to HTML or JSON files.

Latest version **0.2.1** (published 2024-02-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install rollup-plugin-write-output
pnpm add rollup-plugin-write-output
yarn add rollup-plugin-write-output
bun add rollup-plugin-write-output
```

## 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.1 |
| Published | 2024-02-16 |
| First published | 2020-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | eight04 |
| Maintainers | eight04 |
| Keywords | rollup-plugin, inject, output |

## Links

- npm: https://www.npmjs.com/package/rollup-plugin-write-output
- Repository: https://github.com/eight04/rollup-plugin-write-output
- Homepage: https://github.com/eight04/rollup-plugin-write-output#readme
- Issues: https://github.com/eight04/rollup-plugin-write-output/issues
- npm.io page: https://npm.io/package/rollup-plugin-write-output

## 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.1 (latest) — 2024-02-16
- 0.2.0 — 2022-11-26
- 0.1.2 — 2022-02-10
- 0.1.1 — 2021-02-12
- 0.1.0 — 2020-08-19

## README

rollup-plugin-write-output
==============================

![.github/workflows/build.yml](https://github.com/eight04/rollup-plugin-write-output/workflows/.github/workflows/build.yml/badge.svg)
[![codecov](https://codecov.io/gh/eight04/rollup-plugin-write-output/branch/master/graph/badge.svg)](https://codecov.io/gh/eight04/rollup-plugin-write-output)
[![install size](https://packagephobia.now.sh/badge?p=rollup-plugin-write-output)](https://packagephobia.now.sh/result?p=rollup-plugin-write-output)

Inject output chunks to HTML or JSON files. This plugin is often used with [iife](https://www.npmjs.com/package/rollup-plugin-iife) or environments without a module loader.

Installation
------------

```
npm install -D rollup-plugin-write-output
```

Usage
-----

```js
import iife from "rollup-plugin-iife";
import output from "rollup-plugin-write-output";

export default {
  input: ["foo.js", "bar.js"],
  output: {
    dir: "dist/js",
    format: "es"
  },
  plugins: [
    iife(),
    output([
      {
        test: /foo\.js/,
        target: "dist/foo.html",
        handle: (content, {htmlScripts}) =>
          content.replace("</body>", `${htmlScripts}</body>`)
      },
      {
        test: /bar\.js/
        target: "dist/manifest.json",
        handle: (content, {scripts}) => {
          content.files = scripts;
          return content;
        }
      }
    ])
  ]
};
```

API
----

This module exports a single function.

### createPlugin

```js
const plugin = createPlugin([
  {
    test: RegExp,
    target: String,
    handle: async (
      content: String | JSON,
      context: {
        scripts: Array<String>,
        htmlScripts: String
      }
    ) => newContent: String | JSON
  },
  ...
]);
```

This function accepts a list of targets.

`test` is a regular expression matching the output filename.

`target` is the path to the file that you want to modify. You can use `$1`, `$2`, `$3`, etc, to reference the capture group matched by `test`.

`handle` is a function that injects scripts into `target`.

* `content` - the original content of `target`. If `target` is a JSON file, `content` will be parsed into an JSON object.
* `context.scripts` - list of path to output chunks, including the output file and its dependencies. The list is ordered so dependencies load first. Paths are relative to `target` and always use forward slash `/`.
* `context.htmlScripts` - HTML script tags that can be injected to HTML directly. It equals to ``scripts.map(s => `<script src="${s}"></script>`).join("")``.

Scripts are ordered and dependencies will be loaded first.

Changelog
---------

* 0.2.1 (Feb 16, 2024)

  - Bump deps, support rollup 4.

* 0.2.0 (Nov 26, 2022)

  - **Breaking: bump to rollup@3**.*

* 0.1.2 (Feb 10, 2022)

  - Fix: handle generated assets.

* 0.1.1 (Feb 12, 2021)

  - Add: an import resolver to guaratee script execution order.

* 0.1.0 (Aug 19, 2020)

  - Initial release.

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