A plugin for webpack that removes files and folders before and after compilation.
npm install remove-files-webpack-plugin
A plugin for webpack that removes files and folders before and after compilation.
- Content
- Installation
- Support
- Usage
- Notes
- Symbolic links
- Notes for Windows users
- Single backward slash
- Segment separator
- Per-drive working directory
- Parameters
- How to set
- Namespace
- Compilation modes
- Examples
- Version naming
- Contribution
- License
- With npm:
``javascript`
npm install remove-files-webpack-plugin
- With yarn:`javascript`
yarn add remove-files-webpack-plugin
The plugin works on any OS and webpack >= 2.2.0.
`javascript
const RemovePlugin = require('remove-files-webpack-plugin');
module.exports = {
plugins: [
new RemovePlugin({
before: {
// parameters for "before normal compilation" stage.
},
watch: {
// parameters for "before watch compilation" stage.
},
after: {
// parameters for "after normal and watch compilation" stage.
}
})
]
};
`
Symbolic links (soft links) will be treated as ordinary files.
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.
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.
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.
| Name | Type | Default | Namespace | Description |
| :----------------------: | :------------------------------------------------------------------------------------------: | :---------: | :-------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| root | string | . | All | A root directory.package.json
Not absolute paths will be appended to this.
Defaults to where and node_modules are located. |string[]
| include | | [] | All | A folders or files for removing. |string[]
| exclude | | [] | All | A folders or files for excluding. |TestObject[]
| test | | [] | All | A folders for testing. |string
| TestObject.folder | | Required | All | A path to the folder (relative to root). |(absolutePath: string) => boolean
| TestObject.method | | Required | All | A method that accepts an item path (root + folderPath + fileName) andboolean
returns value that indicates should this item be removed or not.
|
| TestObject.recursive | | 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.true
Absolute paths of folders and files that
will be removed will be passed into this function.
If returned value is ,emulate: true
then remove process will be canceled.
Will be not called if items for removing
not found, or skipFirstBuild: true. |(
| afterRemove | absoluteFoldersPaths: string[],absoluteFilesPaths: string[]) => void | undefined | All | If specified, will be called after removing.emulate: true
Absolute paths of folders and files
that have been removed will be passed into this function.
Will be not called if or skipFirstBuild: true. |boolean
| trash | | false | All | Move folders and files to the trash (recycle bin) instead of permanent removing.afterRemove
It is an async operation and you won't be able to control an execution chain along with other webpack plugins! callback behavior is undefined (it can be executed before, during or after actual execution).boolean
Requires Windows 8+, macOS 10.12+ or Linux. |
| log | | true | All | Print messages of "info" levelboolean
(example: "Which folders or files have been removed"). |
| logWarning | | true | All | Print messages of "warning" levelboolean
(example: "An items for removing not found"). |
| logError | | false | All | Print messages of "error" levelboolean
(example: "No such file or directory"). |
| logDebug | | false | All | Print messages of "debug" levelboolean
(used for debugging). |
| emulate | | false | All | Emulate remove process.log
Print which folders and files will be removed
without actually removing them.
Ignores parameter. |boolean
| allowRootAndOutside | | false | All | Allow removing of root directory or outside root directory.boolean
It is kind of safe mode.
Don't turn it on if you don't know
what you actually do! |
| readWebpackConfiguration | | false | All | Change parameters based on webpack configuration.stats
Following webpack parameters are supported: (controls logging).boolean
These webpack parameters have priority over the plugin parameters.
See webpack documentation for more - https://webpack.js.org/configuration |
| skipFirstBuild | | false | watch | First build will be skipped. |boolean
| beforeForFirstBuild | | false | watch | For first build before parameters will be applied,watch
for subsequent builds parameters will be applied. | | | | | |
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" 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.
- "normal" compilation means full compilation.
- "watch" compilation means first build is a full compilation and subsequent builds is a short rebuilds of changed files.
`javascript./dist
new RemovePlugin({
/**
* Before compilation permanently removes
* entire folder.`
*/
before: {
include: [
'./dist'
]
}
})
`javascript./dist/js/entry.js
new RemovePlugin({
/**
* Every time before "watch" compilation
* permanently removes file.`
*/
watch: {
include: [
'./dist/js/entry.js'
]
}
})
`javascript./dist/manifest.json
new RemovePlugin({
/**
* After compilation moves both
* file and./dist/maps
* folder to the trash.`
*/
after: {
root: './dist',
include: [
'manifest.json',
'maps'
],
trash: true
}
})
`javascript./dist/manifest.json
new RemovePlugin({
/**
* Before compilation permanently removes both
* file and ./dist/maps folder.`
* Log only works for warnings and errors.
*/
before: {
include: [
'dist/manifest.json',
'./dist/maps'
],
log: false,
logWarning: true,
logError: true,
logDebug: false
}
})
`javascript./dist/styles
new RemovePlugin({
/**
* After compilation permanently removes
* all maps files in folder and./dist/styles/header
* all subfolders (e.g. ).`
*/
after: {
test: [
{
folder: 'dist/styles',
method: (absoluteItemPath) => {
return new RegExp(/\.map$/, 'm').test(absoluteItemPath);
},
recursive: true
}
]
}
})
`javascript./dist/styles
new RemovePlugin({
/**
* After compilation:
* - permanently removes all css maps in folder../dist/scripts
* - permanently removes all js maps in folder and./dist/scripts/header
* all subfolders (e.g. ).`
*/
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
}
]
}
})
`javascript./dist/maps
new RemovePlugin({
/**
* Before compilation permanently removes all
* folders, subfolders and files from ./dist/maps/main.map.js
* except file.include: ['./maps']
*/
before: {
root: './dist'
/**
* You should do like this
* instead of .
*/
test: [
{
folder: './maps',
method: () => true,
recursive: true
}
],
exclude: [
'./maps/main.map.js'
]
},
/**
* After compilation permanently removes
* all css maps in ./dist/styles folderpopup.css.map
* except file.`
*/
after: {
test: [
{
folder: 'dist/styles',
method: (absoluteItemPath) => {
return new RegExp(/\.css\.map$/, 'm').test(absoluteItemPath);
}
}
],
exclude: [
'dist/styles/popup.css.map'
]
}
})
`javascript./dist
new RemovePlugin({
/**
* Before "normal" compilation permanently
* removes entire folder.
*/
before: {
include: [
'./dist'
]
},
/**
* Every time before compilation in "watch"
* mode (webpack --watch) permanently removes JS files
* with hash in the name (like "entry-vqlr39sdvl12.js").
*/
watch: {
test: [
{
folder: './dist/js',
method: (absPath) => new RegExp(/(.*)-([^-\\\/]+)\.js/).test(absPath)
}
]
},
/**
* Once after "normal" compilation or every time
* after "watch" compilation moves ./dist/log.txt`
* file to the trash.
*/
after: {
include: [
'./dist/log.txt'
],
trash: true
}
})
`javascript`
new RemovePlugin({
/**
* Before compilation emulates remove process
* for a file that is outside of the root directory.
* That file will be moved to the trash in case of
* not emulation.
*/
before: {
root: '.', // "D:\\remove-files-webpack-plugin-master"
include: [
"C:\\Desktop\\test.txt"
],
trash: true,
emulate: true,
allowRootAndOutside: true
}
})
`javascript
new RemovePlugin({
/**
* After compilation grabs all files from
* all subdirectories and decides should
* remove process be continued or not.
* If removed process is continued,
* then logs results with custom logger.
*/
after: {
root: './dist',
test: [
{
folder: '.',
method: () => true,
recursive: true
}
],
beforeRemove: (absoluteFoldersPaths, absoluteFilesPaths) => {
// cancel removing if there at least one folder.
if (absoluteFoldersPaths.length) {
return true;
}
// cancel removing if there at least one .txt file.Folders – [${absoluteFoldersPaths}]
for (const item of absoluteFilesPaths) {
if (item.includes('.txt')) {
return true;
}
}
},
afterRemove: (absoluteFoldersPaths, absoluteFilesPaths) => {
// replacing plugin logger with custom logger.
console.log('Successfully removed:');
console.log();Files – [${absoluteFilesPaths}]
console.log();`
},
log: false
}
})
This project uses following structure for version naming:
Feel free to use issues. Pull requests are also always welcome!
MIT.