Skip to content

Commit

Permalink
auto import dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Jul 12, 2021
1 parent a30adac commit 4aeb804
Show file tree
Hide file tree
Showing 22 changed files with 46 additions and 32 deletions.
3 changes: 1 addition & 2 deletions docs/assets/plugins/metadata/metadata.js
Expand Up @@ -420,8 +420,7 @@
result += `
## Dependencies
This component has the following dependencies so, if you're [cherry picking](/getting-started/installation#cherry-picking),
be sure to import these components in addition to <code>&lt;${tag}&gt;</code>.
This component imports the following dependencies.
${createDependenciesList(component.tagName, getAllComponents(metadata))}
`;
Expand Down
9 changes: 3 additions & 6 deletions docs/getting-started/installation.md
Expand Up @@ -56,24 +56,21 @@ However, if you're [cherry picking](#cherry-picking) or [bundling](#bundling) Sh

The previous approach is the _easiest_ way to load Shoelace, but easy isn't always efficient. You'll incur the full size of the library even if you only use a handful of components. This is convenient for prototyping, but may result in longer load times in production. To improve this, you can cherry pick the components you need.

Cherry picking can be done from your local install or [directly from the CDN](https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@%VERSION%/). This will limit the number of files the browser has to download and reduce the amount of bytes being transferred. The disadvantage is that you need to load and register each component manually, including its dependencies.
Cherry picking can be done from your local install or [directly from the CDN](https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@%VERSION%/). This will limit the number of files the browser has to download and reduce the amount of bytes being transferred. The disadvantage is that you need to load and register each component manually.

Here's an example that loads only the button component and its dependencies. Again, if you're not using a module resolver, you'll need to adjust the path to point to the folder Shoelace is in.
Here's an example that loads only the button component. Again, if you're not using a module resolver, you'll need to adjust the path to point to the folder Shoelace is in.

```html
<!-- The base stylesheet is always required -->
<link rel="stylesheet" href="@shoelace-style/shoelace/dist/themes/base.css">

<script type="module" data-shoelace="/scripts/shoelace">
import SlButton from '@shoelace-style/shoelace/dist/components/button/button.js';
import SlSpinner from '@shoelace-style/shoelace/dist/components/spinner/spinner.js';
// <sl-button> and <sl-spinner> are ready to use!
// <sl-button> is ready to use!
</script>
```

If a component has dependencies, they'll be listed in the "Dependencies" section of its documentation. These are always Shoelace components, not third-party libraries. For example, `<sl-button>` requires you to load `<sl-spinner>` because it's used internally for its loading state.

!> Never cherry pick components or utilities from `shoelace.js` as this will cause the browser to load the entire library. Instead, cherry pick from specific modules as shown above.

!> You will see files named `chunk.[hash].js` in the `chunks` directory. Never import these files directly, as they are generated and change from version to version.
Expand Down
20 changes: 0 additions & 20 deletions docs/getting-started/usage.md
Expand Up @@ -164,26 +164,6 @@ const MyComponent = (props) => {
};
```

### Dependencies

Some components depend on other components internally. For example, `<sl-button>` requires you to load `<sl-spinner>` because it's used internally for its loading state. If a component has dependencies, they'll be listed in the "Dependencies" section of its documentation. These are always Shoelace components, not third-party libraries.

Since dependencies are just components, you can load them the same way.

```jsx
import SlButton from '@shoelace-style/react/dist/button';
import SlSpinner from '@shoelace-style/react/dist/spinner';
```

However, this may cause your linter to complain (e.g. "SlButton is defined but never used"). If you're not going to use the dependent components in your JSX, you can import them as side effects instead.

```jsx
import '@shoelace-style/react/dist/button';
import '@shoelace-style/react/dist/spinner';
```

This extra step is required for dependencies to ensure they get registered with the browser as custom elements.

## Vue

Vue [plays nice](https://custom-elements-everywhere.com/#vue) with custom elements. You just have to tell it to ignore Shoelace components. This is pretty easy because they all start with `sl-`.
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/changelog.md
Expand Up @@ -8,7 +8,10 @@ _During the beta period, these restrictions may be relaxed in the event of a mis

## Next

This release improves how component dependencies are imported. If you've been cherry picking, you no longer need to import component dependencies manually. This significantly improves developer experience, making Shoelace even easier to use. For transparency, component dependencies will continue to be listed in the docs.

- Added "Reflects" column to the properties table
- Dependencies are now automatically imported for all components

## 2.0.0-beta.46

Expand Down
2 changes: 2 additions & 0 deletions src/components/alert/alert.ts
Expand Up @@ -8,6 +8,8 @@ import { waitForEvent } from '../../internal/event';
import { getAnimation, setDefaultAnimation } from '../../utilities/animation-registry';
import styles from './alert.styles';

import '../icon-button/icon-button';

const toastStack = Object.assign(document.createElement('div'), { className: 'sl-toast-stack' });

/**
Expand Down
2 changes: 2 additions & 0 deletions src/components/avatar/avatar.ts
Expand Up @@ -3,6 +3,8 @@ import { customElement, property, state } from 'lit/decorators.js';
import { classMap } from 'lit-html/directives/class-map';
import styles from './avatar.styles';

import '../icon/icon';

/**
* @since 2.0
* @status stable
Expand Down
2 changes: 2 additions & 0 deletions src/components/button/button.ts
Expand Up @@ -6,6 +6,8 @@ import { emit } from '../../internal/event';
import { hasSlot } from '../../internal/slot';
import styles from './button.styles';

import '../spinner/spinner';

/**
* @since 2.0
* @status stable
Expand Down
5 changes: 5 additions & 0 deletions src/components/color-picker/color-picker.ts
Expand Up @@ -11,6 +11,11 @@ import type SlInput from '../input/input';
import color from 'color';
import styles from './color-picker.styles';

import '../button/button';
import '../dropdown/dropdown';
import '../icon/icon';
import '../input/input';

/**
* @since 2.0
* @status stable
Expand Down
2 changes: 2 additions & 0 deletions src/components/details/details.ts
Expand Up @@ -9,6 +9,8 @@ import { focusVisible } from '../../internal/focus-visible';
import { getAnimation, setDefaultAnimation } from '../../utilities/animation-registry';
import styles from './details.styles';

import '../icon/icon';

let id = 0;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/components/dialog/dialog.ts
Expand Up @@ -13,6 +13,8 @@ import Modal from '../../internal/modal';
import { setDefaultAnimation, getAnimation } from '../../utilities/animation-registry';
import styles from './dialog.styles';

import '../icon-button/icon-button';

const hasPreventScroll = isPreventScrollSupported();

let id = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/components/drawer/drawer.ts
Expand Up @@ -14,6 +14,8 @@ import Modal from '../../internal/modal';
import { setDefaultAnimation, getAnimation } from '../../utilities/animation-registry';
import styles from './drawer.styles';

import '../icon-button/icon-button';

const hasPreventScroll = isPreventScrollSupported();

let id = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/components/icon-button/icon-button.ts
Expand Up @@ -5,6 +5,8 @@ import { ifDefined } from 'lit-html/directives/if-defined';
import { focusVisible } from '../../internal/focus-visible';
import styles from './icon-button.styles';

import '../icon/icon';

/**
* @since 2.0
* @status stable
Expand Down
2 changes: 2 additions & 0 deletions src/components/image-comparer/image-comparer.ts
Expand Up @@ -6,6 +6,8 @@ import { emit } from '../../internal/event';
import { watch } from '../../internal/watch';
import styles from './image-comparer.styles';

import '../icon/icon';

/**
* @since 2.0
* @status stable
Expand Down
2 changes: 2 additions & 0 deletions src/components/input/input.ts
Expand Up @@ -8,6 +8,8 @@ import { getLabelledBy, renderFormControl } from '../../internal/form-control';
import { hasSlot } from '../../internal/slot';
import styles from './input.styles';

import '../icon/icon';

let id = 0;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/components/menu-divider/menu-divider.ts
Expand Up @@ -6,8 +6,6 @@ import styles from './menu-divider.styles';
* @since 2.0
* @status stable
*
* @dependency sl-menu
*
* @csspart base - The component's base wrapper.
*/
@customElement('sl-menu-divider')
Expand Down
2 changes: 2 additions & 0 deletions src/components/menu-item/menu-item.ts
Expand Up @@ -4,6 +4,8 @@ import { classMap } from 'lit-html/directives/class-map';
import { watch } from '../../internal/watch';
import styles from './menu-item.styles';

import '../icon/icon';

/**
* @since 2.0
* @status stable
Expand Down
2 changes: 0 additions & 2 deletions src/components/menu-label/menu-label.ts
Expand Up @@ -6,8 +6,6 @@ import styles from './menu-label.styles';
* @since 2.0
* @status stable
*
* @dependency sl-menu
*
* @slot - The menu label's content.
*
* @csspart base - The component's base wrapper.
Expand Down
2 changes: 2 additions & 0 deletions src/components/rating/rating.ts
Expand Up @@ -9,6 +9,8 @@ import { focusVisible } from '../../internal/focus-visible';
import { clamp } from '../../internal/math';
import styles from './rating.styles';

import '../icon/icon';

/**
* @since 2.0
* @status stable
Expand Down
6 changes: 6 additions & 0 deletions src/components/select/select.ts
Expand Up @@ -13,6 +13,12 @@ import type SlMenu from '../menu/menu';
import type SlMenuItem from '../menu-item/menu-item';
import styles from './select.styles';

import '../dropdown/dropdown';
import '../icon/icon';
import '../icon-button/icon-button';
import '../menu/menu';
import '../tag/tag';

let id = 0;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/components/tab-group/tab-group.ts
Expand Up @@ -10,6 +10,8 @@ import type SlTab from '../tab/tab';
import type SlTabPanel from '../tab-panel/tab-panel';
import styles from './tab-group.styles';

import '../icon-button/icon-button';

/**
* @since 2.0
* @status stable
Expand Down
2 changes: 2 additions & 0 deletions src/components/tab/tab.ts
Expand Up @@ -4,6 +4,8 @@ import { classMap } from 'lit-html/directives/class-map';
import { emit } from '../../internal/event';
import styles from './tab.styles';

import '../icon-button/icon-button';

let id = 0;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/components/tag/tag.ts
Expand Up @@ -4,6 +4,8 @@ import { classMap } from 'lit-html/directives/class-map';
import { emit } from '../../internal/event';
import styles from './tag.styles';

import '../icon-button/icon-button';

/**
* @since 2.0
* @status stable
Expand Down

0 comments on commit 4aeb804

Please sign in to comment.