-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
web-components-webpack5
framework doc
- Loading branch information
Showing
8 changed files
with
220 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,3 @@ | ||
# Storybook for web-components | ||
# Storybook for Web components & Webpack | ||
|
||
--- | ||
|
||
Storybook for web-components is a UI development environment for your plain web-component snippets. | ||
With it, you can visualize different states of your UI components and develop them interactively. | ||
|
||
![Storybook Screenshot](https://github.com/storybookjs/storybook/blob/main/media/storybook-intro.gif) | ||
|
||
Storybook runs outside of your app. | ||
So you can develop UI components in isolation without worrying about app specific dependencies and requirements. | ||
|
||
## Getting Started | ||
|
||
```sh | ||
cd my-app | ||
npx storybook@latest init -t web_components | ||
``` | ||
|
||
For more information visit: [storybook.js.org](https://storybook.js.org) | ||
|
||
--- | ||
|
||
Storybook also comes with a lot of [addons](https://storybook.js.org/addons) and a great API to customize as you wish. | ||
You can also build a [static version](https://storybook.js.org/docs/web-components/sharing/publish-storybook) of your storybook and deploy it anywhere you want. | ||
|
||
# Hot Module Reloading (HMR) | ||
|
||
As web components register on a global registry which only accepts a certain name/class once it can lead to errors when using classical HMR. There are ideas on how to archive HMR with a static registry but there is no proven solution yet. Therefore the best approach for now is to do full page reloads. If you keep your stories to specific states of components (which we would recommend anyways) this usually means it is fast. | ||
|
||
# Setup es6/7 dependencies | ||
|
||
By default storybook only works with precompiled ES5 code but as most web components themselves and their libs are distributed as ES2017 you will need to manually mark those packages as "needs transpilation". | ||
|
||
For example if you have a library called `my-library` which is in ES2017 then you can add it like so | ||
|
||
```js | ||
// .storybook/main.js | ||
|
||
export default { | ||
webpackFinal: async (config) => { | ||
// find web-components rule for extra transpilation | ||
const webComponentsRule = config.module.rules.find( | ||
(rule) => rule.use && rule.use.options && rule.use.options.babelrc === false | ||
); | ||
// add your own `my-library` | ||
webComponentsRule.test.push(new RegExp(`node_modules(\\/|\\\\)my-library(.*)\\.js$`)); | ||
|
||
return config; | ||
}, | ||
}; | ||
``` | ||
|
||
By default the following folders are included | ||
|
||
- `src/*.js` | ||
- `packages/*/src/*.js` | ||
- `node_modules/lit-html/*.js` | ||
- `node_modules/lit-element/*.js` | ||
- `node_modules/@open-wc/*.js` | ||
- `node_modules/@polymer/*.js` | ||
- `node_modules/@vaadin/*.js` | ||
|
||
As you can see the `src` folder is also included. | ||
The reason for that is as it has some extra configuration to allow for example `import.meta`. | ||
If you use a different folder you will need to make sure webpack/babel can handle it. | ||
|
||
# FAQ | ||
|
||
- While working on my component I get the error `Failed to execute 'define' on 'CustomElementRegistry': the name "..." has already been used with this registry` | ||
=> please see <a href="#user-content-setup-page-reload-via-hmr">Setup page reload via HMR</a> | ||
See [documentation](https://storybook.js.org/docs/8.0/get-started/web-components-webpack5?renderer=web-components) for installation instructions, usage examples, APIs, and more. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
--- | ||
title: Storybook for Web components & Webpack | ||
--- | ||
|
||
export const SUPPORTED_RENDERER = 'web-components'; | ||
|
||
Storybook for Web components & Webpack is a [framework](../contribute/framework.md) that makes it easy to develop and test UI components in isolation for applications using [Web components](https://www.webcomponents.org/introduction). | ||
|
||
<If notRenderer={SUPPORTED_RENDERER}> | ||
|
||
<Callout variant="info"> | ||
|
||
Storybook for Web components & Webpack is only supported in [Web components](?renderer=web-components) projects. | ||
|
||
</Callout> | ||
|
||
<!-- End non-supported renderers --> | ||
|
||
</If> | ||
|
||
<If renderer={SUPPORTED_RENDERER}> | ||
|
||
## Requirements | ||
|
||
- Webpack ≥ 5.0 | ||
- Storybook ≥ 7.x | ||
|
||
## Getting started | ||
|
||
### In a project without Storybook | ||
|
||
Follow the prompts after running this command in your Sveltekit project's root directory: | ||
|
||
<!-- prettier-ignore-start --> | ||
|
||
<CodeSnippets | ||
paths={[ | ||
'common/init-command.npx.js.mdx', | ||
'common/init-command.yarn.js.mdx', | ||
'common/init-command.pnpm.js.mdx', | ||
]} | ||
/> | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
[More on getting started with Storybook.](./install.md) | ||
|
||
### In a project with Storybook | ||
|
||
This framework is designed to work with Storybook 7+. If you’re not already using v7, upgrade with this command: | ||
|
||
<!-- prettier-ignore-start --> | ||
|
||
<CodeSnippets | ||
paths={[ | ||
'common/storybook-upgrade.npm.js.mdx', | ||
'common/storybook-upgrade.pnpm.js.mdx', | ||
'common/storybook-upgrade.yarn.js.mdx' | ||
]} | ||
/> | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
#### Automatic migration | ||
|
||
When running the `upgrade` command above, you should get a prompt asking you to migrate to `@storybook/sveltekit`, which should handle everything for you. In case that auto-migration does not work for your project, refer to the manual migration below. | ||
|
||
#### Manual migration | ||
|
||
First, install the framework: | ||
|
||
<!-- prettier-ignore-start --> | ||
|
||
<CodeSnippets | ||
paths={[ | ||
'web-components/web-components-webpack5-install.npm.js.mdx', | ||
'web-components/web-components-webpack5-install.pnpm.js.mdx', | ||
'web-components/web-components-webpack5-install.yarn.js.mdx' | ||
]} | ||
/> | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
Then, update your `.storybook/main.js|ts` to change the framework property: | ||
|
||
<!-- prettier-ignore-start --> | ||
|
||
<CodeSnippets | ||
paths={[ | ||
'web-components/web-components-webpack5-add-framework.js.mdx', | ||
'web-components/web-components-webpack5-add-framework.ts.mdx' | ||
]} | ||
/> | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
## Hot Module Reloading (HMR) | ||
|
||
Web components are registered on global registry, which only accepts a given name/class once. That can lead to errors when using classical HMR. While there are ideas on how to achieve HMR with a static registry, there is no proven solution yet. Therefore, the best approach for now is to do full page reloads while developing. You can ensure those page reloads happen quickly by defining your stories as specific states of components (which we would recommend regardless). | ||
|
||
## Set up es6/7 dependencies | ||
|
||
By default, Storybook only works with precompiled ES5 code. Because most web components themselves and their libs are distributed as ES2017, you will need to manually mark those packages as "needs transpilation". | ||
|
||
For example, if you have a library called `my-library` which is in ES2017, then you can configure your project like so: | ||
|
||
```js | ||
// .storybook/main.js | ||
|
||
export default { | ||
webpackFinal: async (config) => { | ||
// Find web-components rule for extra transpilation | ||
const webComponentsRule = config.module.rules.find( | ||
(rule) => rule.use && rule.use.options && rule.use.options.babelrc === false | ||
); | ||
// Add your own `my-library` | ||
webComponentsRule.test.push(new RegExp(`node_modules(\\/|\\\\)my-library(.*)\\.js$`)); | ||
|
||
return config; | ||
}, | ||
}; | ||
``` | ||
|
||
By default, the following folders are transpiled: | ||
|
||
- `src/*.js` | ||
- `packages/*/src/*.js` | ||
- `node_modules/lit-html/*.js` | ||
- `node_modules/lit-element/*.js` | ||
- `node_modules/@open-wc/*.js` | ||
- `node_modules/@polymer/*.js` | ||
- `node_modules/@vaadin/*.js` | ||
|
||
<Callout variant="info"> | ||
|
||
Note that the `src` folder is also included. This provides some extra configuration to allow for `import.meta` and some other features. | ||
|
||
If you use a folder for your components/stories other than `src`, you will need to use the configuration example above to have it properly transpiled. | ||
|
||
</Callout> | ||
|
||
## API | ||
|
||
### Options | ||
|
||
You can pass an options object for additional configuration if needed: | ||
|
||
```js | ||
// .storybook/main.js | ||
import * as path from 'path'; | ||
|
||
export default { | ||
// ... | ||
framework: { | ||
name: '@storybook/web-components-webpack5', | ||
options: { | ||
// ... | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
The available options are: | ||
|
||
#### `builder` | ||
|
||
Type: `Record<string, any>` | ||
|
||
Configure options for the [framework's builder](../api/main-config-framework.md#optionsbuilder). For this framework, available options can be found in the [Webpack builder docs](../builders/webpack.md). | ||
|
||
## Troubleshooting | ||
|
||
### Error while developing with HMR | ||
|
||
While developing a component, you might encounter this error: | ||
|
||
```sh | ||
Failed to execute 'define' on 'CustomElementRegistry': the name "..." has already been used with this registry | ||
``` | ||
|
||
This error occurs because the component is already registered in the global registry. See the [limitations of HMR with web components](#hot-module-reloading-hmr) for more information. | ||
|
||
<!-- End supported renderers --> | ||
|
||
</If> |
8 changes: 8 additions & 0 deletions
8
docs/snippets/web-components/web-components-webpack5-add-framework.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
```js | ||
// .storybook/main.js | ||
export default { | ||
// ... | ||
// framework: '@storybook/react-webpack5', 👈 Remove this | ||
framework: '@storybook/nextjs', // 👈 Add this | ||
}; | ||
``` |
11 changes: 11 additions & 0 deletions
11
docs/snippets/web-components/web-components-webpack5-add-framework.ts.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
```ts | ||
// .storybook/main.ts | ||
import { StorybookConfig } from '@storybook/web-components-webpack5'; | ||
|
||
const config: StorybookConfig = { | ||
// ... | ||
framework: '@storybook/web-components-webpack5', // 👈 Add this | ||
}; | ||
|
||
export default config; | ||
``` |
3 changes: 3 additions & 0 deletions
3
docs/snippets/web-components/web-components-webpack5-install.npm.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```shell | ||
npm install --save-dev @storybook/web-components-webpack5 | ||
``` |
3 changes: 3 additions & 0 deletions
3
docs/snippets/web-components/web-components-webpack5-install.pnpm.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```shell | ||
pnpm install --save-dev @storybook/web-components-webpack5 | ||
``` |
3 changes: 3 additions & 0 deletions
3
docs/snippets/web-components/web-components-webpack5-install.yarn.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```shell | ||
yarn add --dev @storybook/web-components-webpack5 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters