0.2.1 • Published 2 years ago
rollup-plugin-write-output v0.2.1
rollup-plugin-write-output
Inject output chunks to HTML or JSON files. This plugin is often used with iife or environments without a module loader.
Installation
npm install -D rollup-plugin-write-outputUsage
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
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 oftarget. Iftargetis a JSON file,contentwill 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 totargetand always use forward slash/.context.htmlScripts- HTML script tags that can be injected to HTML directly. It equals toscripts.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.