在不同 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
        }
    );



相关文章
|
6天前
|
JavaScript 前端开发 UED
如何配置 Webpack 进行代码分离?
通过以上方法,可以有效地配置Webpack进行代码分离,根据项目的具体需求和场景选择合适的方式,能够显著提高应用的性能和用户体验。在实际应用中,还可以结合其他优化技术,进一步优化Webpack的打包结果和应用的加载速度。
20 5
|
6天前
|
前端开发 JavaScript
webpack相关配置
以上只是Webpack配置的一些常见部分,实际应用中还可以根据具体的项目需求和场景进行更复杂和细致的配置,以满足不同的构建和优化要求。
17 2
|
6天前
|
缓存 JavaScript 前端开发
配置 Webpack 进行代码分离的常见方法有哪些?
通过以上常见方法,可以根据项目的具体需求和场景,灵活地配置Webpack进行代码分离,从而提高应用的性能和加载速度,优化用户体验。
13 3
|
12天前
|
缓存 前端开发 JavaScript
前端性能优化:Webpack与Babel的进阶配置与优化策略
【10月更文挑战第28天】在现代Web开发中,Webpack和Babel是不可或缺的工具,分别负责模块打包和ES6+代码转换。本文探讨了它们的进阶配置与优化策略,包括Webpack的代码压缩、缓存优化和代码分割,以及Babel的按需引入polyfill和目标浏览器设置。通过这些优化,可以显著提升应用的加载速度和运行效率,从而改善用户体验。
31 6
|
13天前
|
缓存 前端开发 JavaScript
前端工程化:Webpack与Gulp的构建工具选择与配置优化
【10月更文挑战第27天】在现代前端开发中,构建工具的选择对项目的效率和可维护性至关重要。本文比较了Webpack和Gulp两个流行的构建工具,介绍了它们的特点和适用场景,并提供了配置优化的最佳实践。Webpack适合大型模块化项目,Gulp则适用于快速自动化构建流程。通过合理的配置优化,可以显著提升构建效率和性能。
26 2
|
2月前
|
JavaScript
webpack打包TS
webpack打包TS
131 60
|
1月前
|
缓存 前端开发 JavaScript
Webpack 打包的基本原理
【10月更文挑战第5天】
|
1月前
|
前端开发 JavaScript
ES6模块化和webpack打包
【10月更文挑战第5天】
|
29天前
|
缓存 前端开发 JavaScript
深入了解Webpack:模块打包的革命
【10月更文挑战第11天】深入了解Webpack:模块打包的革命
|
2月前
|
JavaScript 测试技术 Windows
vue配置webpack生产环境.env.production、测试环境.env.development(配置不同环境的打包访问地址)
本文介绍了如何使用vue-cli和webpack为Vue项目配置不同的生产和测试环境,包括修改`package.json`脚本、使用`cross-env`处理环境变量、创建不同环境的`.env`文件,并在`webpack.prod.conf.js`中使用`DefinePlugin`来应用这些环境变量。
92 2
vue配置webpack生产环境.env.production、测试环境.env.development(配置不同环境的打包访问地址)