UNPKG

8.02 kBMarkdownView Raw
1[tests]: https://img.shields.io/circleci/project/github/shellscape/webpack-manifest-plugin.svg
2[tests-url]: https://circleci.com/gh/shellscape/webpack-manifest-plugin
3[cover]: https://codecov.io/gh/shellscape/webpack-manifest-plugin/branch/master/graph/badge.svg
4[cover-url]: https://codecov.io/gh/shellscape/webpack-manifest-plugin
5[size]: https://packagephobia.now.sh/badge?p=webpack-manifest-plugin
6[size-url]: https://packagephobia.now.sh/result?p=webpack-manifest-plugin
7
8<div align="center">
9 <img width="256" src="https://raw.githubusercontent.com/shellscape/webpack-manifest-plugin/master/assets/manifest.svg?sanitize=true" alt="webpack-manfiest-plugin"><br/><br/>
10</div>
11
12[![tests][tests]][tests-url]
13[![cover][cover]][cover-url]
14[![size][size]][size-url]
15[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
16
17# webpack-manifest-plugin
18
19A Webpack plugin for generating an asset manifest.
20
21:heart: Please consider [Sponsoring my work](https://github.com/sponsors/shellscape)
22
23## Requirements
24
25`webpack-manifest-plugin` is an [evergreen 🌲](./.github/FAQ.md#what-does-evergreen-mean) module.
26
27This module requires an [Active LTS](https://github.com/nodejs/Release) Node version (v10.0.0+) and Webpack v4.44.0+.
28
29## Contributing
30
31This repository leverages [pnpm](https://pnpm.js.org/) for dependency management.
32
33To begin, please install `pnpm`:
34
35```console
36$ npm install pnpm -g
37```
38
39## Install
40
41Using npm:
42
43```console
44npm install webpack-nano webpack-manifest-plugin --save-dev
45```
46
47_Note: We recommend using [webpack-nano](https://github.com/shellscape/webpack-nano), a very tiny, very clean webpack CLI._
48
49## Usage
50
51Create a `webpack.config.js` file:
52
53```js
54const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
55const options = { ... };
56
57module.exports = {
58 // an example entry definition
59 entry: [ 'app.js' ],
60 ...
61 plugins: [
62 new WebpackManifestPlugin(options)
63 ]
64};
65```
66
67And run `webpack`:
68
69```console
70$ npx wp
71```
72
73With the default options, the example above will create a `manifest.json` file in the output directory for the build. The manifest file will contain a map of source filenames to the corresponding build output file. e.g.
74
75```json
76{
77 "dist/batman.js": "dist/batman.1234567890.js",
78 "dist/joker.js": "dist/joker.0987654321.js"
79}
80```
81
82### Options
83
84### `basePath`
85
86Type: `String`<br>
87Default: `''`
88
89Specifies a path prefix for all keys in the manifest. Useful for including your output path in the manifest.
90
91### `fileName`
92
93Type: `String`<br>
94Default: `manifest.json`
95
96Specifies the file name to use for the resulting manifest. By default the plugin will emit `manifest.json` to your output directory. Passing an absolute path to the `fileName` option will override both the file name and path.
97
98### `filter`
99
100Type: `Function`<br>
101Default: `undefined`
102
103Allows filtering the files which make up the manifest. The passed function should match the signature of `(file: FileDescriptor) => Boolean`. Return `true` to keep the file, `false` to remove the file.
104
105### `generate`
106
107Type: `Function`<br>
108Default: `undefined`
109
110A custom `Function` to create the manifest. The passed function should match the signature of `(seed: Object, files: FileDescriptor[], entries: string[]) => Object` and can return anything as long as it's serialisable by `JSON.stringify`.
111
112### `map`
113
114Type: `Function`<br>
115Default: `undefined`
116
117Allows modifying the files which make up the manifest. The passed function should match the signature of `(file: FileDescriptor) => FileDescriptor` where an object matching `FileDescriptor` is returned.
118
119### `publicPath`
120
121Type: `String`<br>
122Default: `<webpack-config>.output.publicPath`
123
124A path prefix that will be added to values of the manifest.
125
126### `removeKeyHash`
127
128Type: `RegExp | false`<br>
129Default: `/([a-f0-9]{32}\.?)/gi`
130
131If set to a valid `RegExp`, removes hashes from manifest keys. e.g.
132
133```json
134{
135 "index.c5a9bff71fdfed9b6046.html": "index.c5a9bff71fdfed9b6046.html"
136}
137```
138
139```json
140{
141 "index.html": "index.c5a9bff71fdfed9b6046.html"
142}
143```
144
145The default value for this option is a regular expression targeting Webpack's [default md5 hash](https://webpack.js.org/configuration/output/#outputhashfunction). To target other hashing functions / algorithms, set this option to an appropriate `RegExp`. To disable replacing the hashes in key names, set this option to `false`.
146
147### `seed`
148
149Type: `Object`<br>
150Default: `{}`
151
152A cache of key/value pairs to used to seed the manifest. This may include a set of [custom key/value](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json) pairs to include in your manifest, or may be used to combine manifests across compilations in [multi-compiler mode](https://github.com/webpack/webpack/tree/master/examples/multi-compiler). To combine manifests, pass a shared seed object to each compiler's `WebpackManifestPlugin` instance.
153
154### `serialize`
155
156Type: `Function(Object) => string`<br>
157Default: `undefined`
158
159A `Function` which can be leveraged to serialize the manifest in a different format than json. e.g. `yaml`.
160
161### `sort`
162
163Type: `Function`<br>
164Default: `undefined`
165
166Allows sorting the files which make up the manifest. The passed function should match the signature of `(fileA: FileDescriptor, fileB: FileDescriptor) => Number`. Return `0` to indicate no change, `-1` to indicate the file should be moved to a lower index, and `1` to indicate the file shoud be moved to a higher index.
167
168### `useEntryKeys`
169
170Type: `Boolean`<br>
171Default: `false`
172
173If `true`, the keys specified in the `entry` property will be used as keys in the manifest. No file extension will be added (unless specified as part of an `entry` property key).
174
175### `useLegacyEmit`
176
177Type: `Boolean`<br>
178Default: `false`
179
180If `true`, the manifest will be written on the deprecated webpack `emit` hook to be compatible with not yet updated webpack plugins.
181
182A lot of webpack plugins are not yet updated to match the new webpack 5 API. This is a problem when other plugins use the deprecated `emit` hook. The manifest will be written before these other plugins and thus files are missing on the manifest.
183
184### `writeToFileEmit`
185
186Type: `Boolean`<br>
187Default: `false`
188
189If `true`, will emit the manifest to the build directory _and_ in memory for compatibility with `webpack-dev-server`.
190
191## Manifest File Descriptor
192
193This plugin utilizes the following object structure to work with files. Many options for this plugin utilize the structure below.
194
195```ts
196{
197 chunk?: Chunk;
198 isAsset: boolean;
199 isChunk: boolean;
200 isInitial: boolean;
201 isModuleAsset: boolean;
202 name: string | null;
203 path: string;
204}
205```
206
207### `chunk`
208
209Type: [`Chunk`](https://github.com/webpack/webpack/blob/master/lib/Chunk.js)
210
211Only available if `isChunk` is `true`
212
213### `isInitial`
214
215Type: `Boolean`
216
217Is required to run you app. Cannot be `true` if `isChunk` is `false`.
218
219### `isModuleAsset`
220
221Type: `Boolean`
222
223Is required by a module. Cannot be `true` if `isAsset` is `false`.
224
225## Compiler Hooks
226
227This 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.
228
229### `getCompilerHooks`
230
231Returns: `{ afterEmit: SyncWaterfallHook, beforeEmit: SyncWaterfallHook }`
232
233#### Usage
234
235```js
236const { getCompilerHooks } = require('webpack-manifest-plugin');
237
238class BatmanPlugin {
239 apply(compiler) {
240 const { beforeEmit } = getCompilerHooks(compiler);
241
242 beforeEmit.tap('BatmanPlugin', (manifest) => {
243 return { ...manifest, name: 'hello' };
244 });
245 }
246}
247```
248
249## Notes
250
251- If using this plugin with `webpack-clean` and `webpack-dev-server`, please review [this issue](https://github.com/shellscape/webpack-manifest-plugin/issues/267).
252
253## Attiribution
254
255Special thanks to [Dane Thurber](https://github.com/danethurber), the original author of this plugin, without whom this plugin would not exist.
256
257## Meta
258
259[CONTRIBUTING](./.github/CONTRIBUTING.md)
260
261[LICENSE (MIT)](./LICENSE)