# 推荐克隆我的项目,保证与文章同步git clone https://github.com/lxchuan12/element-analysis.git# npm i -g yarncd element-analysis/element && npm run dev# 或者克隆官方项目git clone https://github.com/ElemeFE/element.git# npm i -g yarncd element && npm run dev
package.json
{"script": {"bootstrap": "yarn || npm i","build:file": "node build/bin/iconInit.js & node build/bin/build-entry.js & node build/bin/i18n.js & node build/bin/version.js","dev": "npm run bootstrap && npm run build:file && cross-env NODE_ENV=development webpack-dev-server --config build/webpack.demo.js & node build/bin/template.js",},}
在 npm run dev 时是先执行了 npm run bootstrap => yarn || npm i 命令,安装好了依赖。
组件开发规范
通过 make new 创建组件目录结构,包含测试代码、入口文件、文档 如果包含父子组件,需要更改目录结构,参考 Button 组件内如果依赖了其他组件,需要在当前组件内引入,参考 Select
make 命令的配置对应根目录 Makefile。
# element/Makefilenew:node build/bin/new.js $(filter-out $@,$(MAKECMDGOALS))
通过查看 Makefile 文件我们知道了make new命令对应的是: node build/bin.new.js
调式源码
在最新版的 VSCode 中,auto attach 功能,默认支持智能调试,如果发现不支持,可以通过快捷键 ctrl + shift + p 查看是否启用。
make new ruochuan 若川组件# Ubuntu 和 Mac 支持 make 命令# 不支持可以用 node window 使用 nodenode build/bin/new.js ruochuan 若川组件
我电脑输入命令 不进入断点 所以我在 package.json => scripts 配置了 调式命令 npm run debug
"scripts": {... // 上面的省略 维持不变"debug":"node build/bin/new.js xzt"},
主流程
文件开头判断
'use strict';console.log();process.on('exit', () => {console.log();});// 第一个参数没传递报错,退出进程if (!process.argv[2]) {console.error('[组件名]必填 - Please enter new component name');process.exit(1);}
process.argv 属性返回一个数组,由命令行执行脚本时的各个参数组成。它的第一个成员总是 node,第二个成员是脚本文件名,其余成员是脚本文件的参数。
引用依赖等
// 路径模块const path = require('path');// 文件模块const fs = require('fs');// 保存文件const fileSave = require('file-save');// 转驼峰const uppercamelcase = require('uppercamelcase');// 第一个参数 组件名const componentname = process.argv[2];// 第二个参数 组件中文名const chineseName = process.argv[3] || componentname;// 转驼峰const ComponentName = uppercamelcase(componentname);// package 路径const PackagePath = path.resolve(__dirname, '../../packages', componentname);// const Files = [];
文件模板Files
const Files = [{filename: 'index.js',content: `import ${ComponentName} from './src/main';/* istanbul ignore next */${ComponentName}.install = function(Vue) {Vue.component(${ComponentName}.name, ${ComponentName});};export default ${ComponentName};`},{filename: 'src/main.vue',content: `<template><div class="el-${componentname}"></div></template><script>export default {name: 'El${ComponentName}'};</script>`},// 省略其他];
New
vue-element-admin
平时日常工作中,做最多的就是写业务模块和组件。当每次新开一个view或者component的时候都需要手动创建一个新.vue,然后创建一个、
