Skip to content

👺 Rollup plugin that provides file watching, copy, transform, repath and rename capabilities.

License

Notifications You must be signed in to change notification settings

panoply/rollup-plugin-globlin

Repository files navigation


rollup-plugin-globlin

A rollup plugin that provides file watching, copy, transform, repath and rename capabilities.

Why?

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.

Install

pnpm i rollup-plugin-globlin --save-dev

Usage

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,
    }),
  ],
};

Transforms

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:

Transforming multiple files

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
      }
    },
  },
}),

Transforming all files

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
      }
    },
  },
}),

License

This package licensed under MIT.

Related

Author

🥛 Νίκος Σαβίδης