-
-
Notifications
You must be signed in to change notification settings - Fork 384
feat: allow filename to be a {Function}
#145
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
feat: allow filename to be a {Function}
#145
Conversation
src/index.js
Outdated
@@ -111,6 +111,11 @@ class MiniCssExtractPlugin { | |||
options | |||
); | |||
if (!this.options.chunkFilename) { | |||
if (this.isFunction(this.options.filename)) { | |||
throw new Error( | |||
`You are required to provide a value for chunkFilename when using a Function for this.options.filename, please specify the [name], [id] or [chunkhash] in this 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.
Remove this throw
, it should be done after we integrate schema
for options
src/index.js
Outdated
return this.isFunction(filename) ? filename(chunk) : filename; | ||
} | ||
|
||
isFunction(functionToCheck) { |
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.
Remove this and use typeof something === '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.
This looks OK, but I'm not sure it's a good idea to call getFilename multiple times. It's possible the callback has side-effects (say, incrementing a counter) and returns different results every time.
There's also the leftover console.logs ;)
looking forward to this |
src/index.js
Outdated
@@ -366,6 +371,16 @@ class MiniCssExtractPlugin { | |||
}); | |||
} | |||
|
|||
getFilename(chunk, filename) { | |||
console.log('--chunk--'); | |||
console.log(chunk); |
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.
Remove console.log
Store filename in options, this way function is called once
Sorry it took so long, I remove the console logs and made sure the filename function is called only once. |
Is there enough information in |
filenameTemplate: this.getFilename( | ||
chunk, | ||
this.options.filename, | ||
this.options.processedFilename |
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.
What is this option? There is no documentation about it and it is not used by the test cases.
@@ -366,6 +380,19 @@ class MiniCssExtractPlugin { | |||
}); | |||
} | |||
|
|||
getFilename(chunk, filename, processedFilename) { | |||
if (!processedFilename) { | |||
processedFilename = this.isFunction(filename) |
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.
Use return
here. Reassigning parameters result in deopt.
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.
Could you use typeof
instead please?
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.
I second the parameter reassign, and did not include it in my review because it is mentioned here. this should not be done, and the linter should be throwing a warning there about it. Run npm lint
before the next commit to assure all warnings have been mitigated.
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.
My comments and review are more geared towards code quality, which I feel can be improved to reduce long-term technical debt.
@@ -260,7 +268,13 @@ class MiniCssExtractPlugin { | |||
if (Object.keys(chunkMap).length > 0) { | |||
const chunkMaps = chunk.getChunkMaps(); | |||
const linkHrefPath = mainTemplate.getAssetPath( | |||
JSON.stringify(this.options.chunkFilename), | |||
JSON.stringify( |
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.
I'd like to see a "safe" stringify done here, or at the very least this done in a try/catch that can safely trap and display a meaningful error message.
@@ -260,7 +268,13 @@ class MiniCssExtractPlugin { | |||
if (Object.keys(chunkMap).length > 0) { | |||
const chunkMaps = chunk.getChunkMaps(); | |||
const linkHrefPath = mainTemplate.getAssetPath( |
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.
we've got three nested calls here. that's a nightmare for debugging. break these values out into separate consts
and pass the variables to each function.
JSON.stringify( | ||
this.getFilename( | ||
chunk, | ||
this.options.chunkFilename, |
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.
use destructuring instead of repeating this.options
. the same comment applies to several places in this file.
@@ -366,6 +380,19 @@ class MiniCssExtractPlugin { | |||
}); | |||
} | |||
|
|||
getFilename(chunk, filename, processedFilename) { |
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.
Is filename
actually a full or partial path? If so, follow the node convention and call this path
.
If processedFilename
the path after a transformation? If so, then this should be the path/filename
and the non-transformed path/filename should be the resultPath/resultFilename
.
I'd like to see this use a single opts
parameter, and the signature change to { chunk, fileName, resultFileName }
, or if filename is actually a path, { chunk, path, resultPath }
@@ -366,6 +380,19 @@ class MiniCssExtractPlugin { | |||
}); | |||
} | |||
|
|||
getFilename(chunk, filename, processedFilename) { | |||
if (!processedFilename) { |
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.
we don't need to do an if with a ternary here:
processedFilename = processedFilename || this.isFunction(filename)
? filename(chunk)
: filename;
return processedFilename; | ||
} | ||
|
||
isFunction(functionToCheck) { |
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.
we don't need a separate function here, because it's only called once. Replace the isFunction
call with the typeof
check instead.
{Function}
Closing in favor of #225 Thx :) |
This PR contains a:
Motivation / Use-Case
closes #143
Breaking Changes
Additional Info
Wasn't able to get the tests to run! But wrote a test for the filename-function.