Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 5, 2020
2 parents c322cf8 + f912d0f commit 5cef4d7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 50 deletions.
16 changes: 8 additions & 8 deletions src/Webpack4Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ export default class Webpack4Cache {
}

async get(cacheData, sources) {
const weakOutput = this.weakCache.get(cacheData.assetSource);

if (weakOutput) {
return weakOutput;
}

if (!this.cache) {
// eslint-disable-next-line no-undefined
return undefined;
}

const weakOutput = this.weakCache.get(cacheData.inputSource);

if (weakOutput) {
return weakOutput;
}

// eslint-disable-next-line no-param-reassign
cacheData.cacheIdent =
cacheData.cacheIdent || serialize(cacheData.cacheKeys);
Expand All @@ -55,8 +55,8 @@ export default class Webpack4Cache {
return undefined;
}

if (!this.weakCache.has(cacheData.assetSource)) {
this.weakCache.set(cacheData.assetSource, cacheData);
if (!this.weakCache.has(cacheData.inputSource)) {
this.weakCache.set(cacheData.inputSource, cacheData);
}

const { cacheIdent, source } = cacheData;
Expand Down
6 changes: 3 additions & 3 deletions src/Webpack5Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export default class Cache {
async get(cacheData) {
// eslint-disable-next-line no-param-reassign
cacheData.eTag =
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.assetSource);
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.inputSource);

return this.cache.getPromise(cacheData.assetName, cacheData.eTag);
return this.cache.getPromise(cacheData.name, cacheData.eTag);
}

async store(cacheData) {
const { source } = cacheData;

return this.cache.storePromise(cacheData.assetName, cacheData.eTag, {
return this.cache.storePromise(cacheData.name, cacheData.eTag, {
source,
});
}
Expand Down
56 changes: 22 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class JsonMinimizerPlugin {

static buildError(error, file, context) {
return new Error(
`${file} in "${context}" from Json Minimizer\n${error.stack}`
`"${file}" in "${context}" from Json Minimizer:\n${error}`
);
}

Expand All @@ -71,20 +71,18 @@ class JsonMinimizerPlugin {
}

async optimize(compiler, compilation, assets, CacheEngine, weakCache) {
const matchObject = ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined,
this.options
);

const assetNames = Object.keys(
typeof assets === 'undefined' ? compilation.assets : assets
).filter((assetName) => matchObject(assetName));
).filter((assetName) =>
// eslint-disable-next-line no-undefined
ModuleFilenameHelpers.matchObject.bind(undefined, this.options)(assetName)
);

if (assetNames.length === 0) {
return Promise.resolve();
}

const scheduledTasks = [];
const cache = new CacheEngine(
compilation,
{
Expand All @@ -93,56 +91,54 @@ class JsonMinimizerPlugin {
weakCache
);

const scheduledTasks = [];

for (const assetName of assetNames) {
for (const name of assetNames) {
scheduledTasks.push(
(async () => {
const { source: assetSource, info } = JsonMinimizerPlugin.getAsset(
const { source: inputSource, info } = JsonMinimizerPlugin.getAsset(
compilation,
assetName
name
);

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

let input = assetSource.source();
let input = inputSource.source();

if (Buffer.isBuffer(input)) {
input = input.toString();
}

const cacheData = { assetName, assetSource };
const cacheData = { inputSource };

if (JsonMinimizerPlugin.isWebpack4()) {
if (this.options.cache) {
cacheData.input = input;
cacheData.cacheKeys = this.options.cacheKeys(
{
nodeVersion: process.version,
// eslint-disable-next-line global-require
'json-minimizer-webpack-plugin': require('../package.json')
.version,
'json-minimizer-webpack-plugin-options': this.options,
assetName,
name,
contentHash: crypto
.createHash('md4')
.update(input)
.digest('hex'),
},
assetName
name
);
}
} else {
cacheData.name = name;
}

let output = await cache.get(cacheData, { RawSource });

if (!output) {
try {
const minimizerOptions = {
assetName,
input,
minimizerOptions: this.options.minimizerOptions,
minify: this.options.minify,
Expand All @@ -151,30 +147,22 @@ class JsonMinimizerPlugin {
output = await minifyFn(minimizerOptions);
} catch (error) {
compilation.errors.push(
JsonMinimizerPlugin.buildError(
error,
assetName,
compiler.context
)
JsonMinimizerPlugin.buildError(error, name, compiler.context)
);

return;
}

output.source = new RawSource(output.json);
output.source = new RawSource(output.output);

await cache.store({ ...output, ...cacheData });
}

JsonMinimizerPlugin.updateAsset(
compilation,
assetName,
output.source,
{
...info,
minimized: true,
}
);
// TODO `...` required only for webpack@4
JsonMinimizerPlugin.updateAsset(compilation, name, output.source, {
...info,
minimized: true,
});
})()
);
}
Expand Down
4 changes: 1 addition & 3 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const minify = async (options) => {

const result = JSON.stringify(JSON.parse(input), replacer, space);

return {
json: result,
};
return { output: result };
};

module.exports.minify = minify;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`JsonMinimizerPlugin should emit error when broken json syntax: errors 1`] = `
Array [
"Error: broken-json-syntax.json in \\"/test/fixtures\\" from Json Minimizer
"Error: \\"broken-json-syntax.json\\" in \\"/test/fixtures\\" from Json Minimizer:
SyntaxError: Unexpected token s in JSON at position 4",
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`JsonMinimizerPlugin should emit error when broken json syntax: errors 1`] = `
Array [
"Error: broken-json-syntax.json in \\"/test/fixtures\\" from Json Minimizer
"Error: \\"broken-json-syntax.json\\" in \\"/test/fixtures\\" from Json Minimizer:
SyntaxError: Unexpected token s in JSON at position 4",
]
`;
Expand Down

0 comments on commit 5cef4d7

Please sign in to comment.