-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (153 loc) · 6.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const path = require('path');
// 引入自动生成文件的webpack plugin
const AutoMaticWebpackPlugin = require('automatic-webpack-plugin');
module.exports = (api, options) => {
api.chainWebpack(webpackConfig => {
let options = [{
/*********** 组件自动化注册 ***********/
// 文件监听等级
maxlevel: 1,
// 监听./src/router/*下的文件夹
inPath: api.resolve('src/components'),
// 自动在./src/router/目录下生成index.js
outPath: api.resolve('src/components/index.js'),
// 模板
// fileName: 文件夹名称
// filePath: 文件夹路径
templateEach: (fileName, filePath) => {
// chunk名称
const chunkName = (
api.resolve(filePath).replace(api.resolve('src/components'), '')
).split(path.sep).join('/');
return `Vue.component('${fileName}', () => import(/* webpackChunkName: "components${chunkName}" */ '.${chunkName}/${fileName}.vue'));`;
},
/**
* 输出模板
* template: 模板名称
* modules: 模板模块名称
*/
out: (template, modules) => {
return `
/* eslint-disable */
/**
* @desc 组件自动化注册
* @important 此文件禁止手动修改!
*/
import Vue from 'vue';
${template}
`;
},
// 自动新建index入口文件
addIndex: [{
// 默认路由
state: 'file',
name: (fileName, filePath) => `${fileName}.vue`,
template: (fileName, filePath) => {
return `
<template>
<div>
组件 ${fileName}
</div>
</template>
<script>
export default {
name: '${fileName}'
};
</script>
`
}
}
]
/*********** 组件自动化注册 ***********/
}, {
/*********** 路由自动化注册 ***********/
// 文件监听等级
maxlevel: 1,
// 监听./src/router/*下的文件夹
inPath: api.resolve('src/pages'),
// 自动在./src/router/目录下生成index.js
outPath: api.resolve('src/pages/index.js'),
// 模板
// fileName: 文件夹名称
// filePath: 文件夹路径
templateEach: (fileName, filePath) => {
return `${fileName}: () => import( /* webpackChunkName: "pages/${fileName}" */"./${fileName}/${fileName}.vue"),`;
},
/**
* 输出模板
* template: 模板名称
* modules: 模板模块名称
*/
out: (template, modules) => {
return `
/* eslint-disable */
/**
* @desc 页面自动化注册
* @important 此文件禁止手动修改!
*/
// 路由配置
const pages = {
${template}
};
export default pages;
`;
},
// 自动新建index入口文件
addIndex: [
{
// 默认路由
state: 'file',
name: (fileName, filePath) => `${fileName}.vue`,
template: (fileName, filePath) => {
return `
<template>
<div>
路由 page-${fileName}
</div>
</template>
<script>
export default {
name: '${fileName}'
};
</script>
`
}
}
]
/*********** 路由自动化注册 ***********/
},
{
/*********** sass文件自动化注册 ***********/
// 文件监听等级
maxlevel: 1,
// 监听./src/sass/*下的文件夹
inPath: api.resolve('src/sass'),
// 自动在./src/sass/目录下生成base.scss
outPath: api.resolve('src/sass/base.scss'),
// 模板
// fileName: 文件夹名称
// filePath: 文件夹路径
templateEach: (fileName, filePath) => {
return `@import "${fileName}/index.scss";`;
},
/**
* 输出模板
* template: 模板名称
* modules: 模板模块名称
*/
out: (template, modules) => {
return `
/* eslint-disable */
/**
* @desc sass文件自动化注册
* @important 此文件禁止手动修改!
*/
${template}
`;
}
/***********sass文件自动化注册 ***********/
}
];
webpackConfig.plugin('automatic').use(AutoMaticWebpackPlugin, [options]);
})
}