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

Vite: Add web-components/lit support #19315

Merged
merged 19 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion code/frameworks/vue3-vite/preset.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
console.log('vue3-vite preset!');
module.exports = require('./dist/preset');
1 change: 1 addition & 0 deletions code/frameworks/web-components-vite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Storybook for Web components
86 changes: 86 additions & 0 deletions code/frameworks/web-components-vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"name": "@storybook/web-components-vite",
"version": "7.0.0-alpha.34",
"description": "Storybook for web-components and Vite: Develop Web Components in isolation with Hot Reloading.",
"keywords": [
"storybook"
],
"homepage": "https://github.com/storybookjs/storybook/tree/main/frameworks/web-components-vite",
"bugs": {
"url": "https://github.com/storybookjs/storybook/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/storybookjs/storybook.git",
"directory": "frameworks/web-components-vite"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/storybook"
},
"license": "MIT",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./preset": {
"require": "./dist/preset.js",
"import": "./dist/preset.mjs",
"types": "./dist/preset.d.ts"
},
"./package.json": {
"require": "./package.json",
"import": "./package.json",
"types": "./package.json"
}
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist/**/*",
"types/**/*",
"README.md",
"*.js",
"*.d.ts"
],
"scripts": {
"check": "tsc --noEmit",
"prep": "../../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@storybook/addons": "7.0.0-alpha.34",
"@storybook/builder-vite": "7.0.0-alpha.34",
"@storybook/channel-postmessage": "7.0.0-alpha.34",
"@storybook/channel-websocket": "7.0.0-alpha.34",
"@storybook/client-api": "7.0.0-alpha.34",
"@storybook/core-server": "7.0.0-alpha.34",
"@storybook/node-logger": "7.0.0-alpha.34",
"@storybook/preview-web": "7.0.0-alpha.34",
"@storybook/web-components": "7.0.0-alpha.34",
"@vitejs/plugin-vue": "^3.0.3",
"magic-string": "^0.26.1",
"vite": "3"
},
"devDependencies": {
"@types/node": "^14.14.20 || ^16.0.0",
"typescript": "~4.6.3",
"vite": "^3.1.0"
},
"engines": {
"node": "^14.18 || >=16"
},
"publishConfig": {
"access": "public"
},
"bundler": {
"entries": [
"./src/index.ts",
"./src/preset.ts"
],
"platform": "node"
},
"gitHead": "02c013c33186479017098d532a18ff8654b91f1f"
}
1 change: 1 addition & 0 deletions code/frameworks/web-components-vite/preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/preset');
9 changes: 9 additions & 0 deletions code/frameworks/web-components-vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// exports for builder-vite
export { createChannel as createPostMessageChannel } from '@storybook/channel-postmessage';
export { createChannel as createWebSocketChannel } from '@storybook/channel-websocket';
export { addons } from '@storybook/addons';
export { composeConfigs, PreviewWeb } from '@storybook/preview-web';
export { ClientApi } from '@storybook/client-api';

export * from '@storybook/web-components';
export type { StorybookConfig } from '@storybook/builder-vite';
7 changes: 7 additions & 0 deletions code/frameworks/web-components-vite/src/preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { StorybookConfig } from '@storybook/builder-vite';

export const addons: StorybookConfig['addons'] = ['@storybook/web-components'];

export const core: StorybookConfig['core'] = {
builder: '@storybook/builder-vite',
};
10 changes: 10 additions & 0 deletions code/frameworks/web-components-vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"types": ["node"],
"resolveJsonModule": true
},
"include": ["src/**/*"],
"exclude": ["src/**/*.test.*", "src/**/__testfixtures__/**"]
}
2 changes: 1 addition & 1 deletion code/lib/cli/src/automigrate/fixes/new-frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const packagesMap: Record<string, { webpack5?: string; vite?: string }> = {
},
'@storybook/svelte': {
webpack5: '@storybook/svelte-webpack5',
// TODO: bring this back once we have the svelte-vite package
vite: '@storybook/svelte-vite',
},
'@storybook/web-components': {
webpack5: '@storybook/web-components-webpack5',
vite: '@storybook/web-components-vite',
},
'@storybook/html': {
webpack5: '@storybook/html-webpack5',
Expand Down
24 changes: 24 additions & 0 deletions code/lib/cli/src/repro-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ const svelteViteTemplates = {
// }
};

const litViteTemplates = {
'lit-vite/default-js': {
name: 'Lit Vite (JS)',
script: 'yarn create vite . --template lit',
cadence: [] as any,
expected: {
framework: '@storybook/web-components-vite',
renderer: '@storybook/web-components',
builder: '@storybook/builder-vite',
},
},
'lit-vite/default-ts': {
name: 'Lit Vite (TS)',
script: 'yarn create vite . --template lit-ts',
cadence: [] as any,
expected: {
framework: '@storybook/web-components-vite',
renderer: '@storybook/web-components',
builder: '@storybook/builder-vite',
},
},
};

const vueCliTemplates = {
'vue-cli/default-js': {
name: 'Vue-CLI (Default JS)',
Expand Down Expand Up @@ -124,6 +147,7 @@ export default {
...reactViteTemplates,
...vue3ViteTemplates,
...svelteViteTemplates,
...litViteTemplates,
...vueCliTemplates,
// FIXME: missing documentation.json
// 'angular/latest': {
Expand Down
1 change: 1 addition & 0 deletions code/lib/cli/src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default {
'@storybook/vue3-webpack5': '7.0.0-alpha.34',
'@storybook/web-components': '7.0.0-alpha.34',
'@storybook/web-components-webpack5': '7.0.0-alpha.34',
'@storybook/web-components-vite': '7.0.0-alpha.34',
sb: '7.0.0-alpha.34',
storybook: '7.0.0-alpha.34',
};
1 change: 1 addition & 0 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
"@storybook/vue3-vite": "workspace:*",
"@storybook/vue3-webpack5": "workspace:*",
"@storybook/web-components": "workspace:*",
"@storybook/web-components-vite": "workspace:*",
"@storybook/web-components-webpack5": "workspace:*",
"@testing-library/dom": "^7.29.4",
"@testing-library/jest-dom": "^5.11.9",
Expand Down
2 changes: 1 addition & 1 deletion code/renderers/web-components/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { parameters as docsParams } from './docs/config';

export const parameters = { framework: 'web-components' as const, ...docsParams };
export { decorators, argTypesEnhancers } from './docs/config';
export { renderToDOM } from './render';
export { render, renderToDOM } from './render';
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable import/extensions */
import { html } from 'lit-html';
import { styleMap } from 'lit-html/directives/style-map';
import { styleMap } from 'lit-html/directives/style-map.js';
import { addons, useEffect } from '@storybook/addons';
import { SNIPPET_RENDERED } from '@storybook/docs-tools';
import type { StoryContext } from '../types';
Expand Down
21 changes: 19 additions & 2 deletions code/renderers/web-components/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@
import global from 'global';

import { dedent } from 'ts-dedent';
import { render } from 'lit-html';
import { render as litRender, html } from 'lit-html';
// Keep `.js` extension to avoid issue with Webpack (related to export map?)
// eslint-disable-next-line import/extensions
import { isTemplateResult } from 'lit-html/directive-helpers.js';
import { simulatePageLoad, simulateDOMContentLoaded } from '@storybook/preview-web';
import type { RenderContext } from '@storybook/store';
import { ArgsStoryFn } from '@storybook/csf';
import { WebComponentsFramework } from './types';

const { Node } = global;

export const render: ArgsStoryFn<WebComponentsFramework> = (args, context) => {
const { id, component } = context;
if (!component) {
throw new Error(
`Unable to render story ${id} as the component annotation is missing from the default export`
);
}

// FIXME: replace by one of
// - https://github.com/lit/lit/pull/1960
// - https://github.com/open-wc/open-wc/pull/2398
const argProps = Object.entries(args).map(([key, val]) => `.${key}=${val}`);

return html`<${component} ${argProps.join(' ')}></${component}>`;
};

export function renderToDOM(
{ storyFn, kind, name, showMain, showError, forceRemount }: RenderContext<WebComponentsFramework>,
domElement: Element
Expand All @@ -29,7 +46,7 @@ export function renderToDOM(
}
const renderTo = domElement.querySelector<HTMLElement>('[id="root-inner"]');

render(element, renderTo);
litRender(element, renderTo);
simulatePageLoad(domElement);
} else if (typeof element === 'string') {
domElement.innerHTML = element;
Expand Down
22 changes: 22 additions & 0 deletions code/renderers/web-components/template/components/Button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable import/extensions */
import { html } from 'lit-html';
import { styleMap } from 'lit-html/directives/style-map.js';
import './button.css';

/**
* Primary UI component for user interaction
*/
export const Button = ({ primary, backgroundColor = null, size, label, onClick }) => {
const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';

return html`
<button
type="button"
class=${['storybook-button', `storybook-button--${size || 'medium'}`, mode].join(' ')}
style=${styleMap({ backgroundColor })}
@click=${onClick}
>
${label}
</button>
`;
};
14 changes: 14 additions & 0 deletions code/renderers/web-components/template/components/Pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable import/extensions */
import { html } from 'lit-html';
import { styleMap } from 'lit-html/directives/style-map.js';

/**
* Helper component for rendering text or data
*/
export const Pre = ({ style, object, text }) =>
html`
<pre data-testid="pre" style=${styleMap({ style })}>
${object ? JSON.stringify(object, null, 2) : text}
</pre
>
`;
30 changes: 30 additions & 0 deletions code/renderers/web-components/template/components/button.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.storybook-button {
font-family: 'Nunito Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-weight: 700;
border: 0;
border-radius: 3em;
cursor: pointer;
display: inline-block;
line-height: 1;
}
.storybook-button--primary {
color: white;
background-color: #1ea7fd;
}
.storybook-button--secondary {
color: #333;
background-color: transparent;
box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 0px 1px inset;
}
.storybook-button--small {
font-size: 12px;
padding: 10px 16px;
}
.storybook-button--medium {
font-size: 14px;
padding: 11px 20px;
}
.storybook-button--large {
font-size: 16px;
padding: 12px 24px;
}
6 changes: 6 additions & 0 deletions code/renderers/web-components/template/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import globalThis from 'global';

import { Button } from './Button';
import { Pre } from './Pre';

globalThis.Components = { Button, Pre };
1 change: 1 addition & 0 deletions code/renderers/web-components/template/stories/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Placeholder until we write some render-specific stories
5 changes: 5 additions & 0 deletions code/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@
"type": "library",
"implicitDependencies": []
},
"@storybook/web-components-vite": {
"root": "frameworks/web-components-vite",
"type": "library",
"implicitDependencies": []
},
"@storybook/web-components-webpack5": {
"root": "frameworks/web-components-webpack5",
"type": "library",
Expand Down
22 changes: 22 additions & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8667,6 +8667,7 @@ __metadata:
"@storybook/vue3-vite": "workspace:*"
"@storybook/vue3-webpack5": "workspace:*"
"@storybook/web-components": "workspace:*"
"@storybook/web-components-vite": "workspace:*"
"@storybook/web-components-webpack5": "workspace:*"
"@testing-library/dom": ^7.29.4
"@testing-library/jest-dom": ^5.11.9
Expand Down Expand Up @@ -9233,6 +9234,27 @@ __metadata:
languageName: unknown
linkType: soft

"@storybook/web-components-vite@workspace:*, @storybook/web-components-vite@workspace:frameworks/web-components-vite":
version: 0.0.0-use.local
resolution: "@storybook/web-components-vite@workspace:frameworks/web-components-vite"
dependencies:
"@storybook/addons": 7.0.0-alpha.34
"@storybook/builder-vite": 7.0.0-alpha.34
"@storybook/channel-postmessage": 7.0.0-alpha.34
"@storybook/channel-websocket": 7.0.0-alpha.34
"@storybook/client-api": 7.0.0-alpha.34
"@storybook/core-server": 7.0.0-alpha.34
"@storybook/node-logger": 7.0.0-alpha.34
"@storybook/preview-web": 7.0.0-alpha.34
"@storybook/web-components": 7.0.0-alpha.34
"@types/node": ^14.14.20 || ^16.0.0
"@vitejs/plugin-vue": ^3.0.3
magic-string: ^0.26.1
typescript: ~4.6.3
vite: ^3.1.0
languageName: unknown
linkType: soft

"@storybook/web-components-webpack5@workspace:*, @storybook/web-components-webpack5@workspace:frameworks/web-components-webpack5":
version: 0.0.0-use.local
resolution: "@storybook/web-components-webpack5@workspace:frameworks/web-components-webpack5"
Expand Down
4 changes: 1 addition & 3 deletions scripts/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,7 @@ async function main() {

if (require.main === module) {
main().catch((err) => {
logger.error('🚨 An error occurred when executing "sandbox":');

logger.error(err);
logger.error(err.message);
process.exit(1);
});
}
Loading