Get Nuxt modules into Storybook

Viewed 1280

I try to make a storybook out of our components in a Nuxt project. So far I got the components showing up, but all of them utilize at least one module, and I can't find a way to import them. I don't seem to be alone with my struggle
(https://forum.vuejs.org/t/nuxt-js-and-storybook/26239,
https://github.com/derekshull/nuxt-starter-kit-v2/issues/1),
but I'm hoping someone has found a solution.

.storybook/webpack.config.js:

const path = require("@storybook\vue\dist\server\config\defaults\webpack.config.js");

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loaders: ["style-loader", "css-loader", "sass-loader"],
        include: path.resolve(__dirname, "../")
      }
    ]
  }
};

.storybook/config.js:

import { configure } from '@storybook/vue';
import Vue from 'vue';

const components = require.context('../components', true, /\.vue$/)
const stories = require.context('../components/stories', true, /.stories.js$/)

components.keys().forEach(filename => {
  const [, componentName] = filename.match(/([\w-]+)(.vue)/)
  Vue.component(componentName, components(filename).default)
})

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


configure(loadStories, module);
1 Answers

I had a the same issue but had a very hard time finding a solution and I finally found it.

Your .storybook/webpack.config.js should look something like this:

module.exports = ({ config }) => {
    config.module.rules.push({
        test: /\.scss$/,
        use: [
            'vue-style-loader',
            'sass-loader',
            {
                loader: 'css-loader',
                options: {
                    modules: true,
                    modules: {
                        localIdentName: '[hash:base64:6]'
                    },  
                },
            },
        ],
    });
    return config;
}

The following resources were helpful for me:

https://vue-loader.vuejs.org/guide/css-modules.html#using-with-pre-processors (as of Nov 2019 parts of this were outdated)

https://github.com/rails/webpacker/issues/2197

Related