microbundle icon indicating copy to clipboard operation
microbundle copied to clipboard

Exported PropTypes shapes get lost in translation

Open romainbessugesmeusy opened this issue 4 years ago • 5 comments

Hi there, Thank you for the great work you've done with microbundle. For the record, here is how I'm using the lib: microbundle --jsx React.createElement --no-compress --format modern,cjs

I'm using react in version 17 and microbundle in version 0.12.0-next.9

I've got a PropType shape that is used in several definitions:

import PropTypes from "prop-types";

 const LocaleShape = PropTypes.shape({
    // two-char language code ("en", "fr", "de")
    tag: PropTypes.string.isRequired,
    // human readable language description
    language: PropTypes.oneOf([PropTypes.object, PropTypes.string]).isRequired,
    //latin, cyrillic, etc.
    script: PropTypes.string,
    // two-char region code ("US", "CA", "GB", "CH")
    region: PropTypes.string,
    // text direction / orientation: right to left, left to right
    orientation: PropTypes.oneOf(["rtl", "ltr"]),
    // optional emoji string for displaying a flag 🇨🇬 🇲🇼 🇹🇩
    flag: PropTypes.string,
    // String that can be used to search for a locale
    keywords: PropTypes.string,
    // locale identifier of the fallback locale
    fallback: PropTypes.string,
    // optional identifier. If not specified, locale identifier will result in the concatenation
    // of tag and region, with a dash separator: "en-GB", "fr-CA", "nl-BE"
    id: PropTypes.string,
});
export default LocaleShape;

This shape is imported in different files:

LocaleChooser.propTypes = {
  value: PropTypes.oneOfType([LocaleShape, PropTypes.arrayOf(LocaleShape)]),
  locales: PropTypes.arrayOf(LocaleShape),
}

When the bundle is built, it looks like the exported PropType shape is turned into null, as explain the warnings I receive in console:

Warning: Failed prop type: Invalid prop `locales[0].language` of value `[object Object]` supplied to `LocaleChooser`, expected one of [null,null].

This warning is suppressed once I remove the imports and duplicate the LocaleShape in every file that requires it. Am I missing a parameter in the command?

Thanks for your help

romainbessugesmeusy avatar Dec 17 '20 13:12 romainbessugesmeusy

How are you importing LocaleShape in your LocaleChooser module?

developit avatar Dec 18 '20 19:12 developit

Like this :

import LocaleShape from "./LocalShape";

(IDE autocompletion is OK, and sees the LocaleShape)

romainbessugesmeusy avatar Dec 18 '20 19:12 romainbessugesmeusy

Any chance you happen to have a custom Babel configuration? Some presets for React include babel-plugin-transform-react-remove-prop-types in production, which is the environment microbundle builds for, and that would replace LocaleShape with null.

developit avatar Dec 18 '20 19:12 developit

Hmm it does not seem I do. I've used react-create-library but replaced their microbundle fork by the latest version. And.. that's it. I'll try to look for the transform you mention in my node_modules but I've got no babel configuration in my repository.

romainbessugesmeusy avatar Dec 18 '20 19:12 romainbessugesmeusy

The only places it would could be that Microbundle looks it would be:

babel.config.js .babelrc (or .babelrc.*) package.json "babel" field

Another (wild) possibility: Babel searches all the way up the filesystem tree to find configurations, so if you happen to have one in your home directory or somewhere, it'll find that.

developit avatar Dec 18 '20 19:12 developit