Removing of folders and files for Webpack
A plugin for webpack which removes files and folders before and after compilation.
Installation
npm install remove-files-webpack-plugin
yarn add remove-files-webpack-plugin
Usage
const RemovePlugin = require('remove-files-webpack-plugin');
module.exports = {
plugins: [
new RemovePlugin({
before: {
},
after: {
}
})
]
}
Be aware! You cannot undo deletion of folders or files. Use the emulate
option if you not sure about correctness of the parameters.
Parameters
Name | Type | Default | Description |
---|
root | String | . | A root directory. Not absolute paths will be appended to this. Defaults to from which directory is called. |
include | Array<String> | [] | A folders or files for removing. |
exclude | Array<String> | [] | A files for excluding. |
test | Array<TestObject> | [] | A folders for custom testing. |
TestObject.folder | String | Required | A path to the folder. |
TestObject.method | (filePath: String) => Boolean | Required | A method that accepts an absolute file path and must return boolean value that indicates should be removed that file or not. |
TestObject.recursive | Boolean | false | Test in all subfolders, not just in TestObject.folder . |
log | Boolean | true | Print which folders or files has been removed. |
emulate | Boolean | false | Emulate remove process. Print which folders or files will be removed without actually removing them. Ignores log value. |
allowRootAndOutside | Boolean | false | Allow remove of a root directory or outside the root directory. It's kinda safe mode. Don't turn it on, if you don't know what you actually do! |
Example how to set these options:
You can pass the options into both before
and after
keys. Each key is optional, but at least one should be specified.
before
- executes before compilation;after
- executes after compilation.
const RemovePlugin = require('remove-files-webpack-plugin');
module.exports = {
plugins: [
new RemovePlugin({
before: {
include: ['dist']
},
after: {
test: [
{
folder: 'dist/styles',
method: (filePath) => {
return new RegExp(/\.map$/, 'm').test(filePath);
}
}
]
}
})
]
}
Examples
new RemovePlugin({
before: {
include: ['dist']
},
after: {
exclude: ['dist/styles/popup.css.map'],
test: [
{
folder: 'dist/styles',
method: (filePath) => {
return new RegExp(/\.map$/, 'm').test(filePath);
}
}
]
}
})
new RemovePlugin({
after: {
test: [
{
folder: 'dist/styles',
method: (filePath) => {
return new RegExp(/\.map$/, 'm').test(filePath);
},
recursive: true
}
]
}
})
new RemovePlugin({
before: {
root: './dist',
include: ['manifest.json', 'js']
}
})
new RemovePlugin({
after: {
root: './dist',
test: [
{
folder: './styles',
method: (filePath) => {
return new RegExp(/\.map$/, 'm').test(filePath);
}
},
{
folder: './scripts',
method: (filePath) => {
return new RegExp(/\.js.map$/, 'm').test(filePath);
},
recursive: true
}
]
}
})
Issues and requests
Feel free to use issues. Pull requests are also always welcome!