Skip to content

Commit

Permalink
feat: adds new app "web-components"
Browse files Browse the repository at this point in the history
  • Loading branch information
daKmoR committed Oct 18, 2019
1 parent fd7193a commit 4dc15ec
Show file tree
Hide file tree
Showing 39 changed files with 1,109 additions and 0 deletions.
10 changes: 10 additions & 0 deletions addons/docs/src/frameworks/web-components/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable import/no-extraneous-dependencies */
import { addParameters } from '@storybook/web-components';
import { DocsPage, DocsContainer } from '@storybook/addon-docs/blocks';

addParameters({
docs: {
container: DocsContainer,
page: DocsPage,
},
});
1 change: 1 addition & 0 deletions addons/docs/web-components/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../dist/frameworks/web-components/config');
1 change: 1 addition & 0 deletions addons/docs/web-components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../dist/frameworks/common/index');
1 change: 1 addition & 0 deletions addons/docs/web-components/preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../dist/frameworks/common/makePreset').default('web-components');
91 changes: 91 additions & 0 deletions app/web-components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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 Screenshot](https://github.com/storybookjs/storybook/blob/master/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 -p @storybook/cli 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/addons/introduction) and a great API to customize as you wish.
You can also build a [static version](https://storybook.js.org/basics/exporting-storybook) of your storybook and deploy it anywhere you want.

# Setup page reload via 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.
To activate full page reload

```js
// ==> REPLACE
configure(require.context('../stories', true, /\.stories\.(js|mdx)$/), module);

// ==> WITH
// force full reload to not reregister web components
const req = require.context('../stories', true, /\.stories\.(js|mdx)$/);
configure(req, module);
if (module.hot) {
module.hot.accept(req.id, () => {
const currentLocationHref = window.location.href;
window.history.pushState(null, null, currentLocationHref);
window.location.reload();
});
}
```

# 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 es7 you will need to manually mark those packages as "needs transpilation".

For example if you have a library called `my-library` which is in es7 then you can add it like so

```js
// .storybook/webpack.config.js

module.exports = ({ 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>
4 changes: 4 additions & 0 deletions app/web-components/bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

process.env.NODE_ENV = process.env.NODE_ENV || 'production';
require('../dist/server/build');
3 changes: 3 additions & 0 deletions app/web-components/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/server');
62 changes: 62 additions & 0 deletions app/web-components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@storybook/web-components",
"version": "5.3.0-alpha.17",
"description": "Storybook for web-components: View web components snippets in isolation with Hot Reloading.",
"keywords": [
"storybook",
"web-components",
"lit-html"
],
"homepage": "https://github.com/storybookjs/storybook/tree/master/app/web-components",
"bugs": {
"url": "https://github.com/storybookjs/storybook/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/storybookjs/storybook.git",
"directory": "app/web-components"
},
"license": "MIT",
"files": [
"bin/**/*",
"dist/**/*",
"README.md",
"*.js",
"*.d.ts"
],
"main": "dist/client/index.js",
"types": "dist/client/index.d.ts",
"bin": {
"build-storybook": "./bin/build.js",
"start-storybook": "./bin/index.js",
"storybook-server": "./bin/index.js"
},
"scripts": {
"prepare": "node ../../scripts/prepare.js"
},
"dependencies": {
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-syntax-import-meta": "^7.2.0",
"@storybook/addons": "5.3.0-alpha.17",
"@storybook/core": "5.3.0-alpha.17",
"@types/webpack-env": "^1.13.9",
"babel-plugin-bundled-import-meta": "^0.3.1",
"core-js": "^3.0.1",
"global": "^4.3.2",
"regenerator-runtime": "^0.13.3",
"ts-dedent": "^1.1.0"
},
"devDependencies": {
"lit-html": "^1.0.0"
},
"peerDependencies": {
"babel-loader": "^7.0.0 || ^8.0.0",
"lit-html": "^1.0.0"
},
"engines": {
"node": ">=8.0.0"
},
"publishConfig": {
"access": "public"
}
}
16 changes: 16 additions & 0 deletions app/web-components/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export {
storiesOf,
setAddon,
addDecorator,
addParameters,
configure,
getStorybook,
forceReRender,
raw,
} from './preview';

if (module && module.hot && module.hot.decline) {
module.hot.decline();
}

// TODO: disable HMR and do full page loads because of customElements.define
3 changes: 3 additions & 0 deletions app/web-components/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { window } from 'global';

window.STORYBOOK_ENV = 'web-components';
35 changes: 35 additions & 0 deletions app/web-components/src/client/preview/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable prefer-destructuring */
import { start } from '@storybook/core/client';
import { ClientStoryApi, Loadable } from '@storybook/addons';

import './globals';
import render from './render';
import { StoryFnHtmlReturnType, IStorybookSection } from './types';

const framework = 'html';

interface ClientApi extends ClientStoryApi<StoryFnHtmlReturnType> {
setAddon(addon: any): void;
configure(loader: Loadable, module: NodeModule): void;
getStorybook(): IStorybookSection[];
clearDecorators(): void;
forceReRender(): void;
raw: () => any; // todo add type
}

const api = start(render);

export const storiesOf: ClientApi['storiesOf'] = (kind, m) => {
return (api.clientApi.storiesOf(kind, m) as ReturnType<ClientApi['storiesOf']>).addParameters({
framework,
});
};

export const configure: ClientApi['configure'] = (...args) => api.configure(...args, framework);
export const addDecorator: ClientApi['addDecorator'] = api.clientApi.addDecorator;
export const addParameters: ClientApi['addParameters'] = api.clientApi.addParameters;
export const clearDecorators: ClientApi['clearDecorators'] = api.clientApi.clearDecorators;
export const setAddon: ClientApi['setAddon'] = api.clientApi.setAddon;
export const forceReRender: ClientApi['forceReRender'] = api.forceReRender;
export const getStorybook: ClientApi['getStorybook'] = api.clientApi.getStorybook;
export const raw: ClientApi['raw'] = api.clientApi.raw;
48 changes: 48 additions & 0 deletions app/web-components/src/client/preview/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { document, Node } from 'global';
import dedent from 'ts-dedent';
import { render, TemplateResult } from 'lit-html';
import { RenderMainArgs } from './types';

const rootElement = document.getElementById('root');

export default function renderMain({
storyFn,
selectedKind,
selectedStory,
showMain,
showError,
forceRender,
}: RenderMainArgs) {
const element = storyFn();

showMain();
if (element instanceof TemplateResult) {
// `render` stores the TemplateInstance in the Node and tries to update based on that.
// Since we reuse `rootElement` for all stories, remove the stored instance first.
// But forceRender means that it's the same story, so we want too keep the state in that case.
if (!forceRender || !rootElement.querySelector('[id="root-inner"]')) {
rootElement.innerHTML = '<div id="root-inner"></div>';
}
const renderTo = rootElement.querySelector('[id="root-inner"]');

render(element, renderTo);
} else if (typeof element === 'string') {
rootElement.innerHTML = element;
} else if (element instanceof Node) {
// Don't re-mount the element if it didn't change and neither did the story
if (rootElement.firstChild === element && forceRender === true) {
return;
}

rootElement.innerHTML = '';
rootElement.appendChild(element);
} else {
showError({
title: `Expecting an HTML snippet or DOM node from the story: "${selectedStory}" of "${selectedKind}".`,
description: dedent`
Did you forget to return the HTML snippet from the story?
Use "() => <your snippet or node>" or when defining the story.
`,
});
}
}
27 changes: 27 additions & 0 deletions app/web-components/src/client/preview/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { StoryFn } from '@storybook/addons';

export type StoryFnHtmlReturnType = string | Node;

export interface IStorybookStory {
name: string;
render: () => any;
}

export interface IStorybookSection {
kind: string;
stories: IStorybookStory[];
}

export interface ShowErrorArgs {
title: string;
description: string;
}

export interface RenderMainArgs {
storyFn: () => StoryFn<StoryFnHtmlReturnType>;
selectedKind: string;
selectedStory: string;
showMain: () => void;
showError: (args: ShowErrorArgs) => void;
forceRender: boolean;
}
4 changes: 4 additions & 0 deletions app/web-components/src/server/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { buildStatic } from '@storybook/core/server';
import options from './options';

buildStatic(options);
46 changes: 46 additions & 0 deletions app/web-components/src/server/framework-preset-web-components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { Configuration } from 'webpack';

export function webpack(config: Configuration) {
return {
...config,
module: {
...config.module,
rules: [
...config.module.rules,
{
test: [
new RegExp(`src(.*)\\.js$`),
new RegExp(`packages(\\/|\\\\)*(\\/|\\\\)src(\\/|\\\\)(.*)\\.js$`),
new RegExp(`node_modules(\\/|\\\\)lit-html(.*)\\.js$`),
new RegExp(`node_modules(\\/|\\\\)lit-element(.*)\\.js$`),
new RegExp(`node_modules(\\/|\\\\)@open-wc(.*)\\.js$`),
new RegExp(`node_modules(\\/|\\\\)@polymer(.*)\\.js$`),
new RegExp(`node_modules(\\/|\\\\)@vaadin(.*)\\.js$`),
],
use: {
loader: 'babel-loader',
options: {
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-meta',
// webpack does not support import.meta.url yet, so we rewrite them in babel
['bundled-import-meta', { importStyle: 'baseURI' }],
],
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
corejs: 3,
},
],
],
babelrc: false,
},
},
},
],
},
};
}
4 changes: 4 additions & 0 deletions app/web-components/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { buildDev } from '@storybook/core/server';
import options from './options';

buildDev(options);
7 changes: 7 additions & 0 deletions app/web-components/src/server/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// tslint:disable-next-line: no-var-requires
const packageJson = require('../../package.json');

export default {
packageJson,
frameworkPresets: [require.resolve('./framework-preset-web-components.js')],
};
5 changes: 5 additions & 0 deletions app/web-components/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module '@storybook/core/*';
declare module 'global';

// will be provided by the webpack define plugin
declare var NODE_ENV: string | undefined;
8 changes: 8 additions & 0 deletions app/web-components/standalone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const build = require('@storybook/core/standalone');
const frameworkOptions = require('./dist/server/options').default;

async function buildStandalone(options) {
return build(options, frameworkOptions);
}

module.exports = buildStandalone;
Loading

0 comments on commit 4dc15ec

Please sign in to comment.