mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-01 05:05:25 +08:00
Web-components: webpack preset refactor
This commit is contained in:
parent
40a55bdddb
commit
29791bd3f2
@ -41,22 +41,17 @@
|
||||
"prepare": "node ../../scripts/prepare.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
||||
"@babel/plugin-syntax-import-meta": "^7.10.4",
|
||||
"@babel/preset-env": "^7.12.11",
|
||||
"@storybook/core-common": "6.5.0-alpha.64",
|
||||
"@storybook/core-server": "6.5.0-alpha.64",
|
||||
"@storybook/docs-tools": "6.5.0-alpha.64",
|
||||
"@storybook/preset-web-components-webpack": "6.5.0-alpha.64",
|
||||
"@storybook/renderer-web-components": "6.5.0-alpha.64",
|
||||
"@types/node": "^14.14.20 || ^16.0.0",
|
||||
"@types/webpack-env": "^1.16.0",
|
||||
"babel-plugin-bundled-import-meta": "^0.3.1",
|
||||
"core-js": "^3.8.2",
|
||||
"global": "^4.4.0",
|
||||
"react": "16.14.0",
|
||||
"react-dom": "16.14.0",
|
||||
"regenerator-runtime": "^0.13.7",
|
||||
"ts-dedent": "^2.0.0"
|
||||
"regenerator-runtime": "^0.13.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"lit-html": "2.0.2"
|
||||
|
@ -1,6 +1,6 @@
|
||||
import type { StorybookConfig } from '@storybook/core-common';
|
||||
|
||||
export const addons: StorybookConfig['addons'] = [
|
||||
'@storybook/preset-web-components-webpack',
|
||||
'@storybook/renderer-web-components',
|
||||
require.resolve('./framework-preset-web-components'),
|
||||
];
|
||||
|
71
presets/web-components-webpack/README.md
Normal file
71
presets/web-components-webpack/README.md
Normal file
@ -0,0 +1,71 @@
|
||||
# Storybook for web-components
|
||||
|
||||
---
|
||||
|
||||
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 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 sb 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/docs/web-components/configure/storybook-addons) and a great API to customize as you wish.
|
||||
You can also build a [static version](https://storybook.js.org/docs/web-components/workflows/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
|
||||
|
||||
module.exports = {
|
||||
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>
|
71
presets/web-components-webpack/package.json
Normal file
71
presets/web-components-webpack/package.json
Normal file
@ -0,0 +1,71 @@
|
||||
{
|
||||
"name": "@storybook/preset-web-components-webpack",
|
||||
"version": "6.5.0-alpha.64",
|
||||
"description": "Storybook for web-components: View web components snippets in isolation with Hot Reloading.",
|
||||
"keywords": [
|
||||
"lit-html",
|
||||
"storybook",
|
||||
"web-components"
|
||||
],
|
||||
"homepage": "https://github.com/storybookjs/storybook/tree/main/presets/web-components-webpack",
|
||||
"bugs": {
|
||||
"url": "https://github.com/storybookjs/storybook/issues"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/storybookjs/storybook.git",
|
||||
"directory": "presets/web-components-webpack"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/storybook"
|
||||
},
|
||||
"license": "MIT",
|
||||
"main": "dist/cjs/client/index.js",
|
||||
"module": "dist/esm/client/index.js",
|
||||
"types": "dist/ts3.9/client/index.d.ts",
|
||||
"typesVersions": {
|
||||
"<3.8": {
|
||||
"dist/ts3.9/*": [
|
||||
"dist/ts3.4/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist/**/*",
|
||||
"README.md",
|
||||
"*.js",
|
||||
"*.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"prepare": "node ../../scripts/prepare.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
||||
"@babel/plugin-syntax-import-meta": "^7.10.4",
|
||||
"@babel/preset-env": "^7.12.11",
|
||||
"@storybook/core-common": "6.5.0-alpha.64",
|
||||
"@types/node": "^14.14.20 || ^16.0.0",
|
||||
"@types/webpack-env": "^1.16.0",
|
||||
"babel-loader": "^7.0.0 || ^8.0.0",
|
||||
"babel-plugin-bundled-import-meta": "^0.3.1",
|
||||
"core-js": "^3.8.2",
|
||||
"react": "16.14.0",
|
||||
"react-dom": "16.14.0",
|
||||
"regenerator-runtime": "^0.13.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"lit-html": "2.0.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"lit-html": "^1.4.1 || ^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.13.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "7417a230d67b54d65caedcfb584f924b879ac9f5",
|
||||
"sbmodern": "dist/modern/client/index.js"
|
||||
}
|
1
presets/web-components-webpack/preset.js
Normal file
1
presets/web-components-webpack/preset.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = require('./dist/cjs/preset');
|
@ -1,7 +1,6 @@
|
||||
import type { Options } from '@storybook/core-common';
|
||||
import type { Configuration } from 'webpack';
|
||||
import type { StorybookConfig } from '@storybook/core-common';
|
||||
|
||||
export function webpack(config: Configuration, options: Options) {
|
||||
export const webpack: StorybookConfig['webpack'] = (config, options) => {
|
||||
const babelrcOptions = options.features?.babelModeV7 ? null : { babelrc: false };
|
||||
config.module.rules.push({
|
||||
test: [
|
||||
@ -37,4 +36,4 @@ export function webpack(config: Configuration, options: Options) {
|
||||
});
|
||||
|
||||
return config;
|
||||
}
|
||||
};
|
9
presets/web-components-webpack/tsconfig.json
Normal file
9
presets/web-components-webpack/tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "./src",
|
||||
"types": ["webpack-env", "node"]
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["src/**/*.test.*", "src/__tests__/**/*"]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user