question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to install storybook with vue + type script

See original GitHub issue

Hello, I’m trying to install storybook with vue+typescript

I have used this article and comments as a manual.

After all of this I have an error in dev tools console: [Vue warn]: Cannot find element: #root And page automatically redirects to http://localhost:6006/?selectedKind=Button&selectedStory=with text

screen shot 2019-01-18 at 11 40 45 am
webpack.config.js :
const merge = require('webpack-merge')

const genStorybookDefaultConfig = require('@storybook/vue/dist/server/framework-preset-vue.js')
  .webpack
const vueConfig = require('@vue/cli-service/webpack.config.js')
module.exports = (storybookBaseConfig, configType) => {
  const storybookConfig = genStorybookDefaultConfig(
    storybookBaseConfig,
    configType
  )
  return {
    ...vueConfig, // use vue's webpack configuration by default
    entry: storybookConfig.entry, // overwite entry
    output: storybookConfig.output, // overwrite output
    // remove duplicated plugins
    plugins: merge({
      customizeArray: merge.unique(
        'plugins',
        [
          'HotModuleReplacementPlugin',
          'CaseSensitivePathsPlugin',
          'WatchMissingNodeModulesPlugin',
          'VueLoaderPlugin'
        ],
        plugin => plugin.constructor && plugin.constructor.name
      )
    })(vueConfig, storybookConfig).plugins,
    resolve: {
      ...vueConfig.resolve,
      alias: {
        ...vueConfig.resolve.alias,
        vue$: storybookConfig.resolve.alias.vue$
      }
    }
  }
}

config.js:

import { configure } from '@storybook/vue'

// automatically import all files ending in *.stories.ts

const req = require.context('../src/stories', true, /.stories.ts$/)

function loadStories() {
    req.keys().forEach(filename => req(filename))
}

configure(loadStories, module)

index.stories.ts :

/* eslint-disable react/react-in-jsx-scope, react/no-this-in-sfc */

import { storiesOf } from '@storybook/vue'

import { action } from '@storybook/addon-actions'

// import { linkTo } from '@storybook/addon-links'

import MyButton from './MyButton.vue'

storiesOf('Button', module)
  .add('with text', () => ({
    components: { MyButton },
    template: <my-button @click="action">Hello Button</my-button>
    methods: { action: action('clicked') }
}))
 .add('with some emoji', () => ({
   components: { MyButton },
   template: '<my-button @click="action">😀 😎 👍 💯</my-button>',
   methods: { action: action('clicked') }
  }))
    /* eslint-enable react/react-in-jsx-scope */

Repo Any thoughts ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

8reactions
mateuszgachowskicommented, May 28, 2019

If someone still has problems with it this should help 😃

const vueConfig = require('@vue/cli-service/webpack.config.js');

module.exports = async ({ config }) => {
  return {
    ...config,
    resolve: {
      ...vueConfig.resolve,
      alias: {
        ...vueConfig.resolve.alias,
        'vue$': 'vue/dist/vue.esm.js' // if you need it
      },
    },
    module: {
      ...vueConfig.module,
      rules: vueConfig.module.rules,
    },
  }
}

2reactions
mateuszgachowskicommented, Jun 6, 2019

Thanks for trying the config @justinhodev It depends what kind of components you are compiling - if you use render functions it will work without this alias.

// Here you do not have to use esm
import SomeComponent from 'SomeComponent.vue';
new Vue({
  render: (h) => h(SomeComponent)
})

vs

// Here you need to use ESM alias
import SomeComponent from 'SomeComponent.vue';
new Vue({
  components: { SomeComponent }
  template: '<SomeComponent />',
})

See: https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only

Probably you are using the template option, that is why esm build is required.

Hope it explains why it didn’t work without it 😃

Cheers, Mateusz

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Storybook for Vue with TypeScript - Damir's Corner
Configuring Storybook for Vue with TypeScript ... Storybook is a great tool for component development. But although it supports many frameworks, ...
Read more >
Getting started with Vue (2.x), Storybook and Typescript
In this article, we will take a look at getting started with Storybook and VueJS (2.x) and in particular, using Typescript, class-style ...
Read more >
Building #Vue components in #TypeScript using #Storybook
Welcome to The SingleStore #Developer Live Stream. In this episode, we use # storybook as a harness to quickly develop and flex a...
Read more >
How to install Storybook to VueJS 2 TypeScript project?
npx sb init. according to the latest documentation - https://storybook.js.org/docs/vue/get-started/install.
Read more >
Storybook for Vue 3 - JS.ORG
Storybook is an open source tool for building UI components. It speeds up UI development, generates documentation, and automates testing. This ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found