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

chore: keep prop-types if mode is not production #372

Merged
merged 1 commit into from
Dec 29, 2018

Conversation

montezume
Copy link
Contributor

@montezume montezume commented Dec 28, 2018

Summary

@tdeekens made a good write up about the problem here.

TL;DR: It will be easier for our consumers to use ui-kit if they get prop-types warnings during development.

Approach

Use mode wrap from babel-plugin-transform-react-remove-prop-types.

Which transforms our propTypes into

var nonEmptyString = process.env.NODE_ENV !== "production" ? function (props, propName, componentName) {
  var value = props[propName];
  if (typeof value === 'string' && !value) return new Error("Invalid prop '".concat(propName, "' supplied to '").concat(componentName, "'. Expected it to be nonempty string, but it was empty."));
  return null;
} : {};
// more stuff
Headline.propTypes = process.env.NODE_ENV !== "production" ? {
  elementType: PropTypes.oneOf(['h1', 'h2', 'h3']).isRequired,
  children: PropTypes.node.isRequired,
  title: nonEmptyString,
  truncate: PropTypes.bool
} : {};

Why not unsafe-wrap? There's an open issue with using unsafe wrap when you use propTypes from variables, as we do in Text.Headline and (I believe?) some other components.

Another possible approach

Use unsafe-wrap but stop using variables as propTypes. IE, copy paste nonEmptyString into the propType declarations in each other Text components.

This gives the output of

process.env.NODE_ENV !== "production" ? Headline.propTypes = {
  elementType: PropTypes.oneOf(['h1', 'h2', 'h3']).isRequired,
  children: PropTypes.node.isRequired,
  title: function title(props, propName, componentName) {
    var value = props[propName];
    if (typeof value === 'string' && !value) return new Error("Invalid prop '".concat(propName, "' supplied to '").concat(componentName, "'. Expected it to be nonempty string, but it was empty."));
    return null;
  },
  truncate: PropTypes.bool
} : void 0;

If we go with this approach, perhaps we can create a custom linter rule that doesn't allow variables for proptypes? 🤔

TL;DR

The second approach will produce a nicer production bundle, at the expense of having to duplicate custom propTypes, and without some sort of custom linter rule, it would be difficult to explain to developers why their code is breaking when they are using variables as proptypes...

WDYT?

Copy link
Member

@emmenko emmenko left a comment

Choose a reason for hiding this comment

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

@montezume
Copy link
Contributor Author

@emmenko I'm not sure I see the advantage? UI-Kit only exists as a library, not an application. The MC-preset has to have that code because sometimes it's used to build an application, and sometimes a library. UI-Kit only ever builds libraries.

@emmenko
Copy link
Member

emmenko commented Dec 28, 2018

Of course, I got confused sorry 🙈

Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

Thanks a bunch for spreading the fix!

@montezume montezume force-pushed the ml-babel-preset-update branch from c2b4a95 to f8b878a Compare December 28, 2018 22:35
@montezume montezume merged commit 037297e into master Dec 29, 2018
@montezume montezume deleted the ml-babel-preset-update branch December 29, 2018 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants