-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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(core): allow configureWebpack to return undefined #6784
Conversation
✅ [V2] 🔨 Explore the source changes: 85d47f6 🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/623945a3226bbc0008a558ab 😎 Browse the preview: https://deploy-preview-6784--docusaurus-2.netlify.app |
⚡️ Lighthouse report for the changes in this PR:
Lighthouse ran on https://deploy-preview-6784--docusaurus-2.netlify.app/ |
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 don't really like this change. It is good practice in JS to ensure symmetric return, and ESLint has a rule (consistent-return
) to enforce this. Relying on the implicit undefined
return may be the source of errors and is generally a code smell. Docusaurus wants to promote good coding practices.
Still I would leave the final decision to @slorber
In any case, we want a better error message than the existing one:
I think allowing the code above is quite convenient. Users can also explicitly return undefined. Eventually we can force TS users to be more explicit: configureWebpack?: (
config: Configuration,
isServer: boolean,
utils: ConfigureWebpackUtils,
content: Content,
- ) => Configuration & {mergeStrategy?: ConfigureWebpackFnMergeStrategy};
+ ) => (Configuration & {mergeStrategy?: ConfigureWebpackFnMergeStrategy}) | null; JS users may still use implicit undefined and we can fail-safe in this case, or at least throw a better error message asking to return "null" explicitly? @yorkie I'm willing to merge this change but I'd like to have a test + a correct TS type Let us know if you want help |
No, the point is that this is succinct: configureWebpack(isServer) {
if (isServer) {
return {
foo: 'bar';
};
}
} But this is verbose: configureWebpack(isServer) {
if (isServer) {
return {
foo: 'bar';
};
}
return {};
} It doesn't matter if you return I think we could improve the error message, but nothing more. Returning |
I see IMHO this is more config than real user code Not 100% the same but there are multiple places where we accept "partial" configs and try to infer a good general behavior |
I still think that config is user code, but let's put stylistic preferences aside and talk about potential mistakes. What if the user made a mistake and forgot to write the return statement? These plugins are where the validation system can't cover. What about giving a nicer error message asking the user to explicitly return |
I don't know, this is something I would do myself on purpose. If I am against the idea of using implicit return on purpose, I can bring my own eslint config or use TS to prevent it from happening. We shouldn't force a user to absolutely have to return {} if they prefer using implicit undefined returns, like it's the case for 2/3 users of this PR :) If things can work on the 1st try without asking users to read another error message and do additional tasks, that doesn't seem like a bad idea to me. |
Fair, I would be fine with |
Slightly related choice: React allowing to render undefined: reactwg/react-18#75 |
After some pondering, I decided to not make typing changes. Otherwise, the return type would be @slorber If you read up that reactwg/react-18#75 thread, |
Merging this. I changed my mind because I realized we have null-safe lifecycles elsewhere, so at least we have precedents. |
Motivation
The above is a custom plugin to add a ProvidePlugin for server-side only, in this case, we don't wanna change the config for the client, but I have to return an empty object, so this PR is to improve the writing plugins in this case.
Have you read the Contributing Guidelines on pull requests?
Yes, and I will follow the commit guideline if this change LGTY :)
Test Plan
I'm not sure how to test this :)
Related PRs
None.