Skip to content

Commit

Permalink
refactor: code (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 2, 2020
1 parent 1c37b2f commit 0e9ef0a
Showing 1 changed file with 78 additions and 62 deletions.
140 changes: 78 additions & 62 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,85 +105,101 @@ class CompressionPlugin {
async compress(compiler, compilation, assets) {
const cache = compilation.getCache("CompressionWebpackPlugin");
const assetsForMinify = await Promise.all(
Object.keys(assets)
.filter((name) => {
const { info } = compilation.getAsset(name);
Object.keys(assets).reduce((accumulator, name) => {
const { info, source } = compilation.getAsset(name);

// Skip double minimize assets from child compilation
if (info.compressed) {
return false;
}
// Skip double minimize assets from child compilation
if (info.compressed) {
return accumulator;
}

if (
!compiler.webpack.ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined,
this.options
)(name)
) {
return false;
}
if (
!compiler.webpack.ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined,
this.options
)(name)
) {
return accumulator;
}

return true;
})
.map(async (name) => {
const { info, source } = compilation.getAsset(name);
let input = source.source();

const eTag = cache.getLazyHashedEtag(source);
const cacheItem = cache.getItemCache(
serialize({
name,
algorithm: this.options.algorithm,
compressionOptions: this.options.compressionOptions,
}),
eTag
);
const output = await cacheItem.getPromise();
if (!Buffer.isBuffer(input)) {
input = Buffer.from(input);
}

return { name, info, inputSource: source, output, cacheItem };
})
);
if (input.length < this.options.threshold) {
return accumulator;
}

const { RawSource } = compiler.webpack.sources;
const scheduledTasks = [];
let relatedName;

for (const asset of assetsForMinify) {
scheduledTasks.push(
(async () => {
const { name, inputSource, cacheItem, info } = asset;
let { output } = asset;
if (typeof this.options.algorithm === "function") {
let filenameForRelatedName = this.options.filename;

let input = inputSource.source();
const index = filenameForRelatedName.lastIndexOf("?");

if (!Buffer.isBuffer(input)) {
input = Buffer.from(input);
if (index >= 0) {
filenameForRelatedName = filenameForRelatedName.substr(0, index);
}

if (input.length < this.options.threshold) {
return;
}
relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`;
} else if (this.options.algorithm === "gzip") {
relatedName = "gzipped";
} else {
relatedName = `${this.options.algorithm}ed`;
}

let relatedName;
if (info.related && info.related[relatedName]) {
return accumulator;
}

if (typeof this.options.algorithm === "function") {
let filenameForRelatedName = this.options.filename;
const eTag = cache.getLazyHashedEtag(source);
const cacheItem = cache.getItemCache(
serialize({
name,
algorithm: this.options.algorithm,
compressionOptions: this.options.compressionOptions,
}),
eTag
);

const index = filenameForRelatedName.lastIndexOf("?");
accumulator.push(
(async () => {
const output = await cacheItem.getPromise();

if (index >= 0) {
filenameForRelatedName = filenameForRelatedName.substr(0, index);
}
return {
name,
inputSource: source,
info,
input,
output,
cacheItem,
relatedName,
};
})()
);

relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`;
} else if (this.options.algorithm === "gzip") {
relatedName = "gzipped";
} else {
relatedName = `${this.options.algorithm}ed`;
}
return accumulator;
}, [])
);

if (info.related && info.related[relatedName]) {
return;
}
const { RawSource } = compiler.webpack.sources;
const scheduledTasks = [];

for (const asset of assetsForMinify) {
scheduledTasks.push(
(async () => {
const {
name,
inputSource,
input,
cacheItem,
info,
relatedName,
} = asset;
let { output } = asset;

if (!output) {
try {
Expand Down

0 comments on commit 0e9ef0a

Please sign in to comment.