Skip to content

Commit 5cef4d7

Browse files
refactor: code
2 parents c322cf8 + f912d0f commit 5cef4d7

File tree

6 files changed

+36
-50
lines changed

6 files changed

+36
-50
lines changed

src/Webpack4Cache.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ export default class Webpack4Cache {
2020
}
2121

2222
async get(cacheData, sources) {
23-
const weakOutput = this.weakCache.get(cacheData.assetSource);
24-
25-
if (weakOutput) {
26-
return weakOutput;
27-
}
28-
2923
if (!this.cache) {
3024
// eslint-disable-next-line no-undefined
3125
return undefined;
3226
}
3327

28+
const weakOutput = this.weakCache.get(cacheData.inputSource);
29+
30+
if (weakOutput) {
31+
return weakOutput;
32+
}
33+
3434
// eslint-disable-next-line no-param-reassign
3535
cacheData.cacheIdent =
3636
cacheData.cacheIdent || serialize(cacheData.cacheKeys);
@@ -55,8 +55,8 @@ export default class Webpack4Cache {
5555
return undefined;
5656
}
5757

58-
if (!this.weakCache.has(cacheData.assetSource)) {
59-
this.weakCache.set(cacheData.assetSource, cacheData);
58+
if (!this.weakCache.has(cacheData.inputSource)) {
59+
this.weakCache.set(cacheData.inputSource, cacheData);
6060
}
6161

6262
const { cacheIdent, source } = cacheData;

src/Webpack5Cache.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ export default class Cache {
77
async get(cacheData) {
88
// eslint-disable-next-line no-param-reassign
99
cacheData.eTag =
10-
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.assetSource);
10+
cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.inputSource);
1111

12-
return this.cache.getPromise(cacheData.assetName, cacheData.eTag);
12+
return this.cache.getPromise(cacheData.name, cacheData.eTag);
1313
}
1414

1515
async store(cacheData) {
1616
const { source } = cacheData;
1717

18-
return this.cache.storePromise(cacheData.assetName, cacheData.eTag, {
18+
return this.cache.storePromise(cacheData.name, cacheData.eTag, {
1919
source,
2020
});
2121
}

src/index.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class JsonMinimizerPlugin {
4444

4545
static buildError(error, file, context) {
4646
return new Error(
47-
`${file} in "${context}" from Json Minimizer\n${error.stack}`
47+
`"${file}" in "${context}" from Json Minimizer:\n${error}`
4848
);
4949
}
5050

@@ -71,20 +71,18 @@ class JsonMinimizerPlugin {
7171
}
7272

7373
async optimize(compiler, compilation, assets, CacheEngine, weakCache) {
74-
const matchObject = ModuleFilenameHelpers.matchObject.bind(
75-
// eslint-disable-next-line no-undefined
76-
undefined,
77-
this.options
78-
);
79-
8074
const assetNames = Object.keys(
8175
typeof assets === 'undefined' ? compilation.assets : assets
82-
).filter((assetName) => matchObject(assetName));
76+
).filter((assetName) =>
77+
// eslint-disable-next-line no-undefined
78+
ModuleFilenameHelpers.matchObject.bind(undefined, this.options)(assetName)
79+
);
8380

8481
if (assetNames.length === 0) {
8582
return Promise.resolve();
8683
}
8784

85+
const scheduledTasks = [];
8886
const cache = new CacheEngine(
8987
compilation,
9088
{
@@ -93,56 +91,54 @@ class JsonMinimizerPlugin {
9391
weakCache
9492
);
9593

96-
const scheduledTasks = [];
97-
98-
for (const assetName of assetNames) {
94+
for (const name of assetNames) {
9995
scheduledTasks.push(
10096
(async () => {
101-
const { source: assetSource, info } = JsonMinimizerPlugin.getAsset(
97+
const { source: inputSource, info } = JsonMinimizerPlugin.getAsset(
10298
compilation,
103-
assetName
99+
name
104100
);
105101

106102
// Skip double minimize assets from child compilation
107103
if (info.minimized) {
108104
return;
109105
}
110106

111-
let input = assetSource.source();
107+
let input = inputSource.source();
112108

113109
if (Buffer.isBuffer(input)) {
114110
input = input.toString();
115111
}
116112

117-
const cacheData = { assetName, assetSource };
113+
const cacheData = { inputSource };
118114

119115
if (JsonMinimizerPlugin.isWebpack4()) {
120116
if (this.options.cache) {
121117
cacheData.input = input;
122118
cacheData.cacheKeys = this.options.cacheKeys(
123119
{
124-
nodeVersion: process.version,
125120
// eslint-disable-next-line global-require
126121
'json-minimizer-webpack-plugin': require('../package.json')
127122
.version,
128123
'json-minimizer-webpack-plugin-options': this.options,
129-
assetName,
124+
name,
130125
contentHash: crypto
131126
.createHash('md4')
132127
.update(input)
133128
.digest('hex'),
134129
},
135-
assetName
130+
name
136131
);
137132
}
133+
} else {
134+
cacheData.name = name;
138135
}
139136

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

142139
if (!output) {
143140
try {
144141
const minimizerOptions = {
145-
assetName,
146142
input,
147143
minimizerOptions: this.options.minimizerOptions,
148144
minify: this.options.minify,
@@ -151,30 +147,22 @@ class JsonMinimizerPlugin {
151147
output = await minifyFn(minimizerOptions);
152148
} catch (error) {
153149
compilation.errors.push(
154-
JsonMinimizerPlugin.buildError(
155-
error,
156-
assetName,
157-
compiler.context
158-
)
150+
JsonMinimizerPlugin.buildError(error, name, compiler.context)
159151
);
160152

161153
return;
162154
}
163155

164-
output.source = new RawSource(output.json);
156+
output.source = new RawSource(output.output);
165157

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

169-
JsonMinimizerPlugin.updateAsset(
170-
compilation,
171-
assetName,
172-
output.source,
173-
{
174-
...info,
175-
minimized: true,
176-
}
177-
);
161+
// TODO `...` required only for webpack@4
162+
JsonMinimizerPlugin.updateAsset(compilation, name, output.source, {
163+
...info,
164+
minimized: true,
165+
});
178166
})()
179167
);
180168
}

src/minify.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ const minify = async (options) => {
1212

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

15-
return {
16-
json: result,
17-
};
15+
return { output: result };
1816
};
1917

2018
module.exports.minify = minify;

test/__snapshots__/JsonMinimizerPlugin.test.js.snap.webpack4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

test/__snapshots__/JsonMinimizerPlugin.test.js.snap.webpack5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

0 commit comments

Comments
 (0)