Skip to content

Commit 95f3418

Browse files
fix: improved performance (#345)
1 parent 3c50404 commit 95f3418

File tree

2 files changed

+95
-74
lines changed

2 files changed

+95
-74
lines changed

src/index.js

Lines changed: 95 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -101,81 +101,32 @@ class TerserPlugin {
101101
}
102102

103103
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;
158104
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);
166110

167111
// Skip double minimize assets from child compilation
168112
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;
170124
}
171125

172126
let input;
173127
let inputSourceMap;
174128

175-
const {
176-
source: sourceFromInputSource,
177-
map,
178-
} = inputSource.sourceAndMap();
129+
const { source: sourceFromInputSource, map } = source.sourceAndMap();
179130

180131
input = sourceFromInputSource;
181132

@@ -195,9 +146,80 @@ class TerserPlugin {
195146
input = input.toString();
196147
}
197148

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();
199152

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;
201223

202224
if (!output) {
203225
const options = {
@@ -220,8 +242,8 @@ class TerserPlugin {
220242
}
221243

222244
try {
223-
output = await (worker
224-
? worker.transform(serialize(options))
245+
output = await (getWorker
246+
? getWorker().transform(serialize(options))
225247
: minifyFn(options));
226248
} catch (error) {
227249
compilation.errors.push(
@@ -327,7 +349,7 @@ class TerserPlugin {
327349
);
328350
}
329351

330-
await cache.storePromise(name, eTag, {
352+
await cacheItem.storePromise({
331353
source: output.source,
332354
commentsFilename: output.commentsFilename,
333355
extractedCommentsSource: output.extractedCommentsSource,
@@ -356,8 +378,8 @@ class TerserPlugin {
356378

357379
await Promise.all(scheduledTasks);
358380

359-
if (worker) {
360-
await worker.end();
381+
if (initializedWorker) {
382+
await initializedWorker.end();
361383
}
362384

363385
await Array.from(allExtractedComments)

test/cache-option.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ describe('"cache" option', () => {
118118
},
119119
cache: {
120120
type: 'memory',
121-
// cacheDirectory: fileSystemCacheDirectory,
122121
},
123122
});
124123

0 commit comments

Comments
 (0)