0
# Plugin Methods
1
2
Static utility methods and instance methods available on the UglifyJsPlugin class.
3
4
## Capabilities
5
6
### Instance Methods
7
8
Methods available on UglifyJsPlugin instances.
9
10
```javascript { .api }
11
/**
12
* Webpack plugin interface method that hooks into webpack compilation process
13
* This method is called automatically by webpack and should not be called directly
14
* @param {object} compiler - Webpack compiler instance
15
* @returns {void}
16
*/
17
apply(compiler: object): void;
18
```
19
20
**Usage Note:** This method is automatically called by webpack when the plugin is added to the `optimization.minimizer` array. Do not call this method directly.
21
22
### Static Utility Methods
23
24
Helper methods for source map processing, error formatting, and validation.
25
26
#### Source Map Validation
27
28
```javascript { .api }
29
/**
30
* Validates if input object is a valid source map
31
* Checks for required properties: version, sources, mappings
32
* @param {any} input - Input object to validate
33
* @returns {boolean} True if input is a valid source map structure
34
*/
35
static isSourceMap(input: any): boolean;
36
```
37
38
**Usage Example:**
39
40
```javascript
41
const sourceMapData = {
42
version: 3,
43
sources: ['input.js'],
44
mappings: 'AAAA'
45
};
46
47
if (UglifyJsPlugin.isSourceMap(sourceMapData)) {
48
console.log('Valid source map');
49
}
50
```
51
52
Required source map properties:
53
- `version`: Source map version number
54
- `sources`: Array of source file paths
55
- `mappings`: Base64 VLQ mapping string
56
57
#### Source Map Processing
58
59
```javascript { .api }
60
/**
61
* Creates a SourceMapConsumer from input source map object
62
* Returns null if input is invalid or cannot be processed
63
* @param {object} inputSourceMap - Source map object to process
64
* @returns {SourceMapConsumer | null} SourceMapConsumer instance or null
65
*/
66
static buildSourceMap(inputSourceMap: object): SourceMapConsumer | null;
67
```
68
69
**Usage Example:**
70
71
```javascript
72
const sourceMap = UglifyJsPlugin.buildSourceMap(inputSourceMap);
73
if (sourceMap) {
74
// Use SourceMapConsumer for location mapping
75
const originalPosition = sourceMap.originalPositionFor({
76
line: 10,
77
column: 5
78
});
79
}
80
```
81
82
#### Error Formatting
83
84
```javascript { .api }
85
/**
86
* Formats UglifyJS errors with enhanced location information using source maps
87
* Creates detailed error messages with original source locations when available
88
* @param {Error} err - UglifyJS error object containing line/col or stack info
89
* @param {string} file - File path where the error occurred
90
* @param {SourceMapConsumer} sourceMap - Source map for mapping to original locations
91
* @param {RequestShortener} requestShortener - Webpack path shortener for clean paths
92
* @returns {Error} Enhanced error with location details and context
93
*/
94
static buildError(
95
err: Error,
96
file: string,
97
sourceMap: SourceMapConsumer,
98
requestShortener: RequestShortener
99
): Error;
100
```
101
102
**Error Object Properties:**
103
- `line`: Line number where error occurred
104
- `col`: Column number where error occurred
105
- `message`: Error description
106
- `stack`: Error stack trace (fallback if line/col unavailable)
107
108
**Enhanced Error Message Formats:**
109
- With source map: `${file} from UglifyJs\n${message} [${originalSource}:${originalLine},${originalColumn}][${file}:${line},${col}]`
110
- Without source map: `${file} from UglifyJs\n${message} [${file}:${line},${col}]`
111
- Stack trace fallback: `${file} from UglifyJs\n${stack}`
112
113
#### Warning Formatting
114
115
```javascript { .api }
116
/**
117
* Formats UglifyJS warnings with enhanced location information using source maps
118
* Applies warning filters and creates user-friendly warning messages
119
* @param {string} warning - UglifyJS warning message
120
* @param {string} file - File path where the warning occurred
121
* @param {SourceMapConsumer} sourceMap - Source map for mapping to original locations
122
* @param {RequestShortener} requestShortener - Webpack path shortener for clean paths
123
* @param {Function} warningsFilter - Function to determine if warning should be shown
124
* @returns {string | null} Formatted warning message or null if filtered out
125
*/
126
static buildWarning(
127
warning: string,
128
file: string,
129
sourceMap: SourceMapConsumer,
130
requestShortener: RequestShortener,
131
warningsFilter: Function
132
): string | null;
133
```
134
135
**Warning Processing:**
136
137
1. **Location Extraction**: Parses warning message for location info using regex `/\[.+:([0-9]+),([0-9]+)\]/`
138
2. **Source Map Mapping**: Maps locations to original source positions when available
139
3. **Filter Application**: Applies `warningsFilter` function to determine visibility
140
4. **Message Formatting**: Creates clean warning messages with location context
141
142
**Enhanced Warning Message Format:**
143
`UglifyJs Plugin: ${warningMessage}${locationMessage}`
144
145
Where `locationMessage` includes original source location when available:
146
`[${originalSource}:${originalLine},${originalColumn}]`
147
148
**Usage Example:**
149
150
```javascript
151
const warning = UglifyJsPlugin.buildWarning(
152
'Dropping unreachable code [input.js:15,10]',
153
'bundle.js',
154
sourceMap,
155
requestShortener,
156
(warning, source) => !warning.includes('unreachable')
157
);
158
159
// Returns null if filtered, or formatted warning string
160
```
161
162
### Integration with Webpack
163
164
The plugin integrates with webpack through several hooks:
165
166
#### Compilation Hooks
167
168
- **`compilation.hooks.buildModule`**: Enables source map usage for detailed error locations
169
- **`compilation.hooks.optimizeChunkAssets`**: Main minification processing hook
170
- **`mainTemplate.hooks.hashForChunk`**: Updates content hash for minified assets
171
- **`chunkTemplate.hooks.hashForChunk`**: Updates chunk hash for cache invalidation
172
173
#### Asset Processing Flow
174
175
1. **Chunk Filtering**: Applies `chunkFilter` to determine which chunks to process
176
2. **File Matching**: Uses `test`, `include`, `exclude` patterns to select files
177
3. **Source Map Processing**: Extracts and validates source maps when `sourceMap` option enabled
178
4. **Task Creation**: Builds processing tasks with caching keys and minification options
179
5. **Parallel Execution**: Distributes tasks across worker processes when `parallel` enabled
180
6. **Result Processing**: Applies results back to webpack assets with error/warning handling
181
7. **Comment Extraction**: Processes extracted comments into separate license files