| 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 | [](https://liberamanifesto.com)
|
|---|
| 16 |
|
|---|
| 17 | # webpack-manifest-plugin
|
|---|
| 18 |
|
|---|
| 19 | A 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 |
|
|---|
| 27 | This module requires an [Active LTS](https://github.com/nodejs/Release) Node version (v10.0.0+) and Webpack v4.44.0+.
|
|---|
| 28 |
|
|---|
| 29 | ## Contributing
|
|---|
| 30 |
|
|---|
| 31 | This repository leverages [pnpm](https://pnpm.js.org/) for dependency management.
|
|---|
| 32 |
|
|---|
| 33 | To begin, please install `pnpm`:
|
|---|
| 34 |
|
|---|
| 35 | ```console
|
|---|
| 36 | $ npm install pnpm -g
|
|---|
| 37 | ```
|
|---|
| 38 |
|
|---|
| 39 | ## Install
|
|---|
| 40 |
|
|---|
| 41 | Using npm:
|
|---|
| 42 |
|
|---|
| 43 | ```console
|
|---|
| 44 | npm 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 |
|
|---|
| 51 | Create a `webpack.config.js` file:
|
|---|
| 52 |
|
|---|
| 53 | ```js
|
|---|
| 54 | const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
|
|---|
| 55 | const options = { ... };
|
|---|
| 56 |
|
|---|
| 57 | module.exports = {
|
|---|
| 58 | // an example entry definition
|
|---|
| 59 | entry: [ 'app.js' ],
|
|---|
| 60 | ...
|
|---|
| 61 | plugins: [
|
|---|
| 62 | new WebpackManifestPlugin(options)
|
|---|
| 63 | ]
|
|---|
| 64 | };
|
|---|
| 65 | ```
|
|---|
| 66 |
|
|---|
| 67 | And run `webpack`:
|
|---|
| 68 |
|
|---|
| 69 | ```console
|
|---|
| 70 | $ npx wp
|
|---|
| 71 | ```
|
|---|
| 72 |
|
|---|
| 73 | With 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 |
|
|---|
| 86 | Type: `String`<br>
|
|---|
| 87 | Default: `''`
|
|---|
| 88 |
|
|---|
| 89 | Specifies a path prefix for all keys in the manifest. Useful for including your output path in the manifest.
|
|---|
| 90 |
|
|---|
| 91 | ### `fileName`
|
|---|
| 92 |
|
|---|
| 93 | Type: `String`<br>
|
|---|
| 94 | Default: `manifest.json`
|
|---|
| 95 |
|
|---|
| 96 | Specifies 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 |
|
|---|
| 100 | Type: `Function`<br>
|
|---|
| 101 | Default: `undefined`
|
|---|
| 102 |
|
|---|
| 103 | Allows 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 |
|
|---|
| 107 | Type: `Function`<br>
|
|---|
| 108 | Default: `undefined`
|
|---|
| 109 |
|
|---|
| 110 | A 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 |
|
|---|
| 114 | Type: `Function`<br>
|
|---|
| 115 | Default: `undefined`
|
|---|
| 116 |
|
|---|
| 117 | Allows 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 |
|
|---|
| 121 | Type: `String`<br>
|
|---|
| 122 | Default: `<webpack-config>.output.publicPath`
|
|---|
| 123 |
|
|---|
| 124 | A path prefix that will be added to values of the manifest.
|
|---|
| 125 |
|
|---|
| 126 | ### `removeKeyHash`
|
|---|
| 127 |
|
|---|
| 128 | Type: `RegExp | false`<br>
|
|---|
| 129 | Default: `/([a-f0-9]{32}\.?)/gi`
|
|---|
| 130 |
|
|---|
| 131 | If 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 |
|
|---|
| 145 | The 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 |
|
|---|
| 149 | Type: `Object`<br>
|
|---|
| 150 | Default: `{}`
|
|---|
| 151 |
|
|---|
| 152 | A cache of key/value pairs 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 |
|
|---|
| 156 | Type: `Function(Object) => string`<br>
|
|---|
| 157 | Default: `undefined`
|
|---|
| 158 |
|
|---|
| 159 | A `Function` which can be leveraged to serialize the manifest in a different format than json. e.g. `yaml`.
|
|---|
| 160 |
|
|---|
| 161 | ### `sort`
|
|---|
| 162 |
|
|---|
| 163 | Type: `Function`<br>
|
|---|
| 164 | Default: `undefined`
|
|---|
| 165 |
|
|---|
| 166 | Allows 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 |
|
|---|
| 170 | Type: `Boolean`<br>
|
|---|
| 171 | Default: `false`
|
|---|
| 172 |
|
|---|
| 173 | If `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 |
|
|---|
| 177 | Type: `Boolean`<br>
|
|---|
| 178 | Default: `false`
|
|---|
| 179 |
|
|---|
| 180 | If `true`, the manifest will be written on the deprecated webpack `emit` hook to be compatible with not yet updated webpack plugins.
|
|---|
| 181 |
|
|---|
| 182 | A 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 |
|
|---|
| 186 | Type: `Boolean`<br>
|
|---|
| 187 | Default: `false`
|
|---|
| 188 |
|
|---|
| 189 | If `true`, will emit the manifest to the build directory _and_ in memory for compatibility with `webpack-dev-server`.
|
|---|
| 190 |
|
|---|
| 191 | ## Manifest File Descriptor
|
|---|
| 192 |
|
|---|
| 193 | This 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 |
|
|---|
| 209 | Type: [`Chunk`](https://github.com/webpack/webpack/blob/master/lib/Chunk.js)
|
|---|
| 210 |
|
|---|
| 211 | Only available if `isChunk` is `true`
|
|---|
| 212 |
|
|---|
| 213 | ### `isInitial`
|
|---|
| 214 |
|
|---|
| 215 | Type: `Boolean`
|
|---|
| 216 |
|
|---|
| 217 | Is required to run you app. Cannot be `true` if `isChunk` is `false`.
|
|---|
| 218 |
|
|---|
| 219 | ### `isModuleAsset`
|
|---|
| 220 |
|
|---|
| 221 | Type: `Boolean`
|
|---|
| 222 |
|
|---|
| 223 | Is required by a module. Cannot be `true` if `isAsset` is `false`.
|
|---|
| 224 |
|
|---|
| 225 | ## Compiler Hooks
|
|---|
| 226 |
|
|---|
| 227 | 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.
|
|---|
| 228 |
|
|---|
| 229 | ### `getCompilerHooks`
|
|---|
| 230 |
|
|---|
| 231 | Returns: `{ afterEmit: SyncWaterfallHook, beforeEmit: SyncWaterfallHook }`
|
|---|
| 232 |
|
|---|
| 233 | ### `assetHookStage`
|
|---|
| 234 |
|
|---|
| 235 | Type: `Number`
|
|---|
| 236 | Default: `Infinity`
|
|---|
| 237 |
|
|---|
| 238 | If you need to consume the output of this plugin in another plugin, it can be useful to adjust the stage at which the manifest is generated. Pass a new stage to `assetHookStage` to change when the manifest is generated. See the [docs on `processAssets`](https://webpack.js.org/api/compilation-hooks/#list-of-asset-processing-stages) for more detail.
|
|---|
| 239 |
|
|---|
| 240 | Note: any files added to the compilation after the stage specified will not be included in the manifest.
|
|---|
| 241 |
|
|---|
| 242 | #### Usage
|
|---|
| 243 |
|
|---|
| 244 | ```js
|
|---|
| 245 | const { getCompilerHooks } = require('webpack-manifest-plugin');
|
|---|
| 246 |
|
|---|
| 247 | class BatmanPlugin {
|
|---|
| 248 | apply(compiler) {
|
|---|
| 249 | const { beforeEmit } = getCompilerHooks(compiler);
|
|---|
| 250 |
|
|---|
| 251 | beforeEmit.tap('BatmanPlugin', (manifest) => {
|
|---|
| 252 | return { ...manifest, name: 'hello' };
|
|---|
| 253 | });
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | ```
|
|---|
| 257 |
|
|---|
| 258 | ## Notes
|
|---|
| 259 |
|
|---|
| 260 | - If using this plugin with `webpack-clean` and `webpack-dev-server`, please review [this issue](https://github.com/shellscape/webpack-manifest-plugin/issues/267).
|
|---|
| 261 |
|
|---|
| 262 | ## Attiribution
|
|---|
| 263 |
|
|---|
| 264 | Special thanks to [Dane Thurber](https://github.com/danethurber), the original author of this plugin, without whom this plugin would not exist.
|
|---|
| 265 |
|
|---|
| 266 | ## Meta
|
|---|
| 267 |
|
|---|
| 268 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
|---|
| 269 |
|
|---|
| 270 | [LICENSE (MIT)](./LICENSE)
|
|---|