Skip to content

Commit

Permalink
feat: beforeEmit hook, add hook documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Dec 2, 2020
1 parent 69c30bd commit f62ca8b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
29 changes: 27 additions & 2 deletions README.md
Expand Up @@ -44,15 +44,15 @@ _Note: We recommend using [webpack-nano](https://github.com/shellscape/webpack-n
Create a `webpack.config.js` file: Create a `webpack.config.js` file:


```js ```js
const Manifest = require('webpack-manifest-plugin'); const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const options = { ... }; const options = { ... };


module.exports = { module.exports = {
// an example entry definition // an example entry definition
entry: [ 'app.js' ], entry: [ 'app.js' ],
... ...
plugins: [ plugins: [
new Manifest(options) new WebpackManifestPlugin(options)
] ]
}; };
``` ```
Expand Down Expand Up @@ -200,6 +200,31 @@ Type: `Boolean`


Is required by a module. Cannot be `true` if `isAsset` is `false`. Is required by a module. Cannot be `true` if `isAsset` is `false`.


## Compiler Hooks

This plugin supports the following hooks via the `getCompilerHooks` export; `afterEmit`, `beforeEmit`. These hooks can be useful, e.g. changing manifest contents before emitting to disk.

### `getCompilerHooks`

Returns: `{ afterEmit: SyncWaterfallHook, beforeEmit: AsyncSeriesWaterfallHook }`

#### Usage

```js
const { getCompilerHooks } = require('webpack-manifest-plugin');

class BatmanPlugin {
apply(compiler) {
const { beforeEmit } = getCompilerHooks();

beforeEmit.tapAsync('MyPlugin', (manifest, cb) => {
cb(null, { ...manifest, name: 'hello' })
})
}
}

```

## Attiribution ## Attiribution


Special thanks to [Dane Thurber](https://github.com/danethurber), the original author of this plugin, without whom this plugin would not exist. Special thanks to [Dane Thurber](https://github.com/danethurber), the original author of this plugin, without whom this plugin would not exist.
Expand Down
5 changes: 3 additions & 2 deletions lib/hooks.js
@@ -1,7 +1,7 @@
const { mkdirSync, writeFileSync } = require('fs'); const { mkdirSync, writeFileSync } = require('fs');
const { basename, dirname, join } = require('path'); const { basename, dirname, join } = require('path');


const { SyncWaterfallHook } = require('tapable'); const { AsyncSeriesWaterfallHook, SyncWaterfallHook } = require('tapable');


const { generateManifest, reduceAssets, reduceChunk, transformFiles } = require('./helpers'); const { generateManifest, reduceAssets, reduceChunk, transformFiles } = require('./helpers');


Expand All @@ -11,7 +11,8 @@ const getCompilerHooks = (compiler) => {
let hooks = compilerHookMap.get(compiler); let hooks = compilerHookMap.get(compiler);
if (typeof hooks === 'undefined') { if (typeof hooks === 'undefined') {
hooks = { hooks = {
afterEmit: new SyncWaterfallHook(['manifest']) afterEmit: new SyncWaterfallHook(['manifest']),
beforeEmit: new AsyncSeriesWaterfallHook(['manifest'])
}; };
compilerHookMap.set(compiler, hooks); compilerHookMap.set(compiler, hooks);
} }
Expand Down
5 changes: 5 additions & 0 deletions test/unit/index.js
Expand Up @@ -13,6 +13,11 @@ test.after(() => del(outputPath));
test('exports', async (t) => { test('exports', async (t) => {
t.truthy(getCompilerHooks); t.truthy(getCompilerHooks);
t.truthy(WebpackManifestPlugin); t.truthy(WebpackManifestPlugin);

const compiler = {};
const hooks = getCompilerHooks(compiler);
t.snapshot(Object.keys(hooks));
t.is(hooks, getCompilerHooks(compiler));
}); });


test('outputs a manifest of one file', async (t) => { test('outputs a manifest of one file', async (t) => {
Expand Down
14 changes: 14 additions & 0 deletions test/unit/snapshots/index.js.md
@@ -0,0 +1,14 @@
# Snapshot report for `test/unit/index.js`

The actual snapshot is saved in `index.js.snap`.

Generated by [AVA](https://avajs.dev).

## exports

> Snapshot 1
[
'afterEmit',
'beforeEmit',
]
Binary file added test/unit/snapshots/index.js.snap
Binary file not shown.

0 comments on commit f62ca8b

Please sign in to comment.