rollup-plugin-insert

string mutation plugin for Rollup

Usage no npm install needed!

<script type="module">
  import rollupPluginInsert from 'https://cdn.skypack.dev/rollup-plugin-insert';
</script>

README

rollup-plugin-insert

npm David Peer David

string mutation plugin for Rollup

Usage

# npm
npm install -D rollup-plugin-insert

# yarn
yarn add -D rollup-plugin-insert
// 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')

Common Usage

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:

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.

Append

Appends a string onto the contents.

insert.append('world') // Appends 'world' to the contents of every file

Prepend

Prepends a string onto the contents.

insert.prepend('Hello') // Prepends 'Hello' to the contents of every file

Wrap

Wraps the contents with two strings.

insert.wrap('Hello', 'World') // prepends 'hello' and appends 'world' to the contents

Transform

Calls a function with the contents of the file.

insert.transform((magicString, code, id) => code.toUpperCase()) // should return a string or MagicString