1.0.0 โ€ข Published 6 months ago

@omnihash/rollup-plugin-remove-files v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@omnihash/rollup-plugin-remove-files

A secure Rollup plugin that safely removes files and directories using regex patterns and explicit file paths. Built with security-first design to prevent accidental deletion of source files.

Features

  • ๐Ÿ›ก๏ธ Security First: Only allows deletion from build/dist directories by default
  • ๐ŸŽฏ Regex Support: Use powerful regex patterns to match files
  • ๐Ÿšซ Path Traversal Protection: Prevents ../ attacks and escaping to parent directories
  • ๐Ÿงช Test Mode: Preview what files would be deleted without actually removing them
  • ๐Ÿ“ Detailed Logging: See exactly what's being removed and why operations are blocked
  • โšก Perfect Timing: Runs after build completion when files actually exist
  • ๐Ÿ”ง Flexible Configuration: Customize allowed directories and safety settings

Installation

npm install @omnihash/rollup-plugin-remove-files --save-dev

Quick Start

import { defineConfig } from "rollup";
import remove from "@omnihash/rollup-plugin-remove-files";

export default defineConfig({
  input: "src/index.js",
  output: {
    file: "dist/bundle.js",
    format: "es",
  },
  plugins: [
    remove({
      patterns: [/\.map$/, /\.temp$/],
      logging: true,
    }),
  ],
});

API Reference

Options

OptionTypeDefaultDescription
filePathsstring \| string[][]Explicit file paths to remove
patternsRegExp \| RegExp[] \| string \| string[][]Regex patterns to match files
allowedDirsstring[]['dist', 'build', 'out', '.next/static']Directories where deletion is allowed
baseDirstringprocess.cwd()Base directory for relative paths
strictModebooleantrueOnly allow deletion in allowedDirs
testRunbooleanfalsePreview mode - show what would be deleted
loggingbooleanfalseEnable detailed logging

Usage Examples

Remove Source Maps

remove({
  patterns: [/\.map$/],
  logging: true,
});

Clean Up Temp Files

remove({
  patterns: [/\.temp$/, /cache.*\.json$/],
  filePaths: ["dist/debug.log"],
  logging: true,
});

Test Mode (Safe Preview)

remove({
  patterns: [/\.test\./],
  testRun: true,
  logging: true,
});
// Output: [remove-files] Would remove: dist/app.test.js

Custom Allowed Directories

remove({
  patterns: [/\.log$/],
  allowedDirs: ["dist", "logs", "temp"],
  logging: true,
});

Development vs Production

const isProduction = process.env.NODE_ENV === "production";

remove({
  patterns: isProduction ? [/\.map$/, /\.test\./] : [],
  logging: !isProduction,
});

Security Features

Automatic Safety

  • Build Directory Detection: Automatically allows deletion from Rollup's output directory
  • Path Traversal Protection: Blocks ../ and absolute paths that could escape allowed directories
  • Whitelist Approach: Only specified directories are allowed for deletion

Safety Examples

// โœ… SAFE - Within allowed directory
remove({
  filePaths: ["dist/temp.js"],
});

// โŒ BLOCKED - Outside allowed directory
remove({
  filePaths: ["../secret-file.txt"], // Path traversal blocked
});

// โŒ BLOCKED - Absolute path
remove({
  filePaths: ["/etc/passwd"], // Absolute path blocked
});

Advanced Configuration

Disable Strict Mode (Use with Caution)

remove({
  filePaths: ["src/temp.js"], // Normally blocked
  strictMode: false, // Allows deletion outside allowedDirs
  logging: true, // Will show warnings
});

Multiple Pattern Types

remove({
  patterns: [
    /\.map$/, // RegExp
    ".*\\.temp", // String (converted to RegExp)
    /node_modules.*\\.log$/, // Complex pattern
  ],
});

Integration Examples

With TypeScript

import { defineConfig } from "rollup";
import typescript from "@rollup/plugin-typescript";
import remove from "@omnihash/rollup-plugin-remove-files";

export default defineConfig({
  input: "src/index.ts",
  output: { dir: "dist", format: "es" },
  plugins: [
    typescript(),
    remove({
      patterns: [/\.tsbuildinfo$/, /\.d\.ts\.map$/],
      logging: true,
    }),
  ],
});

With Vite (Rollup-based)

import { defineConfig } from "vite";
import remove from "@omnihash/rollup-plugin-remove-files";

export default defineConfig({
  build: {
    rollupOptions: {
      plugins: [
        remove({
          patterns: [/\.map$/],
          testRun: process.env.NODE_ENV !== "production",
        }),
      ],
    },
  },
});

Error Handling

The plugin gracefully handles errors and provides clear feedback:

removeFiles({
  filePaths: ["nonexistent.js"],
  logging: true,
});
// Output: [remove-files] File not found: /path/to/nonexistent.js

Contributing

  1. Fork the repository
  2. Create your feature branch: git checkout -b feat/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feat/amazing-feature
  5. Open a Pull Request

License

MIT ยฉ Your Name

Changelog

1.0.0

  • Initial release
  • Regex pattern support
  • Security-first design
  • Test mode
  • Comprehensive logging