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

docs: refine Babel config merging strategies #2531

Merged
merged 6 commits into from May 28, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented May 14, 2021

I realize that we didn't mention how we merge parserOpts, generatorOpts and assumptions. This PR documents current merging strategies and updates the examples.

Preview

Resolves #2536

@JLHwung JLHwung added the docs label May 14, 2021
docs/configuration.md Outdated Show resolved Hide resolved
@netlify
Copy link

netlify bot commented May 14, 2021

Deploy preview for babel ready!

Built with commit f81c02c

https://deploy-preview-2531--babel.netlify.app

@netlify
Copy link

netlify bot commented May 14, 2021

✔️ Deploy Preview for babel ready!

🔨 Explore the source changes: 963a15d

🔍 Inspect the deploy log: https://app.netlify.com/sites/babel/deploys/60b0ec7caa415700076b79e8

😎 Browse the preview: https://deploy-preview-2531--babel.netlify.app

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

We can also show an assuption which is set to true in the first config and false in the second.

For each config items mentioned above, Babel applies `Object.assign` on options except for `plugins` and `presets`, which is concatenated by `Array#concat`. For example
For each config items mentioned above, Babel applies `Object.assign` on the option object, which means the option value will be overwritten by another one with higher priority. However, the following options have specialized strategies:

- for `plugins` and `presets`, Babel merge them by `Array#concat`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- for `plugins` and `presets`, Babel merge them by `Array#concat`.
- for `plugins` and `presets`, Babel merges them via `Array#concat`.

For each config items mentioned above, Babel applies `Object.assign` on the option object, which means the option value will be overwritten by another one with higher priority. However, the following options have specialized strategies:

- for `plugins` and `presets`, Babel merge them by `Array#concat`.
- for `parserOpts`, `generatorOpts` and `assumptions`, Babel merge them by `Object.assign`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- for `parserOpts`, `generatorOpts` and `assumptions`, Babel merge them by `Object.assign`
- for `parserOpts`, `generatorOpts` and `assumptions`, Babel merges them via `Object.assign`

@JLHwung
Copy link
Contributor Author

JLHwung commented May 25, 2021

I didn't realize we already have option merging docs on https://babeljs.io/docs/en/options#merging, thanks to @thernstig who points it out. Since the options.md is very verbose, I move this section to configuration and added the assumptions and more examples.

@thernstig Can you take a review if it resolves #2536? The preview link is here.

Babel's configuration merging is relatively straightforward. Options will overwrite existing options
when they are present, and their value is not `undefined`, with a few special cases:

- For `assumptions`, `parserOpts` and `generatorOpts`, objects are merged, rather than replaced, using the same logic as top-level options.
Copy link
Contributor

Choose a reason for hiding this comment

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

using the same logic as top-level options.

I don't think that logic has been explained anywhere else. But since the first part of the sentence says objects are merged, maybe this part is superfluous?

The confusion for me is that the options you list are themselves "top-level options": https://babeljs.io/docs/en/options

docs/configuration.md Outdated Show resolved Hide resolved
@@ -196,34 +198,99 @@ If your input is ignored by `ignore` or `only`, Babel will print that this file

### How Babel merges config items
Copy link
Contributor

Choose a reason for hiding this comment

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

This headline is called "How babel merges config items". But config items are "Options" in my world: https://babeljs.io/docs/en/options

Then later there is now a new headline "Options merging"

This makes it confusing to the reader. as the entire chapter is about options merging. Note that presets and plugins are also options, as indicated by the headline here: https://babeljs.io/docs/en/options#plugin-and-preset-options

};
```

When `NODE_ENV` is `test`, the `test` item will be merged on top of the top-level plugins.
Copy link
Contributor

Choose a reason for hiding this comment

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

top-level plugins

Mistake?

}
```

#### Plugin/Preset merging
Copy link
Contributor

Choose a reason for hiding this comment

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

Plugin and presets are also options: https://babeljs.io/docs/en/options

So the above "Options merging" makes this headline readers confused as everything described in this chapter are "Options". (This is the same comment as the ones above in a sense.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the intention is options except plugins/presets. A config item in @babel/core have plugins, presets and options. But yeah you are right, we should figure out a new term.

```

because each instance has been given a unique name and this a unique identity.
Please refer to [How Babel merges config items](configuration.md#how-babel-merges-config-items).
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice 😃

JLHwung and others added 2 commits May 26, 2021 10:37
Co-authored-by: Brian Ng <bng412@gmail.com>
docs/configuration.md Outdated Show resolved Hide resolved
@thernstig
Copy link
Contributor

thernstig commented May 28, 2021

LGTM 👍

@JLHwung JLHwung merged commit b5c28d5 into babel:main May 28, 2021
@JLHwung JLHwung deleted the refine-option-merge-strategies branch May 28, 2021 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Merging: Inconsistent descriptions of merging in docs "Configure Babel" and "Config Options"
4 participants