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

webpack: handle a couple more cfg.entry formats #197

Merged
merged 5 commits into from
Aug 11, 2023

Conversation

thomasjm
Copy link
Contributor

This fixes a crash I was getting in my project based on Razzle.js, because the Webpack plugin code wasn't handling all of the cases that can happen in the configuration's entry field. (Specifically, when the entry is an object and its keys are of type string[].)

This is also a little more defensively written and (IMO) easier to understand.

Based on the TS types here:

https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/types.d.ts#L2396-L2400

Copy link
Collaborator

@webpro webpro left a comment

Choose a reason for hiding this comment

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

Thanks @thomasjm! Happy to merge, but please run npm run format first :)

else if (typeof entry === "function") entries.push((entry as () => string)());
else if (entry && typeof entry === "object" && "filename" in entry) entries.push(entry["filename"] as string);
else {
// TODO: warn about unrecognized single entry in object
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not Knip's responsibility to validate/lint the configuration of external tooling. The plugins just try to find dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wouldn't say it's about validation or linting, but instead making it easier to detect when Webpack changes/extends its configuration schema and the plugin doesn't recognize it. Rather than crash as happened in my case :P

I left this note in case knip has a preferred way of surfacing such warnings. But I can remove the comments if you like.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There's no handling of such config issues (I'd leave that to e.g. Webpack in this case), other than handling valid configuration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right -- I'm not sure you've understood me. The scenario here is that some configuration is perfectly valid according to Webpack, but hasn't been handled in Knip for some reason. So a warning would be useful to make it clear that Knip needs to handle a new case.

But it sounds like you don't want it, so I'll remove these branches.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks, what you describe is the ongoing process of what is Knip is all about, to keep iterating and improving based on fixes like yours. Unless you already have a specific case in mind that you know should be handled, the comment does not add much for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh well the suggestion was to turn the comment into a console.warn, or some other suitable way of surfacing a warning.

That way the next person who runs into the same scenario won't have to decipher an opaque error. (My error was an undefined string deep inside some stacktrace.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But no worries either way, my use-case works after this fix so I'm happy.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Exception handling in general is something that should be improved indeed!

src/plugins/webpack/index.ts Outdated Show resolved Hide resolved
@webpro webpro merged commit c45cb07 into webpro-nl:main Aug 11, 2023
9 checks passed
@webpro
Copy link
Collaborator

webpro commented Aug 11, 2023

🚀 This pull request is included in v2.19.3. See Release 2.19.3 for release notes.

@webpro
Copy link
Collaborator

webpro commented Aug 11, 2023

Thanks again Tom! 🙏

@thomasjm
Copy link
Contributor Author

No problem, thanks for the fast review!

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

Successfully merging this pull request may close these issues.

2 participants