在不同 webpack 版本的 Vue 项目中配置 Storybook

简介: 在不同 webpack 版本的 Vue 项目中配置 Storybook

在之前的一篇文章中,介绍过组件化搭建工具 storybook 在 vue 项目中的安装和配置。 相比于其成文的时间,vue 项目依赖的工具多有发展;并且在实际应用中,多种历史版本的项目并存的状况比比皆是,用官方提供的 npx sb init 往往会出现配置失败的情况,而较新或过旧的资料都在网上难觅 --

所以在此特别补充一篇,记录 新、旧 两种典型配置下,storybook 可用的手动配置方法:

1. babel7 + webpack5

1.1 安装过程


diff --git a/.babelrc b/.babelrc
index e1f4817..71b5fe8 100644
--- a/.babelrc
+++ b/.babelrc
@@ -6,7 +6,8 @@
         "modules": false,
         "targets": "last 2 years, ie > 8"
       }
-    ]
+    ],
+    "@vue/babel-preset-jsx"
   ],
   "plugins": [
     "@babel/plugin-transform-runtime",
diff --git a/.eslintignore b/.eslintignore
index e2192c5..0858135 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -2,4 +2,5 @@
+/test/*
+*.spec.js
diff --git a/.storybook/demo.css b/.storybook/demo.css
new file mode 100644
index 0000000..820f051
--- /dev/null
+++ b/.storybook/demo.css
@@ -0,0 +1,12 @@
+.demo {
+    padding: 50px;
+    display: inline-block;
+}
+.demo .block {
+    margin-bottom: 30px;
+}
diff --git a/.storybook/main.js b/.storybook/main.js
new file mode 100644
index 0000000..ceca416
--- /dev/null
+++ b/.storybook/main.js
@@ -0,0 +1,22 @@
+const path = require("path");
+const pathResolve = p => path.resolve(__dirname, "../", p);
+
+module.exports = {
+  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
+  addons: ["@storybook/addon-links", "@storybook/addon-essentials"],
+  core: {
+    builder: "webpack5"
+  },
+  webpackFinal: config => {
+    config.resolve.alias = {
+      ...config.resolve.alias,
+      "@": pathResolve("src"),
+      "~": pathResolve("node_modules")
+    };
+    config.module.rules.push({
+      test: /\.scss$/,
+      use: ["vue-style-loader", "css-loader", "sass-loader"]
+    });
+    return config;
+  }
+};
diff --git a/.storybook/manager-head.html b/.storybook/manager-head.html
new file mode 100644
index 0000000..c5c2955
--- /dev/null
+++ b/.storybook/manager-head.html
@@ -0,0 +1,14 @@
+<style>
+  .sidebar-header {
+    text-align: center;
+  }
+  .sidebar-header > div > a {
+    font-size: 28px;
+  }
+</style>
diff --git a/.storybook/manager.js b/.storybook/manager.js
new file mode 100644
index 0000000..c2321a4
--- /dev/null
+++ b/.storybook/manager.js
@@ -0,0 +1,10 @@
+import { addons } from "@storybook/addons";
+import { create } from "@storybook/theming";
+
+addons.setConfig({
+  theme: create({
+    base: "light",
+    brandImage: null,
+    brandTitle: "前端组件"
+  })
+});
diff --git a/.storybook/preview.js b/.storybook/preview.js
new file mode 100644
index 0000000..55d75d7
--- /dev/null
+++ b/.storybook/preview.js
@@ -0,0 +1,23 @@
+import "./demo.css";
+
+export const parameters = {
+    actions: { argTypesRegex: "^on[A-Z].*" },
+    controls: {
+        matchers: {
+            color: /(background|color)$/i,
+            date: /Date$/
+        }
+    },
+    // https://github.com/storybookjs/storybook/pull/9090/commits/2db3faa727fed585fb5d9e6db23cc3835fc88829
+    viewMode: "docs"
+};
diff --git a/package.json b/package.json
index 673f3a9..c0b7b27 100644
--- a/package.json
+++ b/package.json
@@ -1,19 +1,23 @@
+    "storybook": "start-storybook -p 6006 --no-manager-cache",
+    "build-storybook": "build-storybook"
   },
   "dependencies": {
+    "@babel/plugin-syntax-jsx": "^7.12.13",
+    "@storybook/addon-actions": "^6.2.7",
+    "@storybook/addon-essentials": "^6.2.7",
+    "@storybook/addon-links": "^6.2.7",
+    "@storybook/builder-webpack5": "^6.2.7",
+    "@storybook/theming": "^6.2.8",
+    "@storybook/vue": "^6.2.7",
+    "@vue/babel-helper-vue-jsx-merge-props": "^1.2.1",
+    "@vue/babel-preset-jsx": "^1.2.4",
     "babel-plugin-transform-vue-jsx": "~3.7.0",
diff --git a/src/utils/utils.js b/src/utils/utils.js
new file mode 100644
index 0000000..3e8413d
--- /dev/null
+++ b/src/utils/utils.js
@@ -0,0 +1,103 @@
+export const formatCode = jsCode =>
+    JSON.stringify(
+        jsCode,
+        // eslint-disable-next-line
+    (key, value) => {
+            if (typeof value === 'function') {
+                return value.toString();
+            } else {
+                return value;
+            }
+        },
+        2
+    ).replace(/\\n/g, '\n');
diff --git a/src/utils/storyUtils.js b/src/utils/storyUtils.js
new file mode 100644
index 0000000..4ad5149
--- /dev/null
+++ b/src/utils/storyUtils.js
@@ -0,0 +1,49 @@
+import {formatCode} from './utils';
+
+/**
+ * 生成 storybook 用例辅助函数
+ * @param {String} path - 组件的层级式路径,由 `/` 分割
+ * @param {Object} storyComponent - 用于展示的用例
+ * @param {String} markdown - 文档
+ * @param {Object} [originComponent] - 可选,如果需要从原始组件内部的 jsdoc 自动生成 API,则传入原始组件
+ * @return {Object} story - {metadata, named}
+ * @see https://github.com/storybookjs/storybook/issues/8527
+ */
+export const storyOf = (path, storyComponent, markdown, originComponent) => {
+    const pathArr = path.split('/').map(p => {
+    // https://storybook.js.org/docs/vue/writing-stories/args#setting-args-through-the-url
+        if (!/^[0-9a-zA-Z]/.test(p)) {
+            return '#' + p;
+        }
+        return p;
+    });
+    const storyName = pathArr[pathArr.length - 1];
+
+    const metadata = {
+        component: originComponent || void 0,
+        title: pathArr.join('/'),
+        decorators: [
+            () => ({template: '<div class="demo"><story></story></div>'})
+        ]
+    };
+
+    const named = () => storyComponent;
+    named.storyName = storyName;
+    named.parameters = {
+        docs: {
+            description: {
+                component: markdown || '&nbsp;'
+            },
+            source: {
+                // type: 'dynamic'
+                type: 'code',
+                code: formatCode(storyComponent)
+            }
+        }
+    };
+
+    return {
+        metadata,
+        named
+    };
+};

1.2 组件和用例

用例1:

mdx

代码解读

复制代码

import { Meta, Preview, Description } from '@storybook/addon-docs/blocks'
import readme from './README.md'
import pathPrefix from './pathPrefix.js'
<Meta title={pathPrefix + "/API"} />
<Description markdown={readme} />

组件2:


<template functional>
    <x-table
        v-bind="{ ...data.attrs, ...props }"
        v-on="listeners"
    >
       <!-- ... --> 
    </x-table>
</template>
<script>
export default {
    props: {
    /**
     * 配置中增加了 __自定义 component__ 的能力,避免了在 template 中再分别写 slot;
     * 自定义组件对象会默认接收 `row` 和 `column-config` 两个属性
     */
        columns: {
            type: Array,
            default: () => []
        }
    },
    emits: [
    /**
     * 自定义组件中如果发出同名事件,会被 table 的容器监听到;
     * 参数为 `key, ...values`
     */
        'column-action'
    ]
};
</script>

用例2:


import {storyOf} from '@/utils/storyUtils';
import Comp from '@/components/SimpleTable';
const title = 'Example/SimpleTable|全配置化表格';
const markdown = `
  > 基于 \`x-table\` 的扩展,可以接收其原有属性和事件
`;
const comp = {
    template: `
        <x-simple-table
            :width="600"
            :columns="columns"
            :data="list"
            @column-action="onColAction"
        />
    `,
    data() {
        return {
            //...
        };
    },
    methods: {
        onColAction(act, value) {
            alert([act, value]);
        }
    }
};
const {metadata, named} = storyOf(title, comp, markdown, Comp);
export default metadata;
export const story = named;

2. babel6 + webpack4

2.1 安装过程


diff --git a/.storybook/addons.js b/.storybook/addons.js
new file mode 100644
index 0000000..7106272
--- /dev/null
+++ b/.storybook/addons.js
@@ -0,0 +1,4 @@
+import 'storybook-addon-vue-info/lib/register';
+import '@storybook/addon-backgrounds/register';
+import '@storybook/addon-viewport/register';
+import '@storybook/addon-storysource/register';
diff --git a/.storybook/config.js b/.storybook/config.js
new file mode 100644
index 0000000..f84ef78
--- /dev/null
+++ b/.storybook/config.js
@@ -0,0 +1,34 @@
+import {configure, addDecorator, addParameters} from '@storybook/vue';
+import {create} from '@storybook/theming';
+import {withInfo, setDefaults} from 'storybook-addon-vue-info';
+import './demo.css';
+
+setDefaults({
+    header: false,
+    useDocgen: false
+});
+addDecorator(withInfo);
+addParameters({
+    options: {
+        theme: create({
+            base: 'light',
+            brandImage: null,
+            brandTitle: '前端组件'
+        })
+    }
+});
+const req = require.context('../src', true, /\.stories\.js$/);
+function loadStories() {
+    req.keys().forEach(filename => {
+        try {
+            req(filename);
+        } catch (ex) {
+            console.log('storybook-req', ex);
+        }
+    });
+}
+configure(loadStories, module);
diff --git a/.storybook/manager-head.html b/.storybook/manager-head.html
new file mode 100644
index 0000000..c5c2955
--- /dev/null
+++ b/.storybook/manager-head.html
@@ -0,0 +1,14 @@
+<style>
+  .sidebar-header {
+    text-align: center;
+  }
+  .sidebar-header > div > a {
+    font-size: 28px;
+  }
+</style>
diff --git a/.storybook/demo.css b/.storybook/demo.css
new file mode 100644
index 0000000..820f051
--- /dev/null
+++ b/.storybook/demo.css
@@ -0,0 +1,12 @@
+.demo {
+    padding: 50px;
+    display: inline-block;
+}
+
+.demo .block {
+    margin-bottom: 30px;
+}
diff --git a/.storybook/preview-head.html b/.storybook/preview-head.html
new file mode 100644
index 0000000..e69de29
diff --git a/.storybook/webpack.config.js b/.storybook/webpack.config.js
new file mode 100644
index 0000000..3186d15
--- /dev/null
+++ b/.storybook/webpack.config.js
@@ -0,0 +1,32 @@
+const path = require('path');
+const pathResolve = p => path.resolve(__dirname, '../', p);
+module.exports = ({config, mode}) => {
+    config.resolve.alias = {
+        ...config.resolve.alias,
+        '@': pathResolve('src'),
+        '~': pathResolve('node_modules')
+    };
+    config.module.rules.push({
+        test: /\.scss$/,
+        use: ['style-loader', 'css-loader', 'sass-loader']
+    });
+    config.module.rules.push({
+        test: /\.vue$/,
+        loader: 'storybook-addon-vue-info/loader',
+        enforce: 'post'
+    });
+    config.module.rules.push({
+        test: /\.stories\.js$/,
+        loaders: [require.resolve('@storybook/addon-storysource/loader')],
+        enforce: 'pre'
+    });
+    if (process.env.NODE_ENV === 'production') {
+        config.output.filename = 'bundle.[name].js';
+        config.optimization.splitChunks.automaticNameDelimiter = '.';
+        config.optimization.runtimeChunk = {
+            name: entrypoint => `runtime.${entrypoint.name}`
+        };
+    }
+    // console.log(config);
+    return config;
+};
diff --git a/package.json b/package.json
index d83bbba..6027c58 100644
--- a/package.json
+++ b/package.json
@@ -21,7 +21,9 @@
+        "build-storybook": "build-storybook",
+        "storybook": "start-storybook -p 6006"
     "devDependencies": {
+        "@storybook/addon-backgrounds": "^5.3.18",
+        "@storybook/addon-info": "^5.3.18",
+        "@storybook/addon-storysource": "^5.3.18",
+        "@storybook/addon-viewport": "^5.3.18",
+        "@storybook/addons": "^5.3.18",
+        "@storybook/theming": "^5.3.18",
+        "@storybook/vue": "^5.3.18",
+        "storybook-addon-vue-info": "^1.4.2",

2.2 用例


import {storiesOf} from '@storybook/vue';
import CustomCols from './index';
const totalColumns = [
    // ...
];
const description = {
    CustomCols: {
        props: {
            choose: '用于 v-model 的值',
            totalColumns: '可选择的列',
            storageName: '本地存储的key',
        },
        events: {
            'on-change': '选择的列改变时触发'
        }
    }
};
const info = {
    useDocgen: false,
    summary: '基于 xxx'
};
storiesOf('增强的自定义表格列', module)
    .add(
        '本机存储上次选择结果',
        () => ({
            components: {
                CustomCols
            },
            template: `
            <div class="demo">
                    <div id="demo-result"></div>
                    <custom-cols
                        :storage-name="selColsStoName"
                        :total-columns="totalColumns"
                        v-model="selectedCols"
                        @on-change="onChange"
                    />
            </div>
            `,
            data() {
                return {
                    totalColumns,
                    selColsStoName: `${window.location.hostname}_overview_custom_cols`,
                    selectedCols: ['impression', 'click']
                };
            },
            methods: {
                onChange(v) {
                    //...
                }
            },
            description
        }),
        {
            info
        }
    );



相关文章
|
25天前
|
前端开发
在Webpack配置文件中,如何配置loader以处理其他类型的文件,如CSS或图片
在Webpack配置文件中,通过设置`module.rules`来配置loader处理不同类型的文件。例如,使用`css-loader`和`style-loader`处理CSS文件,使用`file-loader`或`url-loader`处理图片等资源文件。配置示例:在`rules`数组中添加对应规则,指定`test`匹配文件类型,`use`指定使用的loader。
|
1月前
|
缓存 前端开发 JavaScript
Webpack与Babel的进阶配置与优化
通过以上的进阶配置和优化策略,可以更好地发挥`Webpack`与`Babel`的功能,提高项目的性能和开发效率。
|
1月前
|
JavaScript 前端开发 UED
如何配置 Webpack 进行代码分离?
通过以上方法,可以有效地配置Webpack进行代码分离,根据项目的具体需求和场景选择合适的方式,能够显著提高应用的性能和用户体验。在实际应用中,还可以结合其他优化技术,进一步优化Webpack的打包结果和应用的加载速度。
46 5
|
1月前
|
前端开发 JavaScript
webpack相关配置
以上只是Webpack配置的一些常见部分,实际应用中还可以根据具体的项目需求和场景进行更复杂和细致的配置,以满足不同的构建和优化要求。
39 2
|
1月前
|
缓存 JavaScript 前端开发
配置 Webpack 进行代码分离的常见方法有哪些?
通过以上常见方法,可以根据项目的具体需求和场景,灵活地配置Webpack进行代码分离,从而提高应用的性能和加载速度,优化用户体验。
30 3
|
JavaScript 前端开发
史上最走心webpack4.0中级教程——配置之外你应该知道的事
《webpack4.0各个击破系列》适合不满足于只会配置webpack但一时间又看不懂源码的中级读者。我没法保证这个系列是最好的,但至少能保证每一篇博文都跟那些Ctrl+C和Ctrl+V的博文有本质的区别,不信你读读看。
1791 0
|
3月前
|
JavaScript
webpack打包TS
webpack打包TS
138 60
|
2月前
|
缓存 前端开发 JavaScript
Webpack 打包的基本原理
【10月更文挑战第5天】
|
2月前
|
前端开发 JavaScript
ES6模块化和webpack打包
【10月更文挑战第5天】
|
2月前
|
缓存 前端开发 JavaScript
深入了解Webpack:模块打包的革命
【10月更文挑战第11天】深入了解Webpack:模块打包的革命
下一篇
DataWorks