0
# Plugin Configuration
1
2
This document covers the complete configuration options and initialization of the WebpackAssetsManifest plugin.
3
4
## Plugin Class
5
6
```typescript { .api }
7
class WebpackAssetsManifest implements WebpackPluginInstance {
8
constructor(options?: Partial<Options>);
9
apply(compiler: Compiler): void;
10
11
// Public properties
12
options: Options;
13
assets: AssetsStorage;
14
assetNames: Map<string, string>;
15
compiler?: Compiler;
16
hooks: HookSystem;
17
18
// Getter properties
19
get defaultOptions(): Options;
20
get isMerging(): boolean;
21
get utils(): {
22
isKeyValuePair: typeof isKeyValuePair;
23
isObject: typeof isObject;
24
getSRIHash: typeof getSRIHash;
25
};
26
}
27
```
28
29
## Configuration Options
30
31
### Core Options
32
33
```typescript { .api }
34
interface Options {
35
// Plugin state
36
enabled: boolean; // Default: true
37
38
// Asset storage
39
assets: AssetsStorage; // Default: Object.create(null)
40
41
// Output settings
42
output: string; // Default: 'assets-manifest.json'
43
writeToDisk: boolean | 'auto'; // Default: 'auto'
44
45
// JSON formatting
46
replacer: JsonStringifyReplacer; // Default: null
47
space: JsonStringifySpace; // Default: 2
48
49
// Asset processing
50
fileExtRegex: RegExp | false; // Default: /\.\w{2,4}\.(?:map|gz|br)$|\.\w+$/i
51
contextRelativeKeys: boolean; // Default: false
52
53
// Manifest behavior
54
sortManifest: boolean | ((left: string, right: string) => number); // Default: true
55
merge: boolean | 'customize'; // Default: false
56
57
// Public path configuration
58
publicPath?: string | boolean | ((filename: string, manifest: WebpackAssetsManifest) => string);
59
60
// Entrypoints
61
entrypoints: boolean; // Default: false
62
entrypointsKey: string | false; // Default: 'entrypoints'
63
entrypointsUseAssets: boolean; // Default: false
64
65
// Subresource Integrity
66
integrity: boolean; // Default: false
67
integrityHashes: string[]; // Default: ['sha256', 'sha384', 'sha512']
68
integrityPropertyName: string; // Default: 'integrity'
69
70
// Extensions
71
extra: Record<PropertyKey, unknown>; // Default: Object.create(null)
72
}
73
```
74
75
### Hook Options
76
77
```typescript { .api }
78
interface Options {
79
// Hook callbacks
80
apply?: (manifest: WebpackAssetsManifest) => void;
81
customize?: (
82
entry: KeyValuePair | false | undefined | void,
83
original: KeyValuePair,
84
manifest: WebpackAssetsManifest,
85
asset?: Asset
86
) => KeyValuePair | false | undefined | void;
87
transform?: (assets: AssetsStorage, manifest: WebpackAssetsManifest) => AssetsStorage;
88
done?: (manifest: WebpackAssetsManifest, stats: Stats) => Promise<void>;
89
}
90
```
91
92
## Usage Examples
93
94
### Basic Configuration
95
96
```typescript
97
import { WebpackAssetsManifest } from "webpack-assets-manifest";
98
99
const manifest = new WebpackAssetsManifest({
100
output: "manifest.json",
101
enabled: true,
102
writeToDisk: true,
103
});
104
```
105
106
### Advanced Configuration
107
108
```typescript
109
import { WebpackAssetsManifest } from "webpack-assets-manifest";
110
111
const manifest = new WebpackAssetsManifest({
112
output: "assets/manifest.json",
113
publicPath: "https://cdn.example.com/",
114
sortManifest: true,
115
merge: "customize",
116
entrypoints: true,
117
integrity: true,
118
integrityHashes: ["sha256", "sha384"],
119
customize(entry, original, manifest, asset) {
120
// Custom entry processing
121
if (entry && entry.key.startsWith("img/")) {
122
return {
123
key: entry.key.replace("img/", "images/"),
124
value: entry.value,
125
};
126
}
127
return entry;
128
},
129
transform(assets, manifest) {
130
// Custom asset transformation
131
return assets;
132
},
133
done: async (manifest, stats) => {
134
console.log(`Manifest written to ${manifest.getOutputPath()}`);
135
},
136
});
137
```
138
139
### Multi-Compiler Configuration
140
141
```typescript
142
const sharedAssets = Object.create(null);
143
144
const manifest1 = new WebpackAssetsManifest({
145
assets: sharedAssets,
146
output: "client-manifest.json",
147
});
148
149
const manifest2 = new WebpackAssetsManifest({
150
assets: sharedAssets,
151
output: "server-manifest.json",
152
});
153
```
154
155
### Dynamic Public Path
156
157
```typescript
158
const manifest = new WebpackAssetsManifest({
159
publicPath(filename, manifest) {
160
// Environment-specific CDN URLs
161
const env = process.env.NODE_ENV;
162
const cdnUrl = env === "production" ? "https://cdn.prod.com" : "https://cdn.dev.com";
163
return `${cdnUrl}/${filename}`;
164
},
165
});
166
```
167
168
## Configuration Validation
169
170
The plugin uses schema-utils for configuration validation:
171
172
```typescript { .api }
173
const optionsSchema: Schema;
174
```
175
176
Invalid configurations will throw validation errors with detailed messages about the incorrect options.
177
178
## Default Options
179
180
```typescript { .api }
181
class WebpackAssetsManifest {
182
get defaultOptions(): Options;
183
}
184
```
185
186
All default values are accessible through the `defaultOptions` getter, allowing for inspection and extension of the base configuration.