Skip to content

Commit ad63516

Browse files
author
longshihui
committed
feat(homepage): 适应新的数据格式
1 parent 69513ed commit ad63516

File tree

5 files changed

+79
-68
lines changed

5 files changed

+79
-68
lines changed

lib/HomepageDataMocker.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const Mock = require('mockjs');
2+
const HomepageDataCreator = require('./HomepageDataCreator');
3+
const OptionsDefaulter = require('./OptionsDefaulter');
4+
5+
function mockProjectPages() {
6+
const wrap = Mock.mock({
7+
'pages|3-20': [
8+
{
9+
name: /[a-zA-Z]{5,10}/,
10+
path: ''
11+
}
12+
]
13+
});
14+
15+
wrap.pages.forEach(pageConfig => {
16+
pageConfig.path = './src' + pageConfig.name + '.js';
17+
});
18+
19+
return wrap.pages.reduce(function(pages, pageConfig) {
20+
pages[pageConfig.name] = {
21+
entry: pageConfig.path
22+
};
23+
return pages;
24+
}, {});
25+
}
26+
27+
function mockDefineTags() {
28+
return Mock.mock({
29+
'tags|3-10': [
30+
{
31+
name: /[a-zA-Z]{5,10}/,
32+
color: () => Mock.Random.color()
33+
}
34+
]
35+
}).tags;
36+
}
37+
38+
function mockPageTags(defineTags) {
39+
const hasTagCount = parseInt(Math.random() * defineTags.length);
40+
return defineTags.slice(0, hasTagCount).map(tag => tag.name);
41+
}
42+
43+
function mockPageDescription() {
44+
return Mock.Random.paragraph();
45+
}
46+
47+
function mockPluginPages(pages, defineTags) {
48+
pages = Object.keys(pages);
49+
let pagesOptions = {};
50+
for (let pageName of pages) {
51+
pagesOptions[pageName] = {
52+
tags: mockPageTags(defineTags),
53+
description: mockPageDescription()
54+
};
55+
}
56+
return pagesOptions;
57+
}
58+
59+
module.exports = function() {
60+
const mockPackageJSON = {
61+
name: 'test app'
62+
};
63+
let mockVueConfig = {
64+
pages: mockProjectPages(),
65+
pluginOptions: {}
66+
};
67+
const defineTags = mockDefineTags();
68+
let pluginOptions = OptionsDefaulter(mockVueConfig, mockPackageJSON, {
69+
defineTags,
70+
pages: mockPluginPages(mockVueConfig.pages, defineTags)
71+
});
72+
return HomepageDataCreator(mockVueConfig, pluginOptions);
73+
};

lib/mockPluginData.js

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

src/packages/homepage/homepage.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div id="app">
33
<section class="section"><div id="logo"></div></section>
44
<section class="section">
5-
<h1 class="h1">{{ appName }}</h1>
5+
<h1 class="h1">{{ title }}</h1>
66
</section>
77
<section class="section">
88
<v-input
@@ -147,9 +147,7 @@ export default Vue.extend({
147147
const pluginConfig: PluginConfig = getConfig();
148148
return {
149149
userInput: '',
150-
appName: pluginConfig.appName
151-
? `welcome to ${pluginConfig.appName}`
152-
: 'Vue Pages Navigator',
150+
title: pluginConfig.title,
153151
tags: pluginConfig.defineTags,
154152
pages: pluginConfig.pages,
155153
showDetail: false,

src/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare interface PageConfig {
2+
name: string,
23
// 页面标题
34
title: string,
45
// 页面访问路径
@@ -15,7 +16,7 @@ declare interface Tag {
1516
}
1617

1718
declare interface PluginConfig {
18-
appName: string,
19+
title: string,
1920
defineTags: Tag[],
2021
pages: PageConfig[]
2122
}

vue.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const mockNavigatorData = require('./lib/mockPluginData');
1+
const HomepageDataMocker = require('./lib/HomepageDataMocker');
22

33
module.exports = {
44
devServer: {
@@ -17,7 +17,7 @@ module.exports = {
1717
.tap(htmlWebpackPluginOptionsArr => {
1818
htmlWebpackPluginOptionsArr.forEach(options => {
1919
options.meta = Object.assign(options.meta || {}, {
20-
navigator: mockNavigatorData()
20+
navigator: HomepageDataMocker()
2121
});
2222
});
2323
return htmlWebpackPluginOptionsArr;

0 commit comments

Comments
 (0)