Giter Site home page Giter Site logo

amaimersion / remove-files-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW
50.0 2.0 3.0 537 KB

A plugin for webpack that removes files and folders before and after compilation.

Home Page: https://www.npmjs.com/package/remove-files-webpack-plugin

License: MIT License

JavaScript 100.00%
webpack plugins plugin webpack-plugin build-automation clean remove clean-plugin clean-webpack-plugin remove-plugin

remove-files-webpack-plugin's Introduction

Removing of folders and files for Webpack

A plugin for webpack that removes files and folders before and after compilation.

Content

Installation

  • With npm:
npm install remove-files-webpack-plugin
  • With yarn:
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: {
                // parameters for "before normal compilation" stage.
            },
            watch: {
                // parameters for "before watch compilation" stage.
            },
            after: {
                // parameters for "after normal and watch compilation" stage.
            }
        })
    ]
};

Notes

Symbolic links

Symbolic links (soft links) will be treated as ordinary files.

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 or files for removing.
exclude string[] [] All A folders or 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 the trash (recycle bin) instead of permanent removing.
It is an async operation and you won't be able to control an execution chain along with other webpack plugins!
afterRemove callback behavior is undefined (it can be executed before, during or after actual execution).
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!
readWebpackConfiguration boolean false All Change parameters based on webpack configuration.
Following webpack parameters are supported: stats (controls logging).
These webpack parameters have priority over the plugin parameters.
See webpack documentation for more - https://webpack.js.org/configuration
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 compilation permanently removes
     * entire `./dist` folder.
     */
    before: {
        include: [
            './dist'
        ]
    }
})
new RemovePlugin({
    /**
     * Every time before "watch" compilation
     * permanently removes `./dist/js/entry.js` file.
     */
    watch: {
        include: [
            './dist/js/entry.js'
        ]
    }
})
new RemovePlugin({
    /**
     * After compilation moves both
     * `./dist/manifest.json` file and
     * `./dist/maps` folder to the trash.
     */
    after: {
        root: './dist',
        include: [
            'manifest.json',
            'maps'
        ],
        trash: true
    }
})
new RemovePlugin({
    /**
     * Before compilation permanently removes both
     * `./dist/manifest.json` 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
    }
})
new RemovePlugin({
    /**
     * After compilation permanently removes
     * all maps files in `./dist/styles` folder and
     * all subfolders (e.g. `./dist/styles/header`).
     */
    after: {
        test: [
            {
                folder: 'dist/styles',
                method: (absoluteItemPath) => {
                    return new RegExp(/\.map$/, 'm').test(absoluteItemPath);
                },
                recursive: true
            }
        ]
    }
})
new RemovePlugin({
    /**
     * After compilation:
     * - permanently removes all css maps in `./dist/styles` folder.
     * - permanently removes all js maps in `./dist/scripts` folder and
     * all subfolders (e.g. `./dist/scripts/header`).
     */
    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 compilation permanently removes all
     * folders, subfolders and files from `./dist/maps`
     * except `./dist/maps/main.map.js` file.
     */
    before: {
        root: './dist'
        /**
         * You should do like this
         * instead of `include: ['./maps']`.
         */
        test: [
            {
                folder: './maps',
                method: () => true,
                recursive: true
            }
        ],
        exclude: [
            './maps/main.map.js'
        ]
    },

    /**
     * After compilation permanently removes
     * all css maps in `./dist/styles` folder
     * except `popup.css.map` file.
     */
    after: {
        test: [
            {
                folder: 'dist/styles',
                method: (absoluteItemPath) => {
                    return new RegExp(/\.css\.map$/, 'm').test(absoluteItemPath);
                }
            }
        ],
        exclude: [
            'dist/styles/popup.css.map'
        ]
    }
})
new RemovePlugin({
    /**
     * Before "normal" compilation permanently
     * removes entire `./dist` 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
    }
})
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
    }
})
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.
            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(`Folders – [${absoluteFoldersPaths}]`);
            console.log(`Files – [${absoluteFilesPaths}]`);
        },
        log: false
    }
})

Version naming

This project uses following structure for version naming: <MAJOR RELEASE>.<BREAKING CHANGES>.<NOT BREAKING CHANGES>.

Contribution

Feel free to use issues. Pull requests are also always welcome!

License

MIT.

remove-files-webpack-plugin's People

Contributors

amaimersion avatar aprilarcus avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

remove-files-webpack-plugin's Issues

symlinks always traversed

When an includedd directory contains a symlink, RemovePlugin will traverse and recursively remove the contents of that link. The preferred behavior would be to approximate rm -rf on the target directory, which does not follow symlinks.

Option `Trash` being async

Hi :)
First of all thanks for this great plugin

I'm using this plugin in the middle of the plugins execution, and other plugins depend on this plugin output, there was a bug that some file would delete after some time (micro seconds) which would affect the afterwards plugins.
Used the option of 'Trash: true' and didn't expect it to be the problem, found out after some time of hard digging in a comment that the 'Trash' option is async (in function 'handleRemove'), didn't found any where else that it mention such thing, once it was disable everything worked like magic.

I'm suggest if you could add to the description of the 'Trash' option that it is an async operation ?

'An items for permanent removing not found'

I'm now getting this error:

[email protected]:
Skipped, because unsafe removing – "C:\Users\q00u\dev\web-baseline\build"
An items for permanent removing not found

My settings are:

new RemovePlugin({
  after: {
    test: [{
      folder: 'build',
      method: filepath => {
        return new RegExp(/\.(js|map|html|png|LICENSE)$/, 'm').test(filepath);
       }
    }],
    trash: false
  }
})

If I leave trash as the default true, the error is: An items for removing in trash not found

1.1.3 works as expected, this is a new issue since 1.2.0

Error: EISDIR: illegal operation on a directory, copyfile

Bug Report

Deleting a directory to the trash results in Error: EISDIR: illegal operation on a directory, copyfile

Environment

  • plugin version: 1.4.4
  • system and its version: Ubuntu 20.04.2 LTS
  • webpack version: 3.10.0
  • node version: 14.13.0

Expected Behavior

Directory gets removed and restored without problems

Actual Behavior

Directory gets moved to the trash, but when restoring it results in an error EISDIR

Configuration

new RemovePlugin({
    before: {},
    watch: {},
    after: {
        include: ['./ng-client/'],
        trash: true,
    }
})

Debug Log

[email protected]:
The following items have been moved to the trash:
  folders:
    ng-client/

MergetJsonsWebpackPlugin emit starts...
MergetJsonsWebpackPlugin emit completed...
(node:39878) UnhandledPromiseRejectionWarning: Error: EISDIR: illegal operation on a directory, copyfile '/media/data/WorkSpace/WP-2/ng-client' -> '/home/izio/.local/share/Trash/files/2c41d73d-449f-4cc6-aeec-7358b92702ea'
(node:39878) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 6)

Additional Information

Keep generated files when in watch mode

Hi !
I have setup the remove plugin do clean old generated files before the build.
This works fine, but I would like it to be executed only once when I launch it in watch mode.
When the rebuild occurs on only one of the entry points, the others gets deleted.

I haven't found an option to keep it from executing to every single rebuild.

not useful!!!!

{
                before: {
                    include: ['lib/index.js']
                },
                after: {
                    include: ['index.js'],
                    exclude: ['index.css'],
                }
            }

not one file be remove!
is webpack need 4.0???? my webpack is 3.11

Skipped, because unsafe removing

For some reason, webpack creates two strange files from fontawesome libraries.
The generated files are:
fontawesome.min.js.LICENSE.txt.gz
solid.min.js.LICENSE.txt.gz

For the love of god i can't prevent it, i tried everything. So i used RemovePlugin to remove these two files from my output directory.
This works if the output is within the parent directory, but not working if it is inside another.

Works with:

  • parent
    • dist
      • fontawesome
        • js
          • file1
          • file2

Not working when

  • parent
  • otherDir
    • data
      • fontawesome
        • js
          • file1
          • file2

My remove plugin

let outputPath = path.join(__dirname, '../otherDir/data');

new RemovePlugin({
    log: true,
    allowRootAndOutside: true, // does nothing
    before:{
        include: [outputPath]
    },
    after: {
        include: [
            `${outputPath}/FontAwesome/js/fontawesome.min.js.LICENSE.txt.gz`,
            `${outputPath}/FontAwesome/js/solid.min.js.LICENSE.txt.gz`
        ],
    },
}),

Log

[email protected]:
Skipped, because unsafe removing – "C:\Users\Pc\OneDrive\Dokumentumok\GitHub\otherDir\data"
Could not find any items to permanently remove


[email protected]:
Skipped, because unsafe removing – "C:\Users\Pc\OneDrive\Dokumentumok\GitHub\otherDir\data\FontAwesome\js\fontawesome.min.js.LICENSE.txt.gz"
Skipped, because unsafe removing – "C:\Users\Pc\OneDrive\Dokumentumok\GitHub\otherDir\data\FontAwesome\js\solid.min.js.LICENSE.txt.gz"
Could not find any items to permanently remove

Improve documentation

The description "Defaults to from which directory is called." of the root parameter is not sufficiently clear. Does this refer to the output directory, to the directory in which the webpack config file is located, to the current working directory of the shell when calling webpack or what? Given some minor quirks in the rest of the description I am a bit sceptical - and particularly for a plugin which permanently deletes files it would be good to have this documented clearly and beyond doubts ... :-)

Fix wording of error message

Bug Report

The error message when no files are found does not make sense, or is not clear

Environment

  • plugin version: 1.4.1
  • system and its version:
  • webpack version:4.43.0
  • node version: 10.15.3

Expected Behavior

[email protected]:
No items found to remove 

Or similar!

Actual Behavior

[email protected]:
An items for permanent removing not found

Configuration

new RemovePlugin({
    before: { include: ['./dist'] },
    after: {}
})

Additional Information

The phrase An items implies that an item was found, which isn't the case, and I couldn't immediately work out whether it meant

Any items for permanent removing not found

which just means the folder is already clean, or

An item requested for permanent removing could not be found

which would be an actual error

Files not being removed, unsafe to remove warning in terminal

I have installed @1.2.1 to my dev dependencies, and have a simple setup whereby I want to remove some surplus files created when I run yarn build .

This gives the following warning in the terminal:

[email protected]:
Skipped, because unsafe removing – "/build/assets/js/legacy.js"
An items for removing in trash not found

My webpack setup looks like this:

new RemovePlugin({
            before: {
              include: [
                  'build' // remove the build folder before compiling
              ]
            },
            after: { 
              log: true,
              include: [
                'build/assets/js/legacy.js'
              ]
            }
        }),

Why does the warning appear and how can it be overwritten?

Treat first build with watch as normal build

Hi, thanks for a great plugin!
I know a simular request was made before and a (hacky) solution was presented =) so wanted to raise this again since I dislike hacky..

Feature Request

I want the first startup build with webpack watch to trigger "before"-code.

Summary

When starting a webpack watch session a "normal" build seems to be started followed by short "rebuilds" whenever a change is made. When using watch in this plugin it skips all "before"-code.

Moderate vulnerability trash 7.2.0

Bug Report

trash 7.2.0 Depends on vulnerable versions of make-dir

Environment

Getting error in npm audit

Expected Behavior

Should not produce a vulnerability error

Actual Behavior

Does produce a vulnerability error

Configuration

Debug Log

fix available via npm audit fix --force
Will install [email protected], which is a breaking change
node_modules/trash/node_modules/semver
make-dir 2.0.0 - 3.1.0
Depends on vulnerable versions of semver
node_modules/trash/node_modules/make-dir
trash 6.0.0 - 7.2.0
Depends on vulnerable versions of make-dir
node_modules/trash
remove-files-webpack-plugin >=1.2.0
Depends on vulnerable versions of trash

Additional Information

Breaking change in 1.5.0 - webpack types require webpack 5

Bug Report

Adding webpack types definition of version 5 got a problem with peer dependencies and won't work on systems using webpack 4.

Environment

  • plugin version: 1.4.5
  • system and its version: Big Sur 11.6.1
  • webpack version: 4.46.0
  • node version: 14

Expected Behavior

Install happens without error

Actual Behavior

Install breaks because of incompatible webpack versions

Debug Log

Command failed: npm install --save-dev [email protected] --color=always
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/remove-files-webpack-plugin/node_modules/webpack
npm ERR!   webpack@"^5" from @types/[email protected]
npm ERR!   node_modules/remove-files-webpack-plugin/node_modules/@types/webpack
npm ERR!     @types/webpack@"5.28.0" from [email protected]
npm ERR!     node_modules/remove-files-webpack-plugin
npm ERR!       dev remove-files-webpack-plugin@"1.5.0" from the root project
npm ERR!   peer webpack@"^5.1.0" from [email protected]
npm ERR!   node_modules/remove-files-webpack-plugin/node_modules/terser-webpack-plugin
npm ERR!     terser-webpack-plugin@"^5.1.1" from [email protected]
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer webpack@">=2.2.0" from [email protected]
npm ERR! node_modules/remove-files-webpack-plugin
npm ERR!   dev remove-files-webpack-plugin@"1.5.0" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/webpack
npm ERR!   peer webpack@">=2.2.0" from [email protected]
npm ERR!   node_modules/remove-files-webpack-plugin
npm ERR!     dev remove-files-webpack-plugin@"1.5.0" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Method isSave not working properly

Also, this came up trying the example from the Readme.

Setting the folder as ./dist and the test folders as './css' and './js'. Apparently path detects './css' as absolutePaths, as this routes are detected as not safe. I had to remove the './' from the test folders.

Originally posted by @cespinoza-loga in #5 (comment)

incorrect escaping of root folder path

Hi Sergey,
i like to remove a file after compilation, but unfortunately that does not work,
the file is skipped, because unsafe.
i have possibly a problem with the formatting of the root parameter.
the comparison in the isSave-Method results in false.

macOS: Mojave, 10.14.6
webpack: 4.41.5
remove-files-plugin: 1.2.1

i changed your simple example

image

to:

new RemovePlugin({
    after: {
        root: './dist',
        include: [
            'test.js',
            'test2.js'
        ],
        logDebug: true
    }
})

results in:

[email protected]:
Skipped, because unsafe removing – "dist/test.js"
Skipped, because not exists – "dist/test2.js"
An items for removing in trash not found

[email protected]:
Hook started – "afterEmit"
"test.js" converted to "dist/test.js"
"test2.js" converted to "dist/test2.js"
Testing skipped, because params.test is empty
Before formatting:
Root – "./dist"
Path – "dist/test.js"
After formatting:
Root – "\\/Users\\/dennisbaum\\/Projects\\/test_remove_files\\/dist"
Path – "/Users/dennisbaum/Projects/test_remove_files/dist"
Save – "false"
Skipped, because unsafe removing – "dist/test.js"
Skipped, because not exists – "dist/test2.js"
Directories before cropping – 
Files before cropping – 
Directories after cropping – 
Files after cropping – 
An items for removing in trash not found
Hook ended – "afterEmit"

The escaped root param looks different, and i don't know if the filename should be part of Path?
However, after formatting the comparison is not safe

After formatting:
Root – "\\/Users\\/dennisbaum\\/Projects\\/test_remove_files\\/dist"
Path – "/Users/dennisbaum/Projects/test_remove_files/dist"
Save – "false"

It works if i don't escape the root in plugin.js, but i don't know the consequences for other OSes...

//const rootFormat = format(root, true, true);
const rootFormat = format(root, false, true);
const pthFormat = format(pth, false, true); // we shouldn't escape.

As a workaround i use allowRootAndOutside: false, because then no formatting occurs, but i think that is not the best solution...

Any idea how that could be working?
Tnx.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.