在 github 建立一个云端仓库 右上角+号New repository,比如我在自己的shenyizhou账号下建了一个zy-ui仓库,默认不用 README.md
用 vue-cli 初始化一个本地仓库 vue init webpack-simple zy-ui按自己需求填写
然后进项目yarn或npm i安装依赖
与云端的 Git 仓库建立关联 进入项目运行以下命令:
1 2 3 4 5 6 7 8 9 git init git add . git commit -m "init 1 .0 .0 " git remote add origin https://github.com/shenyizhou/zy-ui.git git push origin master
注意:
git remote add origin https://github.com/shenyizhou/zy-ui.git只能运行一次,如果关联错了,请删除项目文件夹下的隐藏文件夹.git,重新运行git remote add命令。
git log --pretty=online可以看提交的 commit id,--pretty=online参数将 commit 信息都显示在一行。
git reset --hard commit id 设置版本回退到 commit id 对应的版本。
如果报could not resolve host: github.com错误,在 hosts 文件底部添加192.30.253.112 github.com。
如果是 github Group 开发,git push报 403 没有权限的错误,请把 Group 角色的Member权限改为Owner权限。
修改 Webpack 配置文件 Webpack 是用来将 Vue 组件打包成 JS 的重要工具,也能支持热重载。
修改webpack.config.js文件,把生成文件名即配置output中的filename改为zy-ui.min.js:
1 2 3 4 5 6 7 entry: './src/main.js' , output: { path: path.resolve(__dirname, './dist' ), publicPath: '/dist/' , filename: 'zy-ui.min.js' },
修改入口文件 Webpack 的入口文件也就是我们要执行的语句和生成的组件,之后也将写入package.json的main字段中作为 NPM 的入口。
修改入口文件./src/main.js(例子是将 label 和 input 组合的ZyInputGroup组件):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import InputGroup from "./input-group/index.js" ;const components = [InputGroup];const install = function (Vue ) { components.map(component => { Vue.component(component.name, component); }); }; if (typeof window !== "undefined" && window .Vue) { install(window .Vue); } export default { version: "1.0.0" , install, InputGroup };
这里一开始仿照 ElementUI 用了module.exports = {}的写法,但是import和module.exports无法用在同一个文件中,最后改为ES6 Module的写法。
配置就到此完毕,等写好组件运行yarn build或npm run build命令,Webpack 就会帮我们打包生成zy-ui.min.js啦。
编写组件 在 src 中建立 input-group 文件夹,并创建index.js(也就是被上文的main.js所引入的文件):
1 2 3 4 5 6 7 import Component from "./input-group" ;Component.install = function (Vue ) { Vue.component(Component.name, Component); }; export default Component;
并创建input-group.vue(也就是我们这次要写的组件):
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 <template > <label class ="input-group" > <span class ="input-group-label" > {{label}}</span > <input :value ="value" @input ="handleInput" @compositionstart ="stopInput" @compositionend ="continueInput" ref ="input" type ="text" > </label > </template > <script > export default { name: 'zy-input-group' , props: { label: String , value: String , }, data ( ) { return { isComposition: false , }; }, methods: { handleInput (event ) { if (this .isComposition) { return ; } this .$emit('input' , event.target.value); }, stopInput ( ) { this .isComposition = true ; }, continueInput (event ) { this .isComposition = false ; this .handleInput(event); }, }, }; </script > <style lang ="sass" scoped > .input-group display: flex align-items: center margin-bottom: 8px &-label min-width: 40px margin-right: 8px font-size: 14px input flex: 1 padding: 2px 4px line-height: 22px border : 1px solid #999 border-radius: 4px </style >
到此为止组件就算创建完成啦,运行yarn build命令就会生成zy-ui.min.js,然后在 HTML 页面中引入就 OK 啦。
这时候如果报Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 8.x错误,说明node-sass版本与当前环境不匹配,可以在项目下运行npm rebuild node-sass命令解决。
测试组件 直接引入 zy-ui.min.js 测试 将根目录的index.html改为:
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 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="utf-8" > <title > zy-ui</title > </head > <body > <div id ="app" > <zy-input-group :label ="label" :value ="input" @input ="handleInput" > </zy-input-group > </div > <script src ="https://cdn.bootcss.com/vue/2.5.16/vue.js" > </script > <script src ="./dist/zy-ui.min.js" > </script > <script > const example = Vue.extend({ name: 'example' , data ( ) { return { label: 'Hello' , input: 'zy' , } }, methods: { handleInput (value ) { console .log(value); } }, }); new example().$mount('#app' ); </script > </body > </html >
最后用yarn build命令生成zy-ui.min.js,再在页面上测试。
使用 Webpack 实时预览 使用yarn dev或npm run dev命令,访问http://localhost:8080/,修改组件内容就能实时在页面上看到效果。
上传到 NPM 先在package.json中添加或修改字段(NPM 会使用这些字段):
1 2 3 4 5 6 "main": "dist/zy-ui.min.js", "private": false, "repository": { "type": "git", "url": "git+https://github.com/shenyizhou/zy-ui.git" },
其中:
mian指定入口文件,即项目的入口。
private默认是 true 私人的,需要改为 false,不然会无法发布。
repository指定代码存放地址。
接下来注册一个 NPM 账号,有以下两种方式:
官网注册 。
npm adduser 命令。
首次上传操作前需要输入npm login登录,按照提示输入用户名、密码和邮箱。
如果遇到npm ERR! no_perms Private mode enable, only admin can publish this module:问题,重新设为原来的镜像npm config set registry=http://registry.npmjs.org。
最后一个简单的上传命令npm publish就 OK 啦。
如果提示创建名有类似或已存在,只要修改package.json中的name即可。
其他配置 代码格式 可以在根目录中添加.prettierrc.json:
1 2 3 4 5 6 7 8 { "semi" : true , "singleQuote" : true , "trailingComma" : "all" , "bracketSpacing" : false , "jsxBracketSameLine" : true , "arrowParens" : "always" }
并在Visual Studio Code中添加Prettier - Code formatter插件,并修改以下首选项的设置便可以在相应时点自动格式化:
1 2 3 4 5 "editor.formatOnPaste": true, "editor.formatOnSave": true, "[vue]": { "editor.formatOnSave": true },