Skip to content

Commit 4c3300b

Browse files
committed
支持页面分类
1 parent b8bb5d3 commit 4c3300b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3950
-1354
lines changed

README.md

+34-48
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
# grace-vue2-webpack-boilerplate
1+
# vue-template run on koa-grace
22

3-
> a boilerplate for vue-webpack-project run on [koa-grace](https://github.com/xiongwilee/koa-grace)
4-
5-
6-
## Documentation
7-
8-
It's designed for [koa-grace](https://github.com/xiongwilee/koa-grace).
9-
10-
Koa-grace is a new generation Nodejs SFB(Separation of Front and Back ends) framework.
11-
12-
Make sure to read it!
13-
14-
15-
## Usage
16-
17-
### Simple demo
3+
- 在使用之前请确保已阅读 [koa-grace](https://github.com/xiongwilee/koa-grace)的相关文档;
4+
- 基于[vuejs-templates](https://github.com/vuejs-templates/webpack)[grace-vue-webpack-boilerplate](https://github.com/Thunf/grace-vue-webpack-boilerplate),根据项目实际需求做了部分定制。
5+
## 使用方法
186

197
``` bash
208
$ cd ~/fe/app/
@@ -23,42 +11,40 @@ $
2311
$ cd grace_boilerplate
2412
$ yarn install(recommended,or use npm install)
2513
$
26-
$ yarn run dev
27-
```
28-
29-
30-
### Custom
14+
$ npm run dev
3115

32-
- clone this boilerplate to a new folder
33-
```bash
34-
$ cd ~/fe/app/
35-
$ git clone git@github.com:haoranw/grace-vue2-webpack-boilerplate.git <newFolderName>
3616
```
37-
- replace all `grace_boilerplate` with `<newFolderName>` in this folder
38-
- install dependency & run dev
39-
```bash
40-
$ cd <newFolderName>
41-
$ yarn install(recommended,or use npm install)
42-
$ npm run dev
17+
新建页面:
4318
```
19+
$ npm run auto
4420
21+
依次输入项目名称和页面名称
22+
```
4523

46-
## What's Included
47-
48-
- `npm run dev`
49-
- Webpack + `vue-loader` for single file Vue components.
50-
- State preserving compilation error overlay
51-
- Source maps
52-
53-
- `npm run build`: Production ready build.
54-
- JavaScript minified with [UglifyJS](https://github.com/mishoo/UglifyJS2).
55-
- HTML copy with [copy-webpack-plugin](https://github.com/kevlened/copy-webpack-plugin).
56-
- All static assets compiled with version hashes for efficient long-term caching, and a production `index.html` is auto-generated with template to these generated assets.
57-
58-
59-
## Fork It And Make Your Own
24+
## Why I made this
25+
- 随着项目内容的扩展,单个仓储内的vues文件越来越多。
26+
在以往的多入口构建方案中,并不支持页面的二级目录。在单个仓储内包含了多个项目的vues文件,并不能很好的区分它们属于哪一个项目模块。
27+
- 在本构建方案中,相关路径如下:
28+
```
29+
└── vues
30+
├── _components
31+
├── project1 // project1
32+
└── project2 // project2
33+
├── page1 // page1入口
34+
├── page2
35+
└── page3
36+
├── components
37+
├── index.js
38+
├── index.vue
39+
└── router.js
40+
```
41+
- 为了配合此路径,在通过`npm run auto`来新建页面时需要输入项目名称。例如新建`project1/page1`,需要在运行命令之后依次输入`project1``page1`
6042

61-
You can fork this repo to create your own boilerplate, and run it on [koa-grace](https://github.com/xiongwilee/koa-grace)
6243

63-
## Special Thanks
64-
A handsome single boy--[Thunf](https://github.com/Thunf)
44+
## Todo
45+
- [x] 新建页面时判断是否存在同名文件
46+
- [x] 新建页面时生成对应路径的controller
47+
- [x] 修改prefix配置,提高兼容性
48+
- [ ] 检测项目名称,必须输入已存在的项目名。独立新建项目的指令
49+
- [ ] 不同项目内的同名页面共存
50+
- [ ] 集成优化后的flexible && pxtorem的配置

build/auto_make/index.js

+27-32
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,35 @@
22

33
const prompt = require('./prompt.js');
44
const tools = require('./tools.js');
5-
let useVue;
6-
let isExist = false;
7-
let step2Text = '请给新建的业务页面命名:';
5+
let isExist = false,
6+
project = '',
7+
step2Text = '请给新建的业务页面命名:';
88

9-
function _step1 () {
10-
prompt.readLine('新建的业务页面是否使用vue(y/n)?', function (data) {
11-
if (data == 'y') {
12-
useVue = true;
13-
return true;
14-
} else if (data == 'n') {
15-
useVue = false;
16-
return true;
17-
}
18-
return false;
19-
});
9+
function _step1() {
10+
prompt.readLine('输入项目名称:', function(data) {
11+
if (tools.illegal(data)) {
12+
step2Text = '项目名只能包含小写字母、数字和下划线,并不能以数字开头,请重新输入新建的业务页面名:'
13+
return false
14+
}
15+
project = data + '/';
16+
return true;
17+
});
2018
}
2119

22-
function _step2 () {
23-
prompt.readLine(step2Text, function (data) {
24-
if (tools.illegal(data)) {
25-
step2Text = '业务页面名只能包含小写字母、数字和下划线,并不能以数字开头,请重新输入新建的业务页面名:'
26-
} else if (tools.exist(data)) {
27-
step2Text = data + '已存在,请重新输入新建的业务页面名:'
28-
isExist = true;
29-
return false;
30-
} else {
31-
step2Text = '';
32-
tools.autoMake(useVue, data);
33-
return true;
34-
}
35-
})
20+
function _step2() {
21+
prompt.readLine(step2Text, function(data) {
22+
if (tools.illegal(data)) {
23+
step2Text = '业务页面名只能包含小写字母、数字和下划线,并不能以数字开头,请重新输入新建的业务页面名:'
24+
} else if (tools.exist(data)) {
25+
step2Text = data + '已存在,请重新输入新建的业务页面名:'
26+
isExist = true;
27+
return false;
28+
} else {
29+
step2Text = '';
30+
tools.autoMake(project, data);
31+
return true;
32+
}
33+
})
3634
}
3735

38-
prompt.startStepByStep({
39-
step1: _step1,
40-
step2: _step2
41-
})
36+
prompt.startStepByStep({step1: _step1, step2: _step2})

build/auto_make/tools.js

+84-50
Original file line numberDiff line numberDiff line change
@@ -6,102 +6,136 @@ require('shelljs/global');
66

77
const controllerPath = path.resolve(__dirname, '../../controller/') + '/';
88
const srcPath = path.resolve(__dirname, '../../vues/') + '/';
9+
function exec(cmd) {
10+
return require('child_process').execSync(cmd).toString().trim()
11+
}
912

10-
let illegal = function (name) {
11-
return !/^[a-z0-9_]+?$/.test(name) || /^\d/.test(name)
13+
let illegal = function(name) {
14+
return !/^[a-z0-9_]+?$/.test(name) || /^\d/.test(name)
1215
}
1316

14-
let exist = function (name) {
15-
let controllers = glob.sync(controllerPath + '*.js').map(function (item) {
16-
return item.split('/').pop().replace('.js', '');
17-
});
18-
let srcModules = glob.sync(srcPath + '*').map(function (item) {
19-
return item.split('/').pop();
20-
});
21-
return controllers.concat(srcModules).indexOf(name) > -1;
17+
let exist = function(name) {
18+
var dirList = exec(`cd ${srcPath} && ls`).split('\n');
19+
let existNames = [];
20+
dirList.map(function(i) {
21+
let fileList = exec(`cd ${srcPath}${i} && ls`).split('\n');
22+
fileList.map(function(files) {
23+
existNames.push(files)
24+
})
25+
});
26+
return existNames.indexOf(name) > -1;
2227
}
2328

24-
let _autoMakeController = function (name) {
25-
fs.writeFileSync(controllerPath + name + '.js', `'use strict';
29+
let _autoMakeController = function(project, name) {
30+
try {
31+
fs.mkdirSync(controllerPath + project)
32+
} catch (e) {}
33+
fs.writeFileSync(controllerPath + project + name + '.js', `'use strict';
2634
2735
exports.index = function*() {
2836
yield this.bindDefault();
2937
30-
yield this.render('module/${ name }', {
38+
yield this.render('${name}', {
3139
siteInfo: this.siteInfo
3240
});
3341
}
3442
3543
`);
3644
}
3745

38-
let _autoMakeEntryJs = function (name) {
39-
let Name = name[0].toUpperCase() + name.slice(1);
46+
let _autoMakeEntryJs = function(project, name) {
47+
let Name = name[0].toUpperCase() + name.slice(1);
4048

41-
fs.writeFileSync(srcPath + name + '/index.js', `import Vue from 'vue'
49+
fs.writeFileSync(srcPath + project + name + '/index.js', `// The Vue build version to load with the \`import\` command
50+
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
51+
import Vue from 'vue'
4252
import VueRouter from 'vue-router'
4353
import VueResource from 'vue-resource'
44-
import { routes } from './router.js'
45-
/**
46-
* 添加需要import的组件
47-
*/
54+
import routes from './router.js'
55+
import App from './index.vue'
4856
49-
/**
50-
* 使用组件
51-
*/
57+
// 初始化路由
58+
/* eslint-disable no-new */
59+
const router = new VueRouter(routes)
5260
61+
// 置入组件
5362
Vue.use(VueRouter)
5463
Vue.use(VueResource)
5564
56-
57-
const router = new VueRouter({
58-
routes
65+
new Vue({
66+
/**
67+
* 提供的元素只能作为挂载点。
68+
* 不同于 Vue 1.x,所有的挂载元素会被 Vue 生成的 DOM 替换。
69+
* 因此不推荐挂载root实例到 <html> 或者 <body> 上。
70+
*/
71+
el: '#app',
72+
template: '<App/>',
73+
components: { App },
74+
/**
75+
* 置入路由
76+
*/
77+
router
5978
})
60-
61-
const app = new Vue({
62-
router,
63-
}).$mount('#app')
79+
/* eslint-enable no-new */
6480
`);
6581
}
6682

67-
let _autoMakeEntryVue = function (name) {
68-
fs.mkdirSync(srcPath + name + '/components');
69-
fs.writeFileSync(srcPath + name + '/components/index.vue', `<template>
70-
<div>
83+
let _autoMakeEntryVue = function(project, name) {
84+
fs.mkdirSync(srcPath + project + name + '/components');
85+
fs.writeFileSync(srcPath + project + name + '/index.vue', `<template>
86+
<div id="${name}">
87+
<router-view></router-view>
7188
</div>
7289
</template>
7390
7491
<script>
7592
export default {
76-
93+
name: '${name}',
94+
components: {
95+
}
7796
}
7897
</script>
7998
8099
<style lang="less">
81100
</style>
82-
83101
`);
84-
}
85102

86-
let _autoMakeRouter = function (name) {
87-
fs.writeFileSync(srcPath + name + '/router.js', `import index from './components/index.vue';
103+
fs.writeFileSync(srcPath + project + name + '/components/' + name + '.vue', `<template>
104+
<div class="${name}">
105+
</div>
106+
</template>
88107
89-
export const routes = [
90-
{ path:'/', component: index }
91-
]
108+
<script>
109+
export default {
110+
components: {
111+
}
112+
}
113+
</script>
92114
115+
<style lang="less">
116+
</style>
93117
`);
94118
}
95119

120+
let _autoMakeRouter = function(project, name) {
121+
fs.writeFileSync(srcPath + project + name + '/router.js', `module.exports = {
122+
routes: [{
123+
path: '/',
124+
component: require('./components/${name}.vue')
125+
}, {
126+
path: '*',
127+
redirect: '/'
128+
}]
129+
}
130+
`);
131+
}
96132

97-
let autoMake = function (useVue, name) {
98-
_autoMakeController(name);
99-
if (useVue) {
100-
mkdir('-p', srcPath + name)
101-
_autoMakeEntryJs(name);
102-
_autoMakeEntryVue(name);
103-
_autoMakeRouter(name);
104-
}
133+
let autoMake = function(project, name) {
134+
_autoMakeController(project, name);
135+
mkdir('-p', srcPath + project + name)
136+
_autoMakeEntryJs(project, name);
137+
_autoMakeEntryVue(project, name);
138+
_autoMakeRouter(project, name);
105139
}
106140

107141
exports.illegal = illegal;

0 commit comments

Comments
 (0)