Skip to content
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

Closed
wants to merge 6 commits into from
Closed

feat: allow filename to be a {Function} #145

wants to merge 6 commits into from

Conversation

berthertogen
Copy link

@berthertogen berthertogen commented May 14, 2018

This PR contains a:

  • bugfix
  • new feature
  • code refactor
  • test update
  • typo fix
  • metadata update

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.

@jsf-clabot
Copy link

jsf-clabot commented May 14, 2018

CLA assistant check
All committers have signed the CLA.

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`
Copy link
Member

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) {
Copy link
Member

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'

Copy link
Contributor

@Jessidhia Jessidhia left a 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 ;)

@aaarichter
Copy link

looking forward to this :octocat:

src/index.js Outdated
@@ -366,6 +371,16 @@ class MiniCssExtractPlugin {
});
}

getFilename(chunk, filename) {
console.log('--chunk--');
console.log(chunk);
Copy link
Member

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
@berthertogen
Copy link
Author

Sorry it took so long, I remove the console logs and made sure the filename function is called only once.

@glen-84
Copy link

glen-84 commented Jun 16, 2018

Is there enough information in chunk to figure out the path used to reference the CSS file/the path on disk? (see #92)

filenameTemplate: this.getFilename(
chunk,
this.options.filename,
this.options.processedFilename
Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Contributor

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?

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.

Copy link

@shellscape shellscape left a 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(

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(

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,

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) {

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) {

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) {

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.

@michael-ciniawsky michael-ciniawsky changed the title feat: allow filename to be a function feat: allow filename to be a {Function} Aug 6, 2018
@michael-ciniawsky michael-ciniawsky added this to the 0.5.0 milestone Aug 6, 2018
@michael-ciniawsky
Copy link
Member

Closing in favor of #225

Thx :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: allow the option filename to be a function
9 participants