Skip to content

Instantly share code, notes, and snippets.

@jpzwarte
Last active July 28, 2022 14:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpzwarte/0be6a491a3762f2ee2e784ab29669a2c to your computer and use it in GitHub Desktop.
Save jpzwarte/0be6a491a3762f2ee2e784ab29669a2c to your computer and use it in GitHub Desktop.

My stencil + storybook setup

  1. Setup your stencil project as usual
  2. Add storybook: npx -p @storybook/cli sb init --type react (we use react so you can use JSX inside your stories)
  3. Add required deps: npm add @babel/plugin-syntax-import-meta @open-wc/webpack-import-meta-loader webpack-merge -D
  4. preview.js:
import { defineCustomElements } from "../dist/esm/loader";

defineCustomElements();
  1. main.js
const merge = require('webpack-merge');

module.exports = {
  addons: [
  ],
  stories: ['../src/**/*.stories.mdx'],
  webpack: config => {
    return merge(config, {
      module: {
        rules: [
          {
            test: /\.js$/,
            loader: require.resolve('@open-wc/webpack-import-meta-loader')
          }
        ]
      }
    });
  }
};
  1. .babelrc.js
module.exports = {
  plugins: ['@babel/plugin-syntax-import-meta']
};
  1. Start stencil: stencil build --watch (MUST run in prod mode, NOT dev mode)

  2. Start storybook: npm run storybook

  3. Write your stories

import { Meta, Preview, Story } from "@storybook/addon-docs/blocks";

<Meta title="My Component" />

# My Component

Hello world

<Preview>
  <Story name="API">
    <my-component
      first="Jeroen"
      middle="Pieter"
      last="Zwartepoorte"
    ></my-component>
  </Story>
</Preview>
  1. You have storybook running with live reload when you save a stencil file.

NOTE: This setup works great as long as you don't try to pass complex objects to a web components. So this will not work:

<x-foo foo={{ foo: 'asdf', bar: 1234 }}></x-foo>

If you want to do this from an MDX story, you have to use @storybook/web-components and lit-html.

@break-stuff
Copy link

This is great! Thank you for documenting this!

@wjureczka
Copy link

This example is no longer working :( Do you have any repo with example?

@Giorgiosaud
Copy link

Giorgiosaud commented Jul 28, 2022

replace

const merge = require('webpack-merge');

with

const {merge} = require('webpack-merge');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment