1.0.0 โข Published 6 months ago
@omnihash/rollup-plugin-remove-files v1.0.0
@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-devQuick 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
| Option | Type | Default | Description |
|---|---|---|---|
filePaths | string \| string[] | [] | Explicit file paths to remove |
patterns | RegExp \| RegExp[] \| string \| string[] | [] | Regex patterns to match files |
allowedDirs | string[] | ['dist', 'build', 'out', '.next/static'] | Directories where deletion is allowed |
baseDir | string | process.cwd() | Base directory for relative paths |
strictMode | boolean | true | Only allow deletion in allowedDirs |
testRun | boolean | false | Preview mode - show what would be deleted |
logging | boolean | false | Enable 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.jsCustom 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.jsContributing
- Fork the repository
- Create your feature branch:
git checkout -b feat/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feat/amazing-feature - Open a Pull Request
License
MIT ยฉ Your Name
Changelog
1.0.0
- Initial release
- Regex pattern support
- Security-first design
- Test mode
- Comprehensive logging
1.0.0
6 months ago