Skip to content

Commit 6232829

Browse files
fix: compatibility with webpack 5
1 parent b389b4f commit 6232829

12 files changed

+382
-164
lines changed

src/Webpack4Cache.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import findCacheDir from 'find-cache-dir';
55
import serialize from 'serialize-javascript';
66

77
export default class Webpack4Cache {
8-
constructor(compilation, options) {
9-
this.cacheDir =
8+
constructor(compilation, options, weakCache) {
9+
this.cache =
1010
options.cache === true
1111
? Webpack4Cache.getCacheDirectory()
1212
: options.cache;
13+
this.weakCache = weakCache;
1314
}
1415

1516
static getCacheDirectory() {
@@ -18,31 +19,72 @@ export default class Webpack4Cache {
1819
);
1920
}
2021

21-
isEnabled() {
22-
return Boolean(this.cacheDir);
23-
}
22+
async get(task, sources) {
23+
const weakOutput = this.weakCache.get(task.assetSource);
24+
25+
if (weakOutput) {
26+
return weakOutput;
27+
}
28+
29+
if (!this.cache) {
30+
// eslint-disable-next-line no-undefined
31+
return undefined;
32+
}
2433

25-
async get(task) {
2634
// eslint-disable-next-line no-param-reassign
2735
task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys);
2836

2937
let cachedResult;
3038

3139
try {
32-
cachedResult = await cacache.get(this.cacheDir, task.cacheIdent);
40+
cachedResult = await cacache.get(this.cache, task.cacheIdent);
3341
} catch (ignoreError) {
3442
// eslint-disable-next-line no-undefined
3543
return undefined;
3644
}
3745

38-
return JSON.parse(cachedResult.data);
46+
cachedResult = JSON.parse(cachedResult.data);
47+
48+
const { css: code, map, input, assetName, inputSourceMap } = cachedResult;
49+
50+
if (map) {
51+
cachedResult.source = new sources.SourceMapSource(
52+
code,
53+
assetName,
54+
map,
55+
input,
56+
inputSourceMap,
57+
true
58+
);
59+
} else {
60+
cachedResult.source = new sources.RawSource(code);
61+
}
62+
63+
return cachedResult;
3964
}
4065

4166
async store(task) {
67+
if (!this.weakCache.has(task.assetSource)) {
68+
this.weakCache.set(task.assetSource, task);
69+
}
70+
71+
if (!this.cache) {
72+
// eslint-disable-next-line no-undefined
73+
return undefined;
74+
}
75+
76+
const { cacheIdent, css, assetName, map, input, inputSourceMap } = task;
77+
4278
return cacache.put(
43-
this.cacheDir,
44-
task.cacheIdent,
45-
JSON.stringify(task.output)
79+
this.cache,
80+
cacheIdent,
81+
JSON.stringify({
82+
assetName,
83+
css,
84+
map,
85+
input,
86+
inputSourceMap,
87+
})
4688
);
4789
}
4890
}

src/Webpack5Cache.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
export default class Cache {
22
// eslint-disable-next-line no-unused-vars
3-
constructor(compilation, ignored) {
3+
constructor(compilation) {
44
this.cache = compilation.getCache('CssMinimizerWebpackPlugin');
55
}
66

7-
// eslint-disable-next-line class-methods-use-this
8-
isEnabled() {
9-
return true;
10-
}
11-
127
async get(task) {
138
// eslint-disable-next-line no-param-reassign
149
task.eTag = task.eTag || this.cache.getLazyHashedEtag(task.assetSource);
@@ -17,6 +12,11 @@ export default class Cache {
1712
}
1813

1914
async store(task) {
20-
return this.cache.storePromise(task.assetName, task.eTag, task.output);
15+
const { source, warnings } = task;
16+
17+
return this.cache.storePromise(task.assetName, task.eTag, {
18+
source,
19+
warnings,
20+
});
2121
}
2222
}

src/index.js

Lines changed: 79 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ class CssMinimizerPlugin {
234234
}
235235

236236
const task = {
237+
assetName,
237238
assetSource,
238239
assetInfo,
239-
assetName,
240240
input,
241241
inputSourceMap,
242242
map: this.options.sourceMap,
@@ -263,91 +263,15 @@ class CssMinimizerPlugin {
263263
yield task;
264264
}
265265

266-
afterTask(compiler, compilation, task, weakCache) {
267-
const {
268-
error,
269-
inputSourceMap,
270-
assetName,
271-
input,
272-
assetInfo,
273-
assetSource,
274-
output,
275-
} = task;
276-
277-
let sourceMap = null;
278-
279-
if (
280-
(error || (output && output.warnings && output.warnings.length > 0)) &&
281-
inputSourceMap &&
282-
CssMinimizerPlugin.isSourceMap(inputSourceMap)
283-
) {
284-
sourceMap = new SourceMapConsumer(inputSourceMap);
285-
}
286-
287-
// Handling results
288-
// Error case: add errors, and go to next file
289-
if (error) {
290-
compilation.errors.push(
291-
CssMinimizerPlugin.buildError(
292-
error,
293-
assetName,
294-
sourceMap,
295-
new RequestShortener(compiler.context)
296-
)
297-
);
298-
299-
return;
300-
}
301-
302-
const { css: code, map, warnings } = output;
303-
304-
let weakOutput = weakCache.get(assetSource);
305-
306-
if (!weakOutput) {
307-
if (map) {
308-
weakOutput = new SourceMapSource(
309-
code,
310-
assetName,
311-
map,
312-
input,
313-
inputSourceMap,
314-
true
315-
);
316-
} else {
317-
weakOutput = new RawSource(code);
318-
}
319-
320-
weakCache.set(assetSource, weakOutput);
321-
}
322-
323-
CssMinimizerPlugin.updateAsset(compilation, assetName, weakOutput, {
324-
...assetInfo,
325-
minimized: true,
326-
});
327-
328-
// Handling warnings
329-
if (warnings && warnings.length > 0) {
330-
warnings.forEach((warning) => {
331-
const builtWarning = CssMinimizerPlugin.buildWarning(
332-
warning,
333-
assetName,
334-
sourceMap,
335-
new RequestShortener(compiler.context),
336-
this.options.warningsFilter
337-
);
338-
339-
if (builtWarning) {
340-
compilation.warnings.push(builtWarning);
341-
}
342-
});
343-
}
344-
}
345-
346266
// eslint-disable-next-line class-methods-use-this
347267
async runTasks(compiler, compilation, assetNames, CacheEngine, weakCache) {
348-
const cache = new CacheEngine(compilation, {
349-
cache: this.options.cache,
350-
});
268+
const cache = new CacheEngine(
269+
compilation,
270+
{
271+
cache: this.options.cache,
272+
},
273+
weakCache
274+
);
351275
const availableNumberOfCores = CssMinimizerPlugin.getAvailableNumberOfCores(
352276
this.options.parallel
353277
);
@@ -385,24 +309,6 @@ class CssMinimizerPlugin {
385309
const scheduledTasks = [];
386310

387311
for (const assetName of assetNames) {
388-
const enqueue = async (task) => {
389-
try {
390-
// eslint-disable-next-line no-param-reassign
391-
task.output = worker
392-
? await worker.transform(serialize(task))
393-
: await minifyFn(task);
394-
} catch (error) {
395-
// eslint-disable-next-line no-param-reassign
396-
task.error = error;
397-
}
398-
399-
if (cache.isEnabled() && typeof task.output !== 'undefined') {
400-
await cache.store(task);
401-
}
402-
403-
this.afterTask(compiler, compilation, task, weakCache);
404-
};
405-
406312
scheduledTasks.push(
407313
limit(async () => {
408314
const task = this.getTask(compiler, compilation, assetName).next()
@@ -412,23 +318,83 @@ class CssMinimizerPlugin {
412318
return Promise.resolve();
413319
}
414320

415-
if (cache.isEnabled()) {
321+
let resultOutput = await cache.get(task, {
322+
RawSource,
323+
SourceMapSource,
324+
});
325+
326+
if (!resultOutput) {
416327
try {
417-
task.output = await cache.get(task);
418-
} catch (ignoreError) {
419-
return enqueue(task);
328+
// eslint-disable-next-line no-param-reassign
329+
resultOutput = await (worker
330+
? worker.transform(serialize(task))
331+
: minifyFn(task));
332+
} catch (error) {
333+
compilation.errors.push(
334+
CssMinimizerPlugin.buildError(
335+
error,
336+
assetName,
337+
task.inputSourceMap &&
338+
CssMinimizerPlugin.isSourceMap(task.inputSourceMap)
339+
? new SourceMapConsumer(task.inputSourceMap)
340+
: null,
341+
new RequestShortener(compiler.context)
342+
)
343+
);
344+
345+
return Promise.resolve();
420346
}
421347

422-
if (!task.output) {
423-
return enqueue(task);
348+
task.css = resultOutput.css;
349+
task.map = resultOutput.map;
350+
task.warnings = resultOutput.warnings;
351+
352+
if (task.map) {
353+
task.source = new SourceMapSource(
354+
task.css,
355+
assetName,
356+
task.map,
357+
task.input,
358+
task.inputSourceMap,
359+
true
360+
);
361+
} else {
362+
task.source = new RawSource(task.css);
424363
}
425364

426-
this.afterTask(compiler, compilation, task, weakCache);
365+
await cache.store(task);
366+
} else {
367+
task.source = resultOutput.source;
368+
task.warnings = resultOutput.warnings;
369+
}
427370

428-
return Promise.resolve();
371+
if (task.warnings && task.warnings.length > 0) {
372+
task.warnings.forEach((warning) => {
373+
const builtWarning = CssMinimizerPlugin.buildWarning(
374+
warning,
375+
assetName,
376+
task.inputSourceMap &&
377+
CssMinimizerPlugin.isSourceMap(task.inputSourceMap)
378+
? new SourceMapConsumer(task.inputSourceMap)
379+
: null,
380+
new RequestShortener(compiler.context),
381+
this.options.warningsFilter
382+
);
383+
384+
if (builtWarning) {
385+
compilation.warnings.push(builtWarning);
386+
}
387+
});
429388
}
430389

431-
return enqueue(task);
390+
const { source, assetInfo } = task;
391+
392+
CssMinimizerPlugin.updateAsset(compilation, assetName, source, {
393+
...assetInfo,
394+
minimized: true,
395+
});
396+
397+
return Promise.resolve();
432398
})
433399
);
434400
}
@@ -507,7 +473,8 @@ class CssMinimizerPlugin {
507473
const CacheEngine = require('./Webpack4Cache').default;
508474

509475
compilation.hooks.optimizeChunkAssets.tapPromise(pluginName, () =>
510-
optimizeFn(compilation, CacheEngine)
476+
// eslint-disable-next-line no-undefined
477+
optimizeFn(compilation, CacheEngine, undefined, weakCache)
511478
);
512479
} else {
513480
if (this.options.sourceMap) {

src/minify.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const minify = async (options) => {
2929
return {
3030
css: result.css,
3131
map: result.map,
32-
error: result.error,
3332
warnings: warningsToString(result.warnings || []),
3433
};
3534
}
@@ -43,7 +42,6 @@ const minify = async (options) => {
4342
return {
4443
css: result.css,
4544
map: result.map,
46-
error: result.error,
4745
warnings: warningsToString(result.warnings()),
4846
};
4947
};

0 commit comments

Comments
 (0)