将Nuxt模块放入Storybook

时间:2018-08-31 14:33:56

标签: webpack vuejs2 nuxt.js storybook

我试图用Nuxt项目中的组件制作一本故事书。到目前为止,我已经展示了组件,但是它们全部都利用了至少一个模块,而且我找不到导入它们的方法。我似乎并不孤单,
https://forum.vuejs.org/t/nuxt-js-and-storybook/26239
https://github.com/derekshull/nuxt-starter-kit-v2/issues/1),
但我希望有人找到了解决方案。    

.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 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但是很难找到解决方案,我终于找到了。

您的.storybook / webpack.config.js应该看起来像这样:

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;
}

以下资源对我有帮助:

https://vue-loader.vuejs.org/guide/css-modules.html#using-with-pre-processors(截至2019年11月,其中部分已过时)

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