# wipe-webpack-cache

> Wipes webpack cache in a controled way.

Latest version **2.1.0** (published 2018-11-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install wipe-webpack-cache
pnpm add wipe-webpack-cache
yarn add wipe-webpack-cache
bun add wipe-webpack-cache
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2018-11-09 |
| First published | 2017-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 264 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Anton Korzunov |
| Maintainers | kashey |
| Keywords | webpack, require, cache, injection, test, mock, stub |

## Links

- npm: https://www.npmjs.com/package/wipe-webpack-cache
- Repository: https://github.com/theKashey/wipeWebpackCache
- Homepage: https://github.com/theKashey/wipeWebpackCache#readme
- Issues: https://github.com/theKashey/wipeWebpackCache/issues
- npm.io page: https://npm.io/package/wipe-webpack-cache

## Dependencies (1)

- [wipe-node-cache](https://npm.io/package/wipe-node-cache.md) ^2.1.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2018-11-09
- 2.0.1 — 2018-10-20
- 2.0.0 — 2018-10-20
- 1.0.3 — 2018-08-07
- 1.0.2 — 2018-03-25
- 1.0.1 — 2018-03-25
- 1.0.0 — 2017-08-28

## README

**Keep your cache clear** – as my mom always says.

wipeNodeCache – cleans, clears and wipes all old dirty modules from node.js internal require.cache. 

Useful for testing purposes when you need to freshly require a module. Or two.
Or just keep all modules fresh, for example for proxyquire.

## Install

```sh
$ npm install --save wipe-node-cache
$ npm install --save wipe-webpack-cache
```

## Usage
This library requires some extra API from webpack to work:
 - NamedModulesPlugin - to have names for files
 - HotModuleReplacementPlugin - to have parents information
---

```js
// foo.js
var i = 0;
module.exports = function () {
	return ++i;
};
```

```js
var wipe = require('wipe-node-cache');

require('./foo')();
//=> 1

require('./foo')();
//=> 2

wipe(null, function(){return true;})

require('./foo')();
//=> 1 . Module is fresh now
```

But this is simply, and stupid way. We can do it better!

## API

### wipe(object1, filterCallback, bubbleCallback)

Foreach module in system wipe will call _filterCallback_ with 2 arguments – object1 and moduleName(absolute))
And you shall return true, if you want to wipe this module.

After first pass wipe will enter bubbling stage, and will wipe all modules, which use first ones.
Each time _bubbleCallback_ will be called with 1 argument - moduleName.
And you can decide - purge it, or not. 

## Examples

(see examples in source)
```js
function resolver(stubs, fileName, module) {
  return !fileName.indexOf('node_modules') > 0
}

// wipe everything, except node_modules
wipe(null, resolver, function (moduleName) {
  return !resolver(null, moduleName);
});
```

```js
function resolver(stubs, fileName, module) {
  var dirname = module ? path.dirname(module) : '';
  var requireName = fileName;
  if (dirname) {
    requireName = fileName.charAt(0) == '.' ? path.normalize(dirname + '/' + fileName) : fileName;
  }

  for (var i in stubs) {
    if (requireName.indexOf(i) > 0) {
      return stubs[i];
    }
  }
}

// wipe anything from helpers, and app.js.
// but keep hands off node_modules and core during bubbling.
wipe({
  'helpers/*': true,
  'App.js': true
}, resolver, function (moduleName) {
  return !(moduleName.indexOf('node_modules') > 0) && !(moduleName.indexOf('core') > 0)
});
```

## Related

- [proxyquire](https://github.com/thlorenz/proxyquire) - Usefull testing tool, but with bad caching politics.

---
_Source: https://npm.io/package/wipe-webpack-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
