Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Corrections to try catch wrapping as suggested by @ooflorent
  • Loading branch information
owlyowl committed Aug 8, 2018
commit 6012a49623f727f093a2b3fcf4f4e07ce5ed8d0d
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Particularly useful when dealing with multiple entry points and wanting to get m

```javascript
const miniCssExtractPlugin = new MiniCssExtractPlugin({
filename: (chunk) => {
filename(chunk) {
if (chunk.indexOf('admin') > -1 || chunk.indexOf('client') > -1) {
return 'app.css';
}
Expand Down Expand Up @@ -312,7 +312,7 @@ For long term caching use `filename: "[contenthash].css"`. Optionally add `[name
[deps]: https://david-dm.org/webpack-contrib/mini-css-extract-plugin.svg
[deps-url]: https://david-dm.org/webpack-contrib/mini-css-extract-plugin

[tests]: https://img.shields.io/circleci/project/github/webpack-contrib/mini-css-extract-plugin.svg
[tests]: https://img.shields.io/circleci/project/github/webpack-contrib/mini-css-extract-plugin.svg
[tests-url]: https://circleci.com/gh/webpack-contrib/mini-css-extract-plugin

[cover]: https://codecov.io/gh/webpack-contrib/mini-css-extract-plugin/branch/master/graph/badge.svg
Expand Down
108 changes: 52 additions & 56 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,69 +262,65 @@ class MiniCssExtractPlugin {
const chunkMap = this.getCssChunkObject(chunk);
if (Object.keys(chunkMap).length > 0) {
const chunkMaps = chunk.getChunkMaps();
let linkHrefPath;
let chunkFilename;
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not wrap the entire block by a try/catch

- const linkHrefPath = mainTemplate.getAssetPath(
-   JSON.stringify(this.options.chunkFilename),
+ let chunkFilename;
+ try {
+  chunkFilename = JSON.stringify(this.getFilename(chunk, this.options.chunkFilename))
+ } catch (err) {
+   throw …
+ }
+ const linkHrefPath = mainTemplate.getAssetPath(
+  chunkFilename,

linkHrefPath = mainTemplate.getAssetPath(
JSON.stringify(
this.getFilename(chunk, this.options.chunkFilename)
),
{
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: (length) =>
`" + ${mainTemplate.renderCurrentHashCode(
hash,
length
)} + "`,
chunk: {
id: '" + chunkId + "',
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
hashWithLength(length) {
const shortChunkHashMap = Object.create(null);
for (const chunkId of Object.keys(chunkMaps.hash)) {
if (typeof chunkMaps.hash[chunkId] === 'string') {
shortChunkHashMap[chunkId] = chunkMaps.hash[
chunkId
].substring(0, length);
}
}
return `" + ${JSON.stringify(
shortChunkHashMap
)}[chunkId] + "`;
},
contentHash: {
[NS]: `" + ${JSON.stringify(
chunkMaps.contentHash[NS]
)}[chunkId] + "`,
},
contentHashWithLength: {
[NS]: (length) => {
const shortContentHashMap = {};
const contentHash = chunkMaps.contentHash[NS];
for (const chunkId of Object.keys(contentHash)) {
if (typeof contentHash[chunkId] === 'string') {
shortContentHashMap[chunkId] = contentHash[
chunkId
].substring(0, length);
}
}
return `" + ${JSON.stringify(
shortContentHashMap
)}[chunkId] + "`;
},
},
name: `" + (${JSON.stringify(
chunkMaps.name
)}[chunkId]||chunkId) + "`,
},
contentHashType: NS,
}
chunkFilename = JSON.stringify(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's incorrect. chunkFilename can't be a function. The chunkFilename generation logic need to be embedded into the runtime code.

chunk is the runtime chunk here and the referenced chunk is not known.

this.getFilename(chunk, this.options.chunkFilename)
);
} catch (err) {
throw new Error(
`Couldn't stringify JSON for filename provided as function: ${err}`
`Mini CSS Extract Plugin\n\nCouldn't stringify JSON for filename {Function}: ${err}\n`
);
}

const linkHrefPath = mainTemplate.getAssetPath(chunkFilename, {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: (length) => `"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- hashWithLength: (length) => {} 
+ hashWithLength (length)  {} 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michael-ciniawsky we need eslint rule in webpack-defaults for this 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, eslint-config-webpack ;)

+ ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add a new line here?

Copy link
Author

@owlyowl owlyowl Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry end of day and lack of coffee!

Copy link
Member

@ooflorent ooflorent Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside a string. You must not add one here.

Copy link
Author

@owlyowl owlyowl Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry edited comment above! But was following feedback from Michael as to how he wanted the header formatted

chunk: {
id: '" + chunkId + "',
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
hashWithLength(length) {
const shortChunkHashMap = Object.create(null);
for (const chunkId of Object.keys(chunkMaps.hash)) {
if (typeof chunkMaps.hash[chunkId] === 'string') {
shortChunkHashMap[chunkId] = chunkMaps.hash[
chunkId
].substring(0, length);
}
}
return `" + ${JSON.stringify(
shortChunkHashMap
)}[chunkId] + "`;
},
contentHash: {
[NS]: `" + ${JSON.stringify(
chunkMaps.contentHash[NS]
)}[chunkId] + "`,
},
contentHashWithLength: {
[NS]: (length) => {
const shortContentHashMap = {};
const contentHash = chunkMaps.contentHash[NS];
for (const chunkId of Object.keys(contentHash)) {
if (typeof contentHash[chunkId] === 'string') {
shortContentHashMap[chunkId] = contentHash[
chunkId
].substring(0, length);
}
}
return `" + ${JSON.stringify(
shortContentHashMap
)}[chunkId] + "`;
},
},
name: `" + (${JSON.stringify(
chunkMaps.name
)}[chunkId]||chunkId) + "`,
},
contentHashType: NS,
});

return Template.asString([
source,
'',
Expand Down