@@ -48,11 +48,18 @@ class UglifyJsPlugin {
48
48
...uglifyOptions ,
49
49
} ,
50
50
} ;
51
+ this . sourceMapsCache = new WeakMap ( ) ;
51
52
}
52
53
53
- static buildError ( err , file , sourceMap , requestShortener ) {
54
+ buildError ( err , file , inputSourceMap , requestShortener ) {
54
55
// Handling error which should have line, col, filename and message
55
56
if ( err . line ) {
57
+ const sourceMapCacheKey = { file } ;
58
+ let sourceMap = this . sourceMapsCache . get ( sourceMapCacheKey ) ;
59
+ if ( ! sourceMap ) {
60
+ sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
61
+ this . sourceMapsCache . set ( sourceMapCacheKey , sourceMap ) ;
62
+ }
56
63
const original = sourceMap && sourceMap . originalPositionFor ( {
57
64
line : err . line ,
58
65
column : err . col ,
@@ -67,11 +74,20 @@ class UglifyJsPlugin {
67
74
return new Error ( `${ file } from UglifyJs\n${ err . message } ` ) ;
68
75
}
69
76
70
- static buildWarning ( warning , file , sourceMap , warningsFilter , requestShortener ) {
71
- if ( ! file || ! sourceMap ) {
77
+ buildWarning ( warning , file , inputSourceMap , warningsFilter , requestShortener ) {
78
+ if ( ! file || ! inputSourceMap ) {
72
79
return warning ;
73
80
}
74
81
82
+ const sourceMapCacheKey = { file } ;
83
+
84
+ let sourceMap = this . sourceMapsCache . get ( sourceMapCacheKey ) ;
85
+
86
+ if ( ! sourceMap ) {
87
+ sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
88
+ this . sourceMapsCache . set ( sourceMapCacheKey , sourceMap ) ;
89
+ }
90
+
75
91
const match = warningRegex . exec ( warning ) ;
76
92
const line = + match [ 1 ] ;
77
93
const column = + match [ 2 ] ;
@@ -114,15 +130,14 @@ class UglifyJsPlugin {
114
130
. concat ( compilation . additionalChunkAssets || [ ] )
115
131
. filter ( ModuleFilenameHelpers . matchObject . bind ( null , this . options ) )
116
132
. forEach ( ( file ) => {
117
- let sourceMap ;
133
+ let inputSourceMap ;
118
134
const asset = compilation . assets [ file ] ;
119
135
if ( uglifiedAssets . has ( asset ) ) {
120
136
return ;
121
137
}
122
138
123
139
try {
124
140
let input ;
125
- let inputSourceMap ;
126
141
127
142
if ( this . options . sourceMap && asset . sourceAndMap ) {
128
143
const { source, map } = asset . sourceAndMap ( ) ;
@@ -131,11 +146,8 @@ class UglifyJsPlugin {
131
146
132
147
if ( utils . isSourceMap ( map ) ) {
133
148
inputSourceMap = map ;
134
- sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
135
149
} else {
136
150
inputSourceMap = map ;
137
- sourceMap = null ;
138
-
139
151
compilation . warnings . push (
140
152
new Error ( `${ file } contain invalid source map` ) ,
141
153
) ;
@@ -157,7 +169,6 @@ class UglifyJsPlugin {
157
169
const task = {
158
170
file,
159
171
input,
160
- sourceMap,
161
172
inputSourceMap,
162
173
commentsFile,
163
174
extractComments : this . options . extractComments ,
@@ -177,10 +188,10 @@ class UglifyJsPlugin {
177
188
tasks . push ( task ) ;
178
189
} catch ( error ) {
179
190
compilation . errors . push (
180
- UglifyJsPlugin . buildError (
191
+ this . buildError (
181
192
error ,
182
193
file ,
183
- sourceMap ,
194
+ inputSourceMap ,
184
195
requestShortener ,
185
196
) ,
186
197
) ;
@@ -194,17 +205,17 @@ class UglifyJsPlugin {
194
205
}
195
206
196
207
results . forEach ( ( data , index ) => {
197
- const { file, input, sourceMap , inputSourceMap, commentsFile } = tasks [ index ] ;
208
+ const { file, input, inputSourceMap, commentsFile } = tasks [ index ] ;
198
209
const { error, map, code, warnings, extractedComments } = data ;
199
210
200
211
// Handling results
201
212
// Error case: add errors, and go to next file
202
213
if ( error ) {
203
214
compilation . errors . push (
204
- UglifyJsPlugin . buildError (
215
+ this . buildError (
205
216
error ,
206
217
file ,
207
- sourceMap ,
218
+ inputSourceMap ,
208
219
requestShortener ,
209
220
) ,
210
221
) ;
@@ -264,10 +275,10 @@ class UglifyJsPlugin {
264
275
// Handling warnings
265
276
if ( warnings && warnings . length > 0 ) {
266
277
warnings . forEach ( ( warning ) => {
267
- const builtWarning = UglifyJsPlugin . buildWarning (
278
+ const builtWarning = this . buildWarning (
268
279
warning ,
269
280
file ,
270
- sourceMap ,
281
+ inputSourceMap ,
271
282
this . options . warningsFilter ,
272
283
requestShortener ,
273
284
) ;
0 commit comments