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

refactor upgradeConfig.js #50

Closed
wants to merge 6 commits into from
Closed

refactor upgradeConfig.js #50

wants to merge 6 commits into from

Conversation

noahlemen
Copy link
Member

refactored this file to be (in my opinion) a bit easier to follow

not attached to this approach, very open to other suggestions too!!

@@ -71,3 +71,14 @@ test('does not another flow preset if already present and hasFlow option passed'
expect(upgradeConfig(config, { hasFlow: true })).toMatchSnapshot();
});

test('does not another flow preset if already present in an array and hasFlow option passed', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

noticed this mid-refactor, not sure if this is a case worth handling -- but i figured it couldn't hurt to be safe

Copy link
Contributor

Choose a reason for hiding this comment

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

minor grammar fix:

- does not another
+ does not add another

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks! nice catch


function transformPreset(preset) {
let presetName = Array.isArray(preset) ? preset[0] : preset;
const presetOptions = Array.isArray(preset) ? preset[1] : {};
Copy link
Member Author

@noahlemen noahlemen Mar 3, 2018

Choose a reason for hiding this comment

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

normalizing presets to be "__Name" and "__Options" and then transforming them back down to a string (if options is empty) at the end allows more code to be shared preset/plugin formats

might seem a bit unnecessary right now, but it also simplifies operations on options a bit more

}

module.exports = function upgradeConfig(config, options) {
config = Object.assign({}, config);

changePresets(config, options);
changePlugins(config);
if (config.presets) {
Copy link
Member Author

Choose a reason for hiding this comment

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

i was thinking it might be clearer to only mutate config within upgradeConfig, but that might just be personal preference

@noahlemen
Copy link
Member Author

dug in more on reducing duplicate code on my last commit (6fcdef5)

may have gone too far with it, but curious to see if anyone thinks parts of it make sense to use here


if (!Array.isArray(presets) && typeof presets === 'string') {
presets = config.presets = config.presets.split(',').map((preset) => preset.trim());
if (name.indexOf(`babel-${configItemType}`) !== 0 && name.indexOf('@babel/') !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we use .includes instead of .indexOf? Gives a clearer intent imo.

Copy link
Member Author

@noahlemen noahlemen Mar 4, 2018

Choose a reason for hiding this comment

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

hmm -- i agree, but i do think we specifically want to check the beginning of the string, and includes would return true if it were anywhere in it (which would probably be a typo on the user's part, but still)

Copy link
Member Author

Choose a reason for hiding this comment

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

we could use startsWith though? may need a polyfill depending on versions of node we're trying to support?

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, looks like that should be supported even in node 4. let's use startsWith here

Object.keys(config.env).forEach((env) => {
changePresets(config.env[env]);
changePlugins(config.env[env]);
Object.keys(config.env).forEach(env => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Object.values is what you'd want to use here.

Copy link
Member Author

Choose a reason for hiding this comment

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

heh, great point. that totally didn't occur to me while moving this code around 😆

@@ -71,3 +71,14 @@ test('does not another flow preset if already present and hasFlow option passed'
expect(upgradeConfig(config, { hasFlow: true })).toMatchSnapshot();
});

test('does not another flow preset if already present in an array and hasFlow option passed', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

minor grammar fix:

- does not another
+ does not add another

changePresets(config.env[env]);
changePlugins(config.env[env]);
});
Object.values(config.env).forEach(envConfig => upgradeConfigForEnv(envConfig, options));
Copy link
Member

Choose a reason for hiding this comment

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

If we're supporting node 4 we'll need to polyfill this (or just go back to Object.keys)?

Copy link
Member Author

Choose a reason for hiding this comment

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

good point -- i forgot that node 4 doesn't support this. should we be concerned that CI for node 4 isn't failing?

Copy link
Member Author

Choose a reason for hiding this comment

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

now that i look back at the tests, env tests could probably be significantly more extensive. this seems like something that should be caught by travis

Copy link
Member

Choose a reason for hiding this comment

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

I don't think anyone will disagree that we need more tests :)

Copy link
Member Author

@noahlemen noahlemen Mar 6, 2018

Choose a reason for hiding this comment

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

noticed the linked PR does add preset-env with target as node 4 as well as adds @babel/polyfill to bin/babel-upgrade, so we should be ok to use Object.values here? confirmed that it works running in node 4 for me locally

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes, that's fine then

module.exports = {
packages,
presets,
plugins,
latestPackages,
packagesWarnAboutEnv
Copy link
Member

Choose a reason for hiding this comment

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

Where is this used?

Copy link
Member Author

Choose a reason for hiding this comment

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

hm not sure what this is, I suspect I pushed it up by accident

Copy link
Member Author

Choose a reason for hiding this comment

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

ah yeah i had started looking at the Log when replacing out preset-es2015,16,17,latest as FYI mentioned in the readme at some point and then accidentally added these changes to my commit

@existentialism
Copy link
Member

fwiw most of this code was just waiting on some more time for the tool to bake a bit, and I wouldn't have said it was high on the list of things that needed immediate cleanup.

(@hzoo and I had even discussed going to an internal set of plugins that would get passed config/deps down a chain to modify, but said we'd wait and see, and keep it simple if anything)

That being said, changes look good. Thoughts @hzoo?

@noahlemen
Copy link
Member Author

@existentialism that makes sense -- I completely understand if it feels too early to merge in something like this

out of curiosity, what parts of the existing code would you prioritize higher for immediate cleanup?

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.

3 participants