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

[dynamic-import-chunkname] Disallow chunk name when eager mode is enabled #3004

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

amsardesai
Copy link

@amsardesai amsardesai commented Apr 24, 2024

The dynamic-import-chunkname rule is useful for catching missing chunk names for imports, however much of our imports use webpackMode: "eager" to disable creating a separate chunk (i.e for lazy evaluation). Adding webpackChunkName anyway can force webpack to generate a chunk, defeating the purpose of the eager mode. This PR changes the rule to disallow chunk names when webpackMode: "eager" is set.

Related issue: #1904

@ljharb
Copy link
Member

ljharb commented Apr 24, 2024

When would you not want this option enabled?

@amsardesai
Copy link
Author

Never, really. I'll just make it the default.

@amsardesai amsardesai changed the title Add allowEagerMode option to dynamic-import-chunkname rule [dynamic-import-chunkname] Make chunk name optional when eager mode is enabled Apr 24, 2024
Copy link

codecov bot commented Apr 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.82%. Comparing base (c0ac54b) to head (c8eb588).

❗ Current head c8eb588 differs from pull request most recent head 7173569. Consider uploading reports for the commit 7173569 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3004      +/-   ##
==========================================
+ Coverage   95.66%   95.82%   +0.15%     
==========================================
  Files          78       78              
  Lines        3275     3279       +4     
  Branches     1150     1151       +1     
==========================================
+ Hits         3133     3142       +9     
+ Misses        142      137       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ljharb
Copy link
Member

ljharb commented Apr 24, 2024

Is there anyone who this could break? I assume not, because the only complaint would be that it fails to warn on something?

@amsardesai
Copy link
Author

Exactly, this only suppresses existing warnings in a narrow case, and doesn't introduce new warnings.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks, this is great!

One more enhancement we may want to consider before landing this: when webpackMode is eager, and a useless name is present, could we give a suggestion to remove the name?

@amsardesai
Copy link
Author

@ljharb Yeah that makes sense to me, just updated the PR to warn when chunk name and eager mode are both set.

Comment on lines 106 to 112
if (isChunknamePresent && isEagerModePresent) {
context.report({
node,
message:
`dynamic imports should not have both webpackChunkName and webpackMode: "eager"`,
});
}
Copy link
Member

@ljharb ljharb May 1, 2024

Choose a reason for hiding this comment

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

Suggested change
if (isChunknamePresent && isEagerModePresent) {
context.report({
node,
message:
`dynamic imports should not have both webpackChunkName and webpackMode: "eager"`,
});
}
if (isChunknamePresent && isEagerModePresent) {
context.report({
node,
message: 'dynamic imports should not have both webpackChunkName and webpackMode: "eager"`\',
});
}

also is there any reason why someone might still want a chunk name? if so, then this should be a suggestion instead of a warning

Copy link
Author

Choose a reason for hiding this comment

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

I don't think having a chunk name with eager mode makes sense, since eager mode explicitly disables creating a new chunk, so the module is just lazily evaluated instead of lazily loaded+evaluated. Adding a chunk name anyways basically reverts back to being webpackMode: 'lazy' and generates a new chunk. A warning would help users catch unintentional additional chunks when using webpackMode: 'eager'

Copy link
Member

Choose a reason for hiding this comment

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

ok, so having both is always a mistake. thanks!

Copy link
Member

Choose a reason for hiding this comment

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

altho actually - that means that removing the name would quietly change the mode from lazy to eager - i think this should be a suggestion to either remove the name, or remove/change the webpackMode, to be safe.

Copy link
Author

Choose a reason for hiding this comment

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

makes sense, i'll make that change!

@ljharb
Copy link
Member

ljharb commented May 2, 2024

@amsardesai sorry if i wasn't clear; a "suggestion" isn't prose giving the user a hint, it's an actual eslint feature where the editor will prompt them with multiple autofixes they have to explicitly choose. For an example, look for a rule with hasSuggestions: true in its schema.

@amsardesai
Copy link
Author

amsardesai commented May 2, 2024

Ah, I didn't realize you meant ESLint suggestions. Let me do that.

@amsardesai amsardesai changed the title [dynamic-import-chunkname] Make chunk name optional when eager mode is enabled [dynamic-import-chunkname] Disallow chunk name when eager mode is enabled May 2, 2024
@amsardesai
Copy link
Author

Hey @ljharb, would love to know if this can be merged now, or if any other changes need to be made!

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

Successfully merging this pull request may close these issues.

None yet

2 participants