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

Add guidance on importing best practices #61

Open
leerob opened this issue Aug 7, 2019 · 3 comments
Open

Add guidance on importing best practices #61

leerob opened this issue Aug 7, 2019 · 3 comments

Comments

@leerob
Copy link
Contributor

leerob commented Aug 7, 2019

Hey y'all! 👋

I spend most of my time working on a component library. It's closed source, but it's using React + Storybook inside a Lerna Monorepo.

Anyways, one thing that I've learned that I don't see reflected here (or in the consumers of the SDS) is how to properly import for the smallest bundle size.

If you import your component like this:

import {Button} from '@storybook/design-system';

Then it's pulling in the entire design system. Which, might be okay, but it also might not. Thus, you can change it to something like this.

import {Button} from '@storybook/design-system/dist/components/Button';

Which then only imports that specific file. That works, but then you can't combine imports into a single line. Thus, Ant Design created babel-plugin-import to translate dist/components/Button to the correct location.

An example Babel config might be this:

module.exports = {
    plugins: [
        [
            'import',
            {
                libraryDirectory: 'dist/components',
                libraryName: '@storybook/design-system'
            }
        ]
    ]
};

Should there be guidance here on what consumers should do to properly import (such as Ant Design has done)?

Cheers 🍻

@jsomsanith
Copy link
Contributor

jsomsanith commented Aug 7, 2019

Great point. But if we want to enforce that, wouldn’t it be better to remove the index, and add files at root that simply import/export a single component ?
root
|_ Avatar.js (import the real Avatar and export it)
|_ Button.js (import the real Button and export it)
|_ ....

With that we can just import from ˋ@storybook/design-system/Avatar` without knowing the file architecture.

@leerob
Copy link
Contributor Author

leerob commented Aug 7, 2019

@jsomsanith With that approach, the consumer would still need multiple lines for imports from the same package.

import {Avatar} from '@storybook/design-system/Avatar';
import {Button} from '@storybook/design-system/Button';

When you use that Babel plugin, you can simply do this.

import {Avatar, Button} from '@storybook/design-system';

Which is really helpful when you're dealing with a large component:

import {Avatar, Badge, Button, Checkbox, Highlight} from '@storybook/design-system';

@kylemh
Copy link
Member

kylemh commented Aug 7, 2019

#64

Working to resolve this.

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

No branches or pull requests

3 participants