rollup-plugin-insert
Version:
string mutation plugin for Rollup
77 lines (52 loc) • 1.94 kB
Markdown
[](https://www.npmjs.com/package/rollup-plugin-insert)
[](https://david-dm.org/rx-ts/rollup?path=packages/insert&type=peer)
[](https://david-dm.org/rx-ts/rollup?path=packages/insert)
> string mutation plugin for Rollup
```bash
npm install -D rollup-plugin-insert
yarn add -D rollup-plugin-insert
```
```js
// all of following is fine
import * as insert from 'rollup-plugin-insert'
import insert from 'rollup-plugin-insert'
import { append, prepend, wrap, transform } from 'rollup-plugin-insert'
const insert = require('rollup-plugin-insert')
```
All following methods have an optional last argument `options` which is an object and contains key `include`, `exclude` and `sourceMap` which is enabled by default.
It can be used to filter files as you like. For example you can wrapper your html template as following:
```js
insert.transform(
(magicString, code, id) =>
`export default ${JSON.stringify(`<!--add some comments-->${code}`)}`,
{
include: '**/*.html',
},
)
```
If you do not need `sourceMap` at all, just change it to be `false`.
Appends a string onto the contents.
```js
insert.append('world') // Appends 'world' to the contents of every file
```
Prepends a string onto the contents.
```js
insert.prepend('Hello') // Prepends 'Hello' to the contents of every file
```
Wraps the contents with two strings.
```js
insert.wrap('Hello', 'World') // prepends 'hello' and appends 'world' to the contents
```
Calls a function with the contents of the file.
```js
insert.transform((magicString, code, id) => code.toUpperCase()) // should return a string or MagicString
```