-
-
Notifications
You must be signed in to change notification settings - Fork 384
feat(index): allow filename
to be a {Function}
(options.filename
)
#225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
85f6eda
52cf7f6
cf7b731
a06f5df
835624c
44cd88f
5b45d7e
6012a49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,7 +170,7 @@ class MiniCssExtractPlugin { | |
renderedModules, | ||
compilation.runtimeTemplate.requestShortener | ||
), | ||
filenameTemplate: this.options.filename, | ||
filenameTemplate: this.getFilename(chunk, this.options.filename), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you can pass a function to |
||
pathOptions: { | ||
chunk, | ||
contentHashType: NS, | ||
|
@@ -194,7 +194,10 @@ class MiniCssExtractPlugin { | |
renderedModules, | ||
compilation.runtimeTemplate.requestShortener | ||
), | ||
filenameTemplate: this.options.chunkFilename, | ||
filenameTemplate: this.getFilename( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
chunk, | ||
this.options.chunkFilename | ||
), | ||
pathOptions: { | ||
chunk, | ||
contentHashType: NS, | ||
|
@@ -259,56 +262,65 @@ class MiniCssExtractPlugin { | |
const chunkMap = this.getCssChunkObject(chunk); | ||
if (Object.keys(chunkMap).length > 0) { | ||
const chunkMaps = chunk.getChunkMaps(); | ||
const linkHrefPath = mainTemplate.getAssetPath( | ||
JSON.stringify(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[ | ||
let chunkFilename; | ||
try { | ||
chunkFilename = JSON.stringify( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's incorrect.
|
||
this.getFilename(chunk, this.options.chunkFilename) | ||
); | ||
} catch (err) { | ||
throw new Error( | ||
`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) => `" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - hashWithLength: (length) => {}
+ hashWithLength (length) {} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michael-ciniawsky we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, |
||
+ ${mainTemplate.renderCurrentHashCode(hash, length)} + "`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you add a new line here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry end of day and lack of coffee! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is inside a string. You must not add one here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
shortChunkHashMap | ||
shortContentHashMap | ||
)}[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, | ||
} | ||
); | ||
name: `" + (${JSON.stringify( | ||
chunkMaps.name | ||
)}[chunkId]||chunkId) + "`, | ||
}, | ||
contentHashType: NS, | ||
}); | ||
|
||
return Template.asString([ | ||
source, | ||
'', | ||
|
@@ -366,6 +378,10 @@ class MiniCssExtractPlugin { | |
}); | ||
} | ||
|
||
getFilename(chunk, filename) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed |
||
return typeof filename === 'function' ? filename(chunk) : filename; | ||
} | ||
|
||
getCssChunkObject(mainChunk) { | ||
const obj = {}; | ||
for (const chunk of mainChunk.getAllAsyncChunks()) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './style.css'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
body { background: red; } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
body { background: red; } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './style.css'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
body { background: red; } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const Self = require('../../../'); | ||
|
||
module.exports = { | ||
entry: { | ||
index: './index.js', | ||
app: './app.js', | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: [ | ||
Self.loader, | ||
'css-loader', | ||
], | ||
}, | ||
], | ||
}, | ||
plugins: [ | ||
new Self({ | ||
filename: (chunk) => chunk.name == 'app' ? 'this.is.app.css' : `[name].css`, | ||
chunkFilename: (chunk) => '[name].css', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chunkFilename can't be a function |
||
}), | ||
], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
\n
after the header