Removing of folders and files for Webpack
A plugin for webpack that removes files and folders before and after compilation.
Content
Installation
npm install remove-files-webpack-plugin
yarn add remove-files-webpack-plugin
Support
The plugin works on any OS and webpack >= 2.2.0.
Usage
const RemovePlugin = require('remove-files-webpack-plugin');
module.exports = {
plugins: [
new RemovePlugin({
before: {
},
watch: {
},
after: {
}
})
]
};
Notes for Windows users
Single backward slash
JavaScript uses it for escaping. If you want to use backward slash, then use double backward slash. Example: C:\\Windows\\System32\\cmd.exe
. By the way, single forward slashes are also supported.
Segment separator
All paths that you get or see from the plugin will contain platform-specific segment separator (i.e. slash): \\
on Windows and /
on POSIX. So, for example, even if you passed folders or files with /
as separator, TestObject.method
will give you a path with \\
as segment separator.
Per-drive working directory
From Node.js documentation:
On Windows Node.js follows the concept of per-drive working directory. This behavior can be observed when using a drive path without a backslash. For example, path.resolve('c:\\')
can potentially return a different result than path.resolve('c:')
. For more information, see this MSDN page.
Parameters
Name | Type | Default | Namespace | Description |
---|
root | string | . | All | A root directory. Not absolute paths will be appended to this. Defaults to where package.json and node_modules are located. |
include | string[] | [] | All | A folders and files for removing. |
exclude | string[] | [] | All | A folders and files for excluding. |
test | TestObject[] | [] | All | A folders for testing. |
TestObject.folder | string | Required | All | A path to the folder (relative to root ). |
TestObject.method | (absolutePath: string) => boolean | Required | All | A method that accepts an item path (root + folderPath + fileName) and returns value that indicates should this item be removed or not.
|
TestObject.recursive | boolean | false | All | Apply this method to all items in subdirectories. |
beforeRemove | (
absoluteFoldersPaths: string[],
absoluteFilesPaths: string[]
) => boolean | undefined | All | If specified, will be called before removing. Absolute paths of folders and files that will be removed will be passed into this function. If returned value is true , then remove process will be canceled. Will be not called if items for removing not found, emulate: true or skipFirstBuild: true . |
afterRemove | (
absoluteFoldersPaths: string[],
absoluteFilesPaths: string[]
) => void | undefined | All | If specified, will be called after removing. Absolute paths of folders and files that have been removed will be passed into this function. Will be not called if emulate: true or skipFirstBuild: true . |
trash | boolean | false | All | Move folders and files to trash (recycle bin) instead of permanent removing. Requires Windows 8+, macOS 10.12+ or Linux. |
log | boolean | true | All | Print messages of "info" level (example: "Which folders or files have been removed"). |
logWarning | boolean | true | All | Print messages of "warning" level (example: "An items for removing not found"). |
logError | boolean | false | All | Print messages of "error" level (example: "No such file or directory"). |
logDebug | boolean | false | All | Print messages of "debug" level (used for debugging). |
emulate | boolean | false | All | Emulate remove process. Print which folders and files will be removed without actually removing them. Ignores log parameter. |
allowRootAndOutside | boolean | false | All | Allow removing of root directory or outside root directory. It is kind of safe mode. Don't turn it on if you don't know what you actually do! |
skipFirstBuild | boolean | false | watch | First build will be skipped. |
beforeForFirstBuild | boolean | false | watch | For first build before parameters will be applied, for subsequent builds watch parameters will be applied. |
How to set
You can pass these parameters into any of the following keys: before
, watch
or after
. Each key is optional, but at least one should be specified.
before
– executes once before "normal" compilation.watch
– executes every time before "watch" compilation.after
– executes once after "normal" compilation and every time after "watch" compilation.
Namespace
"Namespace" means where particular parameter will be applied. For example, "All" means particular parameter will work in any key (before
, watch
, after
), watch
means particular parameter will work only in watch
key.
Compilation modes
- "normal" compilation means full compilation.
- "watch" compilation means first build is a full compilation and subsequent builds is a short rebuilds of changed files.
Examples
new RemovePlugin({
before: {
include: [
'./dist'
]
}
})
new RemovePlugin({
watch: {
include: [
'./dist/js/entry.js'
]
}
})
new RemovePlugin({
after: {
root: './dist',
include: [
'manifest.json',
'maps'
],
trash: true
}
})
new RemovePlugin({
before: {
include: [
'dist/manifest.json',
'./dist/maps'
],
log: false,
logWarning: true,
logError: true,
logDebug: false
}
})
new RemovePlugin({
after: {
test: [
{
folder: 'dist/styles',
method: (absoluteItemPath) => {
return new RegExp(/\.map$/, 'm').test(absoluteItemPath);
},
recursive: true
}
]
}
})
new RemovePlugin({
after: {
root: './dist',
test: [
{
folder: './styles',
method: (absoluteItemPath) => {
return new RegExp(/\.css\.map$/, 'm').test(absoluteItemPath);
}
},
{
folder: './scripts',
method: (absoluteItemPath) => {
return new RegExp(/\.js\.map$/, 'm').test(absoluteItemPath);
},
recursive: true
}
]
}
})
new RemovePlugin({
before: {
root: './dist'
test: [
{
folder: './maps',
method: () => true,
recursive: true
}
],
exclude: [
'./maps/main.map.js'
]
},
after: {
test: [
{
folder: 'dist/styles',
method: (absoluteItemPath) => {
return new RegExp(/\.css\.map$/, 'm').test(absoluteItemPath);
}
}
],
exclude: [
'dist/styles/popup.css.map'
]
}
})
new RemovePlugin({
before: {
include: [
'./dist'
]
},
watch: {
test: [
{
folder: './dist/js',
method: (absPath) => new RegExp(/(.*)-([^-\\\/]+)\.js/).test(absPath)
}
]
},
after: {
include: [
'./dist/log.txt'
],
trash: true
}
})
new RemovePlugin({
before: {
root: '.',
include: [
"C:\\Desktop\\test.txt"
],
trash: true,
emulate: true,
allowRootAndOutside: true
}
})
new RemovePlugin({
after: {
root: './dist',
test: [
{
folder: '.',
method: () => true,
recursive: true
}
],
beforeRemove: (absoluteFoldersPaths, absoluteFilesPaths) => {
if (absoluteFoldersPaths.length) {
return true;
}
for (const item of absoluteFilesPaths) {
if (item.includes('.txt')) {
return true;
}
}
},
afterRemove: (absoluteFoldersPaths, absoluteFilesPaths) => {
console.log('Successfully removed:');
console.log(`Folders – [${absoluteFoldersPaths}]`);
console.log(`Files – [${absoluteFilesPaths}]`);
},
log: false
}
})
Contribution
Feel free to use issues. Pull requests are also always welcome!
License
MIT.