Skip to content

Commit 945943f

Browse files
committed
Clean up as per feedback, Adding try catch err
1 parent 44cd88f commit 945943f

File tree

1 file changed

+59
-66
lines changed

1 file changed

+59
-66
lines changed

src/index.js

+59-66
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,7 @@ class MiniCssExtractPlugin {
170170
renderedModules,
171171
compilation.runtimeTemplate.requestShortener
172172
),
173-
filenameTemplate: this.getFilename(
174-
chunk,
175-
this.options.filename,
176-
this.options.processedFilename
177-
),
173+
filenameTemplate: this.getFilename(chunk, this.options.filename),
178174
pathOptions: {
179175
chunk,
180176
contentHashType: NS,
@@ -200,8 +196,7 @@ class MiniCssExtractPlugin {
200196
),
201197
filenameTemplate: this.getFilename(
202198
chunk,
203-
this.options.chunkFilename,
204-
this.options.processedChunkFilename
199+
this.options.chunkFilename
205200
),
206201
pathOptions: {
207202
chunk,
@@ -267,62 +262,69 @@ class MiniCssExtractPlugin {
267262
const chunkMap = this.getCssChunkObject(chunk);
268263
if (Object.keys(chunkMap).length > 0) {
269264
const chunkMaps = chunk.getChunkMaps();
270-
const linkHrefPath = mainTemplate.getAssetPath(
271-
JSON.stringify(
272-
this.getFilename(
273-
chunk,
274-
this.options.chunkFilename,
275-
this.options.processedChunkFilename
276-
)
277-
),
278-
{
279-
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
280-
hashWithLength: (length) =>
281-
`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
282-
chunk: {
283-
id: '" + chunkId + "',
284-
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
285-
hashWithLength(length) {
286-
const shortChunkHashMap = Object.create(null);
287-
for (const chunkId of Object.keys(chunkMaps.hash)) {
288-
if (typeof chunkMaps.hash[chunkId] === 'string') {
289-
shortChunkHashMap[chunkId] = chunkMaps.hash[
290-
chunkId
291-
].substring(0, length);
292-
}
293-
}
294-
return `" + ${JSON.stringify(
295-
shortChunkHashMap
296-
)}[chunkId] + "`;
297-
},
298-
contentHash: {
299-
[NS]: `" + ${JSON.stringify(
300-
chunkMaps.contentHash[NS]
301-
)}[chunkId] + "`,
302-
},
303-
contentHashWithLength: {
304-
[NS]: (length) => {
305-
const shortContentHashMap = {};
306-
const contentHash = chunkMaps.contentHash[NS];
307-
for (const chunkId of Object.keys(contentHash)) {
308-
if (typeof contentHash[chunkId] === 'string') {
309-
shortContentHashMap[chunkId] = contentHash[
265+
let linkHrefPath;
266+
try {
267+
linkHrefPath = mainTemplate.getAssetPath(
268+
JSON.stringify(
269+
this.getFilename(chunk, this.options.chunkFilename)
270+
),
271+
{
272+
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
273+
hashWithLength: (length) =>
274+
`" + ${mainTemplate.renderCurrentHashCode(
275+
hash,
276+
length)} + "`,
277+
278+
chunk: {
279+
id: '" + chunkId + "',
280+
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
281+
hashWithLength(length) {
282+
const shortChunkHashMap = Object.create(null);
283+
for (const chunkId of Object.keys(chunkMaps.hash)) {
284+
if (typeof chunkMaps.hash[chunkId] === 'string') {
285+
shortChunkHashMap[chunkId] = chunkMaps.hash[
310286
chunkId
311287
].substring(0, length);
312288
}
313289
}
314290
return `" + ${JSON.stringify(
315-
shortContentHashMap
291+
shortChunkHashMap
316292
)}[chunkId] + "`;
317293
},
294+
contentHash: {
295+
[NS]: `" + ${JSON.stringify(
296+
chunkMaps.contentHash[NS]
297+
)}[chunkId] + "`,
298+
},
299+
contentHashWithLength: {
300+
[NS]: (length) => {
301+
const shortContentHashMap = {};
302+
const contentHash = chunkMaps.contentHash[NS];
303+
for (const chunkId of Object.keys(contentHash)) {
304+
if (typeof contentHash[chunkId] === 'string') {
305+
shortContentHashMap[chunkId] = contentHash[
306+
chunkId
307+
].substring(0, length);
308+
}
309+
}
310+
return `" + ${JSON.stringify(
311+
shortContentHashMap
312+
)}[chunkId] + "`;
313+
},
314+
},
315+
name: `" + (${JSON.stringify(
316+
chunkMaps.name
317+
)}[chunkId]||chunkId) + "`,
318318
},
319-
name: `" + (${JSON.stringify(
320-
chunkMaps.name
321-
)}[chunkId]||chunkId) + "`,
322-
},
323-
contentHashType: NS,
324-
}
325-
);
319+
contentHashType: NS,
320+
}
321+
);
322+
} catch (err) {
323+
throw new Error(
324+
`Couldn't stringify JSON for filename provided as function: ${err}`
325+
);
326+
}
327+
326328
return Template.asString([
327329
source,
328330
'',
@@ -380,17 +382,8 @@ class MiniCssExtractPlugin {
380382
});
381383
}
382384

383-
getFilename(chunk, filename, processedFilename) {
384-
if (!processedFilename) {
385-
processedFilename = this.isFunction(filename)
386-
? filename(chunk)
387-
: filename;
388-
}
389-
return processedFilename;
390-
}
391-
392-
isFunction(functionToCheck) {
393-
return typeof functionToCheck === 'function';
385+
getFilename(chunk, filename) {
386+
return typeof filename === 'function' ? filename(chunk) : filename;
394387
}
395388

396389
getCssChunkObject(mainChunk) {

0 commit comments

Comments
 (0)