Skip to content

Commit 63a6664

Browse files
committed
feat/add-storybook
- Add story book for component previewing - Add custom preview method for renderering Vue3 Component - Modify scripts for storybook boilerplate - Add documentation in README for booting and previewing project feat/storybook - Add story book for component previewing - Add custom preview method for renderering Vue3 Component - Modify scripts for storybook boilerplate - Add documentation in README for booting and previewing project
1 parent 0c48388 commit 63a6664

10 files changed

Lines changed: 5372 additions & 174 deletions

File tree

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ indent_size = 2
77
end_of_line = lf
88
insert_final_newline = true
99
trim_trailing_whitespace = true
10+
quote_type = single

.storybook/custom-presets.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var VueLoaderPlugin = require('vue-loader/dist/plugin');
2+
3+
function webpack(config) {
4+
return {
5+
...config,
6+
plugins: [...config.plugins, new VueLoaderPlugin.default()],
7+
module: {
8+
...config.module,
9+
rules: [
10+
...config.module.rules,
11+
{
12+
test: /\.vue$/,
13+
loader: require.resolve('vue-loader'),
14+
options: {},
15+
},
16+
{
17+
test: /\.(ts|tsx)$/,
18+
use: [
19+
{
20+
loader: require.resolve('ts-loader'),
21+
options: {
22+
transpileOnly: true,
23+
},
24+
},
25+
],
26+
},
27+
],
28+
},
29+
resolve: {
30+
...config.resolve,
31+
extensions: [...config.resolve.extensions, '.vue', '.ts'],
32+
alias: {
33+
...config.resolve.alias,
34+
vue$: require.resolve('vue/dist/vue.esm-bundler.js'),
35+
},
36+
},
37+
};
38+
}
39+
40+
exports.webpack = webpack;

.storybook/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
stories: ['../packages/**/*.stories.[tj]s'],
5+
presets: [path.resolve(__dirname, './custom-presets')],
6+
};

.storybook/preview.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { addDecorator } from "@storybook/html";
2+
import { createApp } from "vue";
3+
import "../src/style/element-ui@2.13.2.css";
4+
/**
5+
* Wraps a story into a Vue Element
6+
* @param {JSX.Element} template - Story templates
7+
* @param {VueElement}
8+
*/
9+
const Wrapper = (template) => {
10+
return {
11+
data() {
12+
return {};
13+
},
14+
template,
15+
};
16+
};
17+
18+
/**
19+
* Custom Addon for previewing ElementPlus component in Vue3
20+
* Due to lacking of support for Vue3, the rendering method has to be made by ourself
21+
* This method takes a template string as parameter returns a HTMLElement which will be inserted to the iframe root node by `@StoryBook`
22+
* @param {Story} content
23+
* @return {HTMLElement}
24+
*/
25+
function CustomDecorator(content) {
26+
const { template, installer } = content();
27+
const app = createApp(Wrapper(template));
28+
installer(app);
29+
const entry = document.createElement("div");
30+
entry.className = "element-plus-previewer";
31+
app.mount(entry);
32+
return entry;
33+
}
34+
35+
addDecorator(CustomDecorator);

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
Element-Plus
1+
Element-Plus
2+
3+
## Bootstrap project
4+
With command
5+
```bash
6+
$ yarn bs
7+
```
8+
the project will install all dependencies and run `lerna bootstrap` to initialize the project
9+
10+
## Storybook preview
11+
With command
12+
```bash
13+
$ yarn sb
14+
```
15+
the project will launch `@storybook` client for you to preview all existing component

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@
66
"dev": "vite",
77
"build": "vite build",
88
"cz": "npx git-cz",
9-
"gc": "sh ./scripts/gc.sh"
9+
"gen": "sh ./scripts/gc.sh",
10+
"storybook": "start-storybook",
11+
"bootstrap": "yarn && npx lerna bootstrap"
1012
},
1113
"dependencies": {
1214
"vue": "^3.0.0-rc.1",
1315
"vue-router": "^4.0.0-beta.2"
1416
},
1517
"devDependencies": {
18+
"@babel/core": "^7.10.5",
19+
"@storybook/html": "^5.3.19",
1620
"@vue/compiler-sfc": "^3.0.0-rc.1",
21+
"babel-loader": "^8.1.0",
22+
"babel-preset-vue": "^2.0.2",
1723
"cz-conventional-changelog": "^3.2.0",
1824
"lerna": "^3.22.1",
19-
"vite": "^1.0.0-rc.1"
25+
"ts-loader": "^8.0.1",
26+
"typescript": "^3.9.7",
27+
"vite": "^1.0.0-rc.1",
28+
"vue-loader": "^v16.0.0-beta.4"
2029
},
2130
"config": {
2231
"commitizen": {

packages/button/doc/index.vue

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/button/index.stories.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import ElButton from './index';
2+
3+
export default {
4+
title: 'Button',
5+
};
6+
7+
export const NormalButton = () => {
8+
return { template: '<el-button>With Text</el-button>', installer: ElButton };
9+
};
10+
export const ButtonTwo = () => {
11+
return { template: '<el-button>button two</el-button>', installer: ElButton };
12+
};

scripts/gc.sh

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,52 @@ for i in $(echo $NAME | sed 's/[_|-]\([a-z]\)/\ \1/;s/^\([a-z]\)/\ \1/'); do
2525
done
2626
NAME=$NORMALIZED_NAME
2727

28-
TEMPLATE_INDEX_VUE="<template>\n
29-
<div>\n
30-
</div>\n
31-
</template>\n
32-
<script lang='ts'>\n
33-
export default {\n
34-
NAME: 'El${NAME}',\n
35-
props: {\n
36-
},\n
37-
setup(props,ctx) { }\n
38-
};\n
39-
</script>\n
40-
<style>\n
41-
</style>\n
42-
"
43-
44-
TEMPLATE_INDEX_TS="\n
45-
import { App } from 'vue'\n
46-
import ${NAME} from './src/index.vue'\n
47-
export default (app: App) => {\n
48-
app.component(${NAME}.name, ${NAME})\n
49-
}
50-
"
51-
TEMPLATE_PKG_JSON="\n
52-
{\n
53-
\"name\": \"@element-plus/${INPUT_NAME}\",\n
54-
\"description\": \"\",\n
55-
\"version\": \"0.1.0\",\n
56-
\"main\": \"./index.ts\",\n
57-
\"license\": \"MIT\",\n
58-
\"dependencies\": {}\n
59-
}\n
60-
"
61-
6228
mkdir -p "$DIRNAME"
6329
mkdir -p "$DIRNAME/src"
64-
echo $TEMPLATE_INDEX_VUE >>"$DIRNAME/src/index.vue"
65-
echo $TEMPLATE_INDEX_TS >>"$DIRNAME/index.ts"
66-
echo $TEMPLATE_PKG_JSON >>"$DIRNAME/package.json"
30+
31+
cat <<EOF >"$DIRNAME/index.ts"
32+
import { App } from 'vue'
33+
import ${NAME} from './src/index.vue'
34+
export default (app: App) => {
35+
app.component(${NAME}.name, ${NAME})
36+
}
37+
38+
EOF
39+
40+
cat <<EOF >"$DIRNAME/src/index.vue"
41+
<template>
42+
<div>
43+
</div>
44+
</template>
45+
<script lang='ts'>
46+
export default {
47+
NAME: 'El${NAME}',
48+
props: { },
49+
setup(props,ctx) { }
50+
};
51+
</script>
52+
<style>
53+
</style>
54+
55+
EOF
56+
57+
cat <<EOF >"$DIRNAME/index.stories.js"
58+
import El${NAME} from './index'
59+
60+
export default {
61+
title: "${NAME}"
62+
}
63+
64+
EOF
65+
66+
cat <<EOF >"$DIRNAME/package.json"
67+
{
68+
"name": "@element-plus/${INPUT_NAME}",
69+
"description": "ElementPlus Component ${INPUT_NAME}",
70+
"version": "0.1.0",
71+
"main": "dist/index.js",
72+
"license": "MIT",
73+
"dependencies": {}
74+
}
75+
76+
EOF

0 commit comments

Comments
 (0)