Delete files and folders using Rollup
npx @tessl/cli install tessl/npm-rollup-plugin-delete@3.0.0Rollup Plugin Delete provides a Rollup plugin for deleting files and folders during the build process. It offers a simple but flexible API for cleaning dist directories, removing generated files, and maintaining clean build environments.
npm install rollup-plugin-delete -Dimport del from "rollup-plugin-delete";
// Named export also available for types
import { type Options } from "rollup-plugin-delete";For CommonJS:
const del = require("rollup-plugin-delete");import del from "rollup-plugin-delete";
export default {
input: "src/index.js",
output: {
file: "dist/app.js"
},
plugins: [
del({ targets: "dist" })
]
};The plugin integrates with Rollup's hook system and leverages the robust del package internally for reliable file deletion operations. Key components:
buildStart)del packagerunOnce option for watch scenarios to prevent repeated deletionsThis package exports:
del function - the main plugin factoryOptions interface - TypeScript type for configurationCreates a Rollup plugin that deletes files and folders during the build process.
/**
* Creates a Rollup plugin for deleting files and folders
* @param options - Configuration options for the deletion plugin
* @returns Rollup Plugin object with name 'delete'
*/
function del(options?: Options): Plugin;Usage Examples:
// Delete entire directory
del({ targets: "dist" })
// Delete specific files
del({ targets: "dist/*.js" })
// Delete multiple patterns
del({ targets: ["dist/*", "images/*.webp"] })
// Use with verbose logging
del({
targets: "dist/*",
verbose: true
})
// Run on different hook
del({
targets: "dist",
hook: "buildEnd"
})
// Watch mode optimization
del({
targets: "dist",
runOnce: true
})Configuration interface extending all options from the del package.
interface Options extends DelOptions {
/**
* Rollup hook the plugin should use.
* @default 'buildStart'
*/
readonly hook?: AsyncPluginHooks;
/**
* Delete items once. Useful in watch mode.
* @default false
*/
readonly runOnce?: boolean;
/**
* Patterns of files and folders to be deleted.
* @default []
*/
readonly targets?: readonly string[] | string;
/**
* Outputs removed files and folders to console.
* @default false
*/
readonly verbose?: boolean;
}The plugin supports any async hook defined by Rollup's AsyncPluginHooks type:
// Imported from 'rollup' package
type AsyncPluginHooks =
| "buildStart"
| "buildEnd"
| "generateBundle"
| "writeBundle"
| "closeBundle"
| "renderStart"
| "renderError"
| "resolveDynamicImport"
| "resolveId"
| "load"
| "transform"
| "moduleParsed"
| "onLog";The plugin uses types imported from external packages:
// From 'rollup' package
interface Plugin {
name: string;
[hookName: string]: any;
}
// From 'del' package
interface DelOptions {
// Defined below
}The plugin inherits all options from the del package for advanced file deletion patterns:
interface DelOptions {
/**
* See what would be deleted without actually deleting
* @default false
*/
readonly dryRun?: boolean;
/**
* Number of concurrent deletions
* @default Infinity
*/
readonly concurrency?: number;
/**
* Called for each file/directory before deletion
*/
readonly onProgress?: (progress: {
totalCount: number;
deletedCount: number;
percent: number;
}) => void;
/**
* Patterns to ignore
* @default []
*/
readonly ignore?: readonly string[];
/**
* Allow deleting files/directories outside current working directory
* @default false
*/
readonly force?: boolean;
/**
* Allow deleting non-empty directories
* @default true
*/
readonly onlyFiles?: boolean;
}The plugin will throw errors in the following scenarios:
targetsWhen using dryRun: true, no actual deletion occurs, but the plugin will still validate patterns and log what would be deleted.
Common glob patterns for targeting files and folders:
// Single folder
{ targets: "dist" }
// Single file
{ targets: "dist/app.js" }
// All JS files in folder
{ targets: "dist/*.js" }
// All files in folder and subfolders
{ targets: "dist/**/*" }
// Multiple patterns
{ targets: ["dist/*", "temp/**/*", "*.log"] }
// Exclude specific files
{
targets: "dist/*",
ignore: ["dist/important.js"]
}