@@ -101,81 +101,32 @@ class TerserPlugin {
101
101
}
102
102
103
103
async optimize ( compiler , compilation , assets ) {
104
- const assetNames = Object . keys ( assets ) . filter ( ( assetName ) =>
105
- compiler . webpack . ModuleFilenameHelpers . matchObject . bind (
106
- // eslint-disable-next-line no-undefined
107
- undefined ,
108
- this . options
109
- ) ( assetName )
110
- ) ;
111
-
112
- if ( assetNames . length === 0 ) {
113
- return ;
114
- }
115
-
116
- const availableNumberOfCores = TerserPlugin . getAvailableNumberOfCores (
117
- this . options . parallel
118
- ) ;
119
-
120
- let concurrency = Infinity ;
121
- let worker ;
122
-
123
- if ( availableNumberOfCores > 0 ) {
124
- // Do not create unnecessary workers when the number of files is less than the available cores, it saves memory
125
- const numWorkers = Math . min ( assetNames . length , availableNumberOfCores ) ;
126
-
127
- concurrency = numWorkers ;
128
-
129
- worker = new Worker ( require . resolve ( './minify' ) , {
130
- numWorkers,
131
- enableWorkerThreads : true ,
132
- } ) ;
133
-
134
- // https://github.com/facebook/jest/issues/8872#issuecomment-524822081
135
- const workerStdout = worker . getStdout ( ) ;
136
-
137
- if ( workerStdout ) {
138
- workerStdout . on ( 'data' , ( chunk ) => {
139
- return process . stdout . write ( chunk ) ;
140
- } ) ;
141
- }
142
-
143
- const workerStderr = worker . getStderr ( ) ;
144
-
145
- if ( workerStderr ) {
146
- workerStderr . on ( 'data' , ( chunk ) => {
147
- return process . stderr . write ( chunk ) ;
148
- } ) ;
149
- }
150
- }
151
-
152
- const limit = pLimit ( concurrency ) ;
153
- const {
154
- SourceMapSource,
155
- ConcatSource,
156
- RawSource,
157
- } = compiler . webpack . sources ;
158
104
const cache = compilation . getCache ( 'TerserWebpackPlugin' ) ;
159
- const allExtractedComments = new Map ( ) ;
160
- const scheduledTasks = [ ] ;
161
-
162
- for ( const name of assetNames ) {
163
- scheduledTasks . push (
164
- limit ( async ( ) => {
165
- const { info, source : inputSource } = compilation . getAsset ( name ) ;
105
+ let numberOfAssetsForMinify = 0 ;
106
+ const assetsForMinify = (
107
+ await Promise . all (
108
+ Object . keys ( assets ) . map ( async ( name ) => {
109
+ const { info, source } = compilation . getAsset ( name ) ;
166
110
167
111
// Skip double minimize assets from child compilation
168
112
if ( info . minimized ) {
169
- return ;
113
+ return false ;
114
+ }
115
+
116
+ if (
117
+ ! compiler . webpack . ModuleFilenameHelpers . matchObject . bind (
118
+ // eslint-disable-next-line no-undefined
119
+ undefined ,
120
+ this . options
121
+ ) ( name )
122
+ ) {
123
+ return false ;
170
124
}
171
125
172
126
let input ;
173
127
let inputSourceMap ;
174
128
175
- const {
176
- source : sourceFromInputSource ,
177
- map,
178
- } = inputSource . sourceAndMap ( ) ;
129
+ const { source : sourceFromInputSource , map } = source . sourceAndMap ( ) ;
179
130
180
131
input = sourceFromInputSource ;
181
132
@@ -195,9 +146,80 @@ class TerserPlugin {
195
146
input = input . toString ( ) ;
196
147
}
197
148
198
- const eTag = cache . getLazyHashedEtag ( inputSource ) ;
149
+ const eTag = cache . getLazyHashedEtag ( source ) ;
150
+ const cacheItem = cache . getItemCache ( name , eTag ) ;
151
+ const output = await cacheItem . getPromise ( ) ;
199
152
200
- let output = await cache . getPromise ( name , eTag ) ;
153
+ if ( ! output ) {
154
+ numberOfAssetsForMinify += 1 ;
155
+ }
156
+
157
+ return { name, info, input, inputSourceMap, output, cacheItem } ;
158
+ } )
159
+ )
160
+ ) . filter ( ( item ) => Boolean ( item ) ) ;
161
+
162
+ let getWorker ;
163
+ let initializedWorker ;
164
+ let numberOfWorkers ;
165
+ const availableNumberOfCores = TerserPlugin . getAvailableNumberOfCores (
166
+ this . options . parallel
167
+ ) ;
168
+
169
+ if ( availableNumberOfCores > 0 ) {
170
+ // Do not create unnecessary workers when the number of files is less than the available cores, it saves memory
171
+ numberOfWorkers = Math . min (
172
+ numberOfAssetsForMinify ,
173
+ availableNumberOfCores
174
+ ) ;
175
+ // eslint-disable-next-line consistent-return
176
+ getWorker = ( ) => {
177
+ if ( initializedWorker ) {
178
+ return initializedWorker ;
179
+ }
180
+
181
+ initializedWorker = new Worker ( require . resolve ( './minify' ) , {
182
+ numWorkers : numberOfWorkers ,
183
+ enableWorkerThreads : true ,
184
+ } ) ;
185
+
186
+ // https://github.com/facebook/jest/issues/8872#issuecomment-524822081
187
+ const workerStdout = initializedWorker . getStdout ( ) ;
188
+
189
+ if ( workerStdout ) {
190
+ workerStdout . on ( 'data' , ( chunk ) => {
191
+ return process . stdout . write ( chunk ) ;
192
+ } ) ;
193
+ }
194
+
195
+ const workerStderr = initializedWorker . getStderr ( ) ;
196
+
197
+ if ( workerStderr ) {
198
+ workerStderr . on ( 'data' , ( chunk ) => {
199
+ return process . stderr . write ( chunk ) ;
200
+ } ) ;
201
+ }
202
+
203
+ return initializedWorker ;
204
+ } ;
205
+ }
206
+
207
+ const limit = pLimit (
208
+ getWorker && numberOfAssetsForMinify > 0 ? numberOfWorkers : Infinity
209
+ ) ;
210
+ const {
211
+ SourceMapSource,
212
+ ConcatSource,
213
+ RawSource,
214
+ } = compiler . webpack . sources ;
215
+ const allExtractedComments = new Map ( ) ;
216
+ const scheduledTasks = [ ] ;
217
+
218
+ for ( const asset of assetsForMinify ) {
219
+ scheduledTasks . push (
220
+ limit ( async ( ) => {
221
+ const { name, input, inputSourceMap, info, cacheItem } = asset ;
222
+ let { output } = asset ;
201
223
202
224
if ( ! output ) {
203
225
const options = {
@@ -220,8 +242,8 @@ class TerserPlugin {
220
242
}
221
243
222
244
try {
223
- output = await ( worker
224
- ? worker . transform ( serialize ( options ) )
245
+ output = await ( getWorker
246
+ ? getWorker ( ) . transform ( serialize ( options ) )
225
247
: minifyFn ( options ) ) ;
226
248
} catch ( error ) {
227
249
compilation . errors . push (
@@ -327,7 +349,7 @@ class TerserPlugin {
327
349
) ;
328
350
}
329
351
330
- await cache . storePromise ( name , eTag , {
352
+ await cacheItem . storePromise ( {
331
353
source : output . source ,
332
354
commentsFilename : output . commentsFilename ,
333
355
extractedCommentsSource : output . extractedCommentsSource ,
@@ -356,8 +378,8 @@ class TerserPlugin {
356
378
357
379
await Promise . all ( scheduledTasks ) ;
358
380
359
- if ( worker ) {
360
- await worker . end ( ) ;
381
+ if ( initializedWorker ) {
382
+ await initializedWorker . end ( ) ;
361
383
}
362
384
363
385
await Array . from ( allExtractedComments )
0 commit comments