A rollup plugin that provides file watching, copy, transform, repath and rename capabilities.
The vast majority of rollup plugins which provide file transforms and/or copies were a bunch of smoke and mirrors and kept falling short for what I required.
pnpm i rollup-plugin-globlin --save-dev
import globs from "rollup-plugin-globlin";
export default {
input: "src/index.js",
output: {
dir: "output",
format: "cjs",
},
plugins: [
globs({
globs: ["dir/**/*.json", "assets/*.svg"],
dest: "dest",
clean: true,
transform: object | function,
}),
],
};
The plugin transform
option will allow you to rename, repath and/or modify contents of a file. The transform option accepts either an object
or function
which will supply an object parameter argument. Transforms must return either a string or object value. When an transform returns a string
it is interpreted as a combination rename and repath, see below example:
globs({
globs: ["dir/**/*.json", "assets/*.svg"],
dest: "dest",
transform: {
"img/**/file.png": "rename.png" // string without slash will rename the file
"image.jpg": "images/prepend-[name].[ext]" // string with slash / will repath from dest/
"data/*.json": ({ content }) => {
// Returning a function on all '.json' files and do some modifications
// For example, lets beautify these JSON files with an indentation of 4
const json = JSON.stringify(JSON.parse(content), null, 4)
// Returning an object using you can repath and rename the files using the
return {
content: json, // the transformed contents, should always be a string!
file: `new-[name].json`, // prepend `new-` to these all filenames
dest: 'json/dest' // output files to a new destination relative to workspace root
}
},
},
}),
import { minify } from 'html-minifier'
globs({
globs: ["views/*.html"],
dest: "pages",
transform: ({ content }) => {
// This will modify all files referenced in globs
// for example, let minify all HTML files
const html = minify(content.toString());
return {
content: html, // the transformed contents, should always be a string!
file: `[name].liquid`, // all files should use a `.liquid` extension
}
},
},
}),
This package licensed under MIT.