一套功能相似于有赞商城后台管理系统中店铺-微页面的系统,该系统实现用户能够选择所须要的组件,拖动调整组件的顺序,以及编辑组件的内容而且在移动端展现等相关功能,以下图所示。 css
仔细想了一想,该系统须要实现下面三大功能html
服务端渲染?仍是用原生js封装组件?使用框架选 react 仍是 vue?(其实我不会react,但请允许我装个b []~( ̄▽ ̄)~*)vue
由于以前且尝试开发过element ui库的组件,详情点这,对于element ui的架构有必定的了解,因而打算把基本的功能照搬element ui的架构,先基于vue开发一套组件系统,其余的功能再自行定制。react
构建工具我选择了webpack,大版本为4.0,试着吃吃螃蟹。先思考下须要webpack的哪些功能:webpack
功能 | 相关插件 |
---|---|
es6-es5 | babel-loader |
sass-css | sass-loader css-loader style-loader |
css文件抽离 | mini-css-extract-plugin |
html模版 | html-loader 以及 html-webpack-plugin |
vue文件解析 | vue-loader |
图片文件解析 | file-loader |
markdown转换vue | vue-markdown-loader |
删除文件 | clean-webpack-plugin 或者脚本 |
热更新 | HotModuleReplacementPlugin |
webpack配置合并 | webpack-merge |
基本上就是以上的loader及插件了。git
由于组件库涉及到多个功能的打包,好比组件库,预览时的配置,以及后面会提到的和其余项目的集成,因此对于webpack的配置,能够学习vue-cli中的配置,将公共的配置抽离,特殊的配置经过webpack-merge插件完成合并。es6
这里将不一样的需求及功能抽离成了如上图所示的几个webpack配置, 其中webpack.base.js为公共的配置,以下图所示,分别处理了vue文件,图片以及js文件github
这篇文章的目的是主要提供一个思路,因此这里不详细讲解webpack的相关配置。web
其实对于开发来讲,提升效率的主要方式就是将相同的事物封装起来,就比如一个函数的封装,这里由于组件文件的结构是类似的,因此我学习element ui的作法,将组件的建立过程封装成脚本,运行命令就可以直接生成好文件,以及添加配置文件。代码以下vue-router
const path = require('path')
const fileSave = require('file-save')
const getUnifiedName = require('../utils').getUnifiedName
const uppercamelcase = require('uppercamelcase')
const config = require('../config')
const component_name = process.argv[2] //组件文件名 横杠分隔
const ComponentName = uppercamelcase(component_name) //组件帕斯卡拼写法命名
const componentCnName = process.argv[3] || ComponentName //组件中文名
const componentFnName = '$' + getUnifiedName(component_name) //组件函数名
/** 如下模版字符串不能缩进,不然建立的文件内容排版会混乱 **/
const createFiles = [
{
path: path.join(config.packagesPath, component_name, 'index.js'),
content: `import Vue from 'vue' import ${ComponentName} from './src/main.vue' const Component = Vue.extend(${ComponentName}) ${ComponentName}.install = function(Vue) { Vue.component(${ComponentName}.name, ${ComponentName}) Vue.prototype.${componentFnName} = function() { const instance = new Component() instance.$mount() return instance } } export default ${ComponentName}`
}, {
path: path.join(config.packagesPath, component_name, 'src', 'main.scss'),
content: `@import '~@/style/common/variable.scss'; @import '~@/style/common/mixins.scss'; @import '~@/style/common/functions.scss';`
}, {
path: path.join(config.packagesPath, component_name, 'src', 'main.vue'),
content: `<template> </template> <script> export default { name: '${getUnifiedName(component_name)}' } </script> <style lang="scss" scoped> @import './main.scss'; </style>`
}, {
path: path.join(config.examplesPath, 'src', 'doc', `${component_name}.md`),
content: `## ${ComponentName} ${componentCnName} <div class="example-conainer"> <div class="phone-container"> <div class="phone-screen"> <div class="title"></div> <div class="webview-container"> <sg-${component_name}></sg-${component_name}> </div> </div> </div> <div class="edit-container"> <edit-component> </div> </div> <script> import editComponent from '../components/edit-components/${component_name}' export default { data() { return { } }, components: { editComponent } } </script> `
}, {
path: path.join(config.examplesPath, 'src/components/edit-components', `${component_name}.vue`),
content: ``
}
]
const componentsJson = require(path.join(config.srcPath, 'components.json'))
const docNavConfig = require(path.join(config.examplesPath, 'src', 'router', 'nav.config.json'))
if(docNavConfig[component_name]) {
console.log(`${component_name} 已经存在,请检查目录或者components.json文件`)
process.exit(0)
}
if(componentsJson[component_name]) {
console.log(`${component_name} 已经存在,请检查目录或者nav.config.json文件`)
process.exit(0)
}
createFiles.forEach(file => {
fileSave(file.path)
.write(file.content, 'utf8')
.end('\n');
})
componentsJson[component_name] = {}
componentsJson[component_name].path = `./packages/${component_name}/index.js`
componentsJson[component_name].cnName = componentCnName
componentsJson[component_name].fnName = componentFnName
componentsJson[component_name].propsData = {}
docNavConfig[component_name] = {}
docNavConfig[component_name].path = `./src/doc/${component_name}.md`
docNavConfig[component_name].cnName = componentCnName
docNavConfig[component_name].vueRouterHref = '/' + component_name
docNavConfig[component_name].fnName = componentFnName
fileSave(path.join(config.srcPath, 'components.json'))
.write(JSON.stringify(componentsJson, null, ' '), 'utf8')
.end('\n');
fileSave(path.join(config.examplesPath, 'src', 'router', 'nav.config.json'))
.write(JSON.stringify(docNavConfig, null, ' '), 'utf8')
.end('\n');
console.log('组件建立完成')
复制代码
以及删除组件
const path = require('path')
const fsdo = require('fs-extra')
const fileSave = require('file-save')
const config = require('../config')
const component_name = process.argv[2]
const files = [{
path: path.join(config.packagesPath, component_name),
type: 'dir'
}, {
path: path.join(config.examplesPath, 'src', 'doc', `${component_name}.md`),
type: 'file'
}, {
path: path.join(config.srcPath, 'components.json'),
type: 'json',
key: component_name
}, {
path: path.join(config.examplesPath, 'src', 'router', 'nav.config.json'),
type: 'json',
key: component_name
}]
files.forEach(file => {
switch(file.type) {
case 'dir':
case 'file':
removeFiles(file.path)
break;
case 'json':
deleteJsonItem(file.path, file.key);
break;
default:
console.log('unknow file type')
process.exit(0);
break;
}
})
function removeFiles(path) {
fsdo.removeSync(path)
}
function deleteJsonItem(path, key) {
const targetJson = require(path)
if(targetJson[key]) {
delete targetJson[key]
}
fileSave(path)
.write(JSON.stringify(targetJson, null, ' '), 'utf8')
.end('\n');
}
console.log('组件删除完成')
复制代码
用过vue的同窗应该知道vue开发组件有两种方式,一种是 vue.component()的方式,另外一种是vue.extend()方式,能够在上面的建立文件代码中看见,这两种方式我都用到了。缘由是,对于配置组件的页面,须要用到动态组件,对于移动端渲染,动态组件确定是不行的,因此须要用到函数形式的组件。
打包vue组件,固然不能将其余无用的功能打包进库中,因此再来一套单独的webpack配置
const path = require('path')
const merge = require('webpack-merge')
const webpackBaseConfig = require('./webpack.base')
const miniCssExtractPlugin = require('mini-css-extract-plugin')
const config = require('./config')
const ENV = process.argv.NODE_ENV
module.exports = merge(webpackBaseConfig, {
output: {
filename: 'senguo.m.ui.js',
path: path.resolve(config.basePath, './dist/ui'),
publicPath: '/dist/ui',
libraryTarget: 'umd'
},
externals: {
vue: {
root: 'Vue',
commonjs: 'vue',
commonjs2: 'vue',
amd: 'vue'
}
},
module: {
rules: [
{
test: /\.(sc|c)ss$/,
use: [miniCssExtractPlugin.loader,
{loader: 'css-loader'},
{loader: 'sass-loader'}]
}
]
},
plugins: [
new miniCssExtractPlugin({
filename: "sg-m-ui.css"
})
]
})
复制代码
先看看组件的入口文件,这是经过配置文件自动生成的,因此没必要操心什么,本文的最后会奉上精简版的vue组件开发webpack脚手架,能够直接拿去用哦。
//文件从 build/bin/build-entry.js生成
import SgAlert from './packages/alert/index.js'
import SgSwipe from './packages/swipe/index.js'
import SgGoodsList from './packages/goods-list/index.js'
const components = [SgAlert,SgSwipe,SgGoodsList]
const install = function(Vue) {
components.forEach(component => {
component.install(Vue)
})
}
/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
// module.exports = {install}
// module.exports.default = module.exports
export default {install}
复制代码
是否是很简单啊。
由于开发组件时确定是须要一套webpack的配置用于启动web服务和热更新,因此在build文件夹中,编写了另一套webpack配置用于开发时预览组件
<--webpack.dev.js-->
const path = require('path')
const webpack = require('webpack')
const merge = require('webpack-merge')
const webpackBaseConfig = require('./webpack.base')
const webpackCleanPlugin = require('clean-webpack-plugin')
const config = require('./config')
module.exports = merge(webpackBaseConfig, {
module: {
rules: [{
test: /\.(sc|c)ss$/,
use: [{
loader: 'style-loader'
}, {
loader: 'vue-style-loader',
}, {
loader: 'css-loader'
}, {
loader: 'sass-loader'
}]
}]
},
devServer: {
host: '0.0.0.0',
publicPath: '/',
hot: true,
},
plugins: [
new webpackCleanPlugin(
['../dist'], {
root: config.basePath,
allowExternal: true
}
),
new webpack.HotModuleReplacementPlugin()
]
})
复制代码
<--webpack.demo.js-->
const path = require('path')
const merge = require('webpack-merge')
const webpackDevConfig = require('./webpack.dev')
const config = require('./config')
const htmlWebpackPlugin = require('html-webpack-plugin')
const webpackDemoConfig= merge(webpackDevConfig, {
entry: path.resolve(config.examplesPath, 'index.js'),
output: {
filename: 'index.js',
path: path.resolve(config.basePath, './dist'),
publicPath: '/'
},
module: {
rules: [{
test: /\.md$/,
use: [
{
loader: 'vue-loader'
},
{
loader: 'vue-markdown-loader/lib/markdown-compiler',
options: {
raw: true
}
}]
}, {
test: /\.html$/,
use: [{loader: 'html-loader'}]
}, ]
},
plugins: [
new htmlWebpackPlugin({
template: path.join(config.examplesPath, 'index.html'),
inject: 'body'
})
]
})
module.exports = webpackDemoConfig
复制代码
在其中能够看见使用了md文件,使用md文件的目的是:
经过vue-markdown-loader就能够将md文件解析成vue文件了,这个库是element ui 的官方人员开发的,其实原理很简单,就是将md文档先解析成html文档,再将html文档放入vue文档的template标签内,script 和 style标签单独抽离并排放置,就是一个vue的文档了,解析完后交给vue-loader处理就能够将md文档内容渲染到页面了。
就像上面建立文件那样,经过配置文件以及脚本动态生成路由文件,运行以前,先建立路由js文件便可
配置文件一览
{
"main": {
"path": "./src/pages/main.vue",
"cnName": "首页",
"vueRouterHref": "/main"
},
"alert": {
"path": "./src/doc/alert.md",
"cnName": "警告",
"vueRouterHref": "/alert"
},
"swipe": {
"path": "./src/doc/swipe.md",
"cnName": "轮播",
"vueRouterHref": "/swipe"
},
"goods-list": {
"path":"./src/doc/goods-list.md",
"cnName": "商品列表",
"vueRouterHref": "/goods-list"
}
}
复制代码
构建完成的路由文件
//文件从 build/bin/build-route.js生成
import Vue from 'vue'
import Router from 'vue-router'
const navConfig = require('./nav.config.json')
import SgMain from '../pages/main.vue'
import SgAlert from '../doc/alert.md'
import SgSwipe from '../doc/swipe.md'
import SgGoodsList from '../doc/goods-list.md'
Vue.use(Router)
const modules = [SgMain,SgAlert,SgSwipe,SgGoodsList]
const routes = []
Object.keys(navConfig).map((value, index) => {
let obj = {}
obj.path = value.vueRouterHref,
obj.component = modules[index]
routes.push(obj)
})
export default new Router({
mode: 'hash',
routes
})
复制代码
就这样,从组件的建立到项目的运行都是自动的啦。
固然是用的插件啦,简单粗暴,看这里,它是基于Sortable.js封装的,有赞貌似用的也是这个库。
但看到右边的那个编辑框,我不由陷入了沉思,怎么样才能作到只开发一次,这个配置页面就不用管理了?
因为中文的博大精深,姑且将下面的关键字分为两种:
分析需求能够发现,功能组件的内容都是能够由选项组件编辑的,最初个人想法是,选项组件的内容也根据配置文件生成,好比组件的props数据,这样就不用开发选项组件了,仔细一想仍是太年轻了,配置项不可能知足设计稿以及不一样的需求。
只能开发另外一套选择组件咯,因而乎将选项组件的内容追加到自动生成文件的列表,这样微微先省点事。
功能组件与选项组件间的通讯可不是一件简单的事,首先要全部的组件实现同一种通讯方式,其次也不能由于参数的丢失而致使报错,更重要的是,功能组件在移动端渲染后须要将选项组件配置的选项还原。
嗯,用那些方式好呢?
vuex? 须要对每个组件都添加状态管理,麻烦
eventBus? 我怕我记不住事件名
props?是个好办法,可是选项组件要怎么样高效的把配置的数据传递出来呢?v-model就是一个很优雅的方式
首先功能组件的props与选项组件的v-model绑定同一个model,这样就能实现高效的通讯,就像这样:
<--swipe.md-->
## Swipe 轮播
<div class="example-conainer">
<div class="phone-container">
<div class="phone-screen">
<div class="title"></div>
<div class="webview-container" ref="phoneScreen">
<sg-swipe :data="data"></sg-swipe>
</div>
</div>
</div>
<div class="edit-container">
<edit-component v-model="data">
</div>
</div>
<script>
import editComponent from '../components/edit-components/swipe'
export default {
data() {
return {
data: {
imagesList: ['https://aecpm.alicdn.com/simba/img/TB183NQapLM8KJjSZFBSutJHVXa.jpg']
}
}
},
components: {
editComponent
}
}
</script>
复制代码
就这样,完美解决组件间通讯,可是这是静态的组件,别忘了还有一个难点,那就是动态组件该如何进行参数传递,以及知道传递什么参数而不会致使报错。
先看个示例图
其中左侧手机里的内容是用v-for渲染的动态组件,右侧选项组件也是动态组件,这样就实现了上面所想的,功能组件和选项组件只需开发完成,配置页面就会自动添加对应的组件,而不用管理,以下图所示
但这样就会有一个问题,每一个组件内部的数据不一致,得知道选中的组件是什么,以及知道该如何传递正确的数据,还记得以前的配置文件吗?其实这些组件也是读取的配置文件渲染的,配置文件以下:
{
"alert": { // 组件名
"path": "./packages/alert/index.js",
"cnName": "警告",
"fnName": "$SgAlert",
"propsData": {} //props须要传递的数据
},
"swipe": {
"path": "./packages/swipe/index.js",
"cnName": "轮播",
"fnName": "$SgSwipe",
"propsData": {
"imagesList": ["https://aecpm.alicdn.com/simba/img/TB183NQapLM8KJjSZFBSutJHVXa.jpg"]
}
},
"goods-list": {
"path": "./packages/goods-list/index.js",
"cnName": "商品列表",
"fnName": "$SgGoodsList",
"propsData": {
}
}
}
复制代码
每个组件的配置都添加了propsData,里面的元素和组件props数据以及选项组件v-model关联,这样就不用担忧缺失字段而报错了,可是这样的作法给开发添加了麻烦。
组件编写的过程当中还得将数据手动添加到配置文件,看能不能直接读取vue文件的props解决这个问题
到了这一步,组件以及组件的编辑拖拽功能均已完成,要考虑的是,如何把编辑拖拽功能页面集成到现有的后台系统中去,由于拖拽编辑组件的功能是给客户用的,这里为了效率和组件系统一同开发了。
vue路由的配置,每个路由都对应一个组件,那么这个系统也能够这样作,只须要把中间那部分拖拽配置组件的页面打包后引入到父工程(商户后台管理系统)中去就行了,那么该如何处理呢?其实很简单,将webpack打包入口设置成相对应的vue文件就行,就像这样。
const path = require('path')
const merge = require('webpack-merge')
const webpackBaseConfig = require('./webpack.base')
const config = require('./config')
const miniCssExtractPlugin = require('mini-css-extract-plugin')
const ENV = process.argv.NODE_ENV
module.exports = merge(webpackBaseConfig, {
entry: path.resolve(config.examplesPath, 'src/manage-system-app.vue'),
output: {
filename: 'components-manage.js',
path: path.resolve(config.basePath, './dist/components-manage'),
publicPath: '/dist/components-manage',
libraryTarget: 'umd'
},
externals: {
vue: {
root: 'Vue',
commonjs: 'vue',
commonjs2: 'vue',
amd: 'vue'
}
},
module: {
rules: [
{
test: /\.(sc|c)ss$/,
use: [
miniCssExtractPlugin.loader,
{loader: 'css-loader'},
{loader: 'sass-loader'}
]
}
]
},
plugins: [
new miniCssExtractPlugin({
filename: "components-manage.css"
})
]
})
复制代码
而后在父工程引入组件库以及样式文件,再将路由对应的组件配置成这个打包后的js文件就行。
import EditPage from '@/pages/EditPage.js'
new Router({
routes: [{
path: '/edit-page',
components: EditPage
}]
})
复制代码
这还不简单么,看代码就懂了。
class InsertModule {
constructor(element, componentsData, thatVue) {
if(element instanceof String) {
const el = document.getElementById(element)
this.element = el ? el : document.body
} else if(element instanceof HTMLElement) {
this.element = element
} else {
return console.error('传入的元素不是一个dom元素id或者dom元素')
}
if(JSON.stringify(componentsData) == '[]') {
return console.error('传入的组件列表为空')
}
this.componentsData = componentsData
this.vueInstance = thatVue
this.insertToElement()
}
insertToElement() {
this.componentsData.forEach((component, index) => {
const componentInstance = (this.vueInstance[component.fnName]
&&
this.vueInstance[component.fnName] instanceof Function
&&
this.vueInstance[component.fnName]({propsData: component.propsData})
||
{}
)
if (componentInstance.$el) {
componentInstance.$el.setAttribute('component-index', index)
componentInstance.$el.setAttribute('isComponent', "true")
componentInstance.$el.setAttribute('component-name', component.fnName)
this.element.appendChild(
componentInstance.$el
)
} else {
console.error(`组件 ${component.fnName} 不存在`)
}
})
}
}
const install = function(Vue) {
Vue.prototype.$insertModule = function(element, componentsData) {
const self = this;
return new InsertModule(element, componentsData, self)
}
}
/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
export default {install}
复制代码
这里将 组件的props数据传入至组件完成相关配置,这也是以前为何选择prosp通讯的缘由
this.vueInstance[component.fnName]({propsData: component.propsData})
<-- swipe.js -->
import Vue from 'vue'
import Swipe from './src/main.vue'
const Component = Vue.extend(Swipe)
Swipe.install = function(Vue) {
Vue.component(Swipe.name, Swipe)
Vue.prototype.$SgSwipe = function(options) {
const instance = new Component({
data: options.data || {},
propsData: {data: options.propsData || {}} //这里接收了数据
})
instance.$mount()
return instance
}
}
export default Swipe
复制代码
就系介样,渲染完成,200元一条的8g内存的梦啥时候可以实现?
最后,奉上此系统精简版的webpack配置,除了没拖拽系统以及组件渲染系统,其余的基本都支持,能够在此配置上定制本身的功能,编写本身的组件系统,可是强烈建议阅读element ui的脚手架配置,尝试从0-1定制本身的脚手架哦。