最近看到 github 上也搞了自动化因此就发布个库试试水吧。css
npm init -y
复制代码
name:包名
version:版本,第一个数字通常为版本不兼容改动,第二个数字为版本兼容的功能修改,第三个为版本兼容的bug修复
description:包的说明
main:入口文件
scripts:可执行的脚本命令
keywords:关键字
author:做者
license:许可证书
types: 描述文件路径
homepage:github地址
repository:同上
复制代码
└── project
├── README.md
├── src
├── lib
└── package.json
复制代码
修改package.json
main 为 lib/xxx.js,这是你的入口文件。node
在 src 文件内编写项目。git
yarn add jest
复制代码
test js
,名字为 <组件名称>.test.js
├── src
└── component.js
├── lib
├── tests
└── component.test.js
└── package.json
复制代码
package.json
github
"scripts": {
+ "test": "jest"
},
复制代码
function sum(a, b) {
return a + b;
}
module.exports = sum;
复制代码
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
复制代码
由于此次组件涉及相关dom操做,例如 getBoundingClientRect,在模拟环境内没法准确的获取视区,因此须要改写来模拟。npm
// 改写所有
Element.prototype.getBoundingClientRect = jest.fn(() => {
return { width: 100,height: 10,top: 0,left: 0,bottom: 0,right: 0 };
});
// 改写指定
const targetElement = document.getElementById('node2');
targetElement.getBoundingClientRect = jest.fn(() => {
return { top: 200, left: 200, bottom: 0, right: 0, width: 50, height: 50 };
});
复制代码
最后运行 npm run test
json
yarn add gulp
复制代码
const gulp = require('gulp'),
ugLify = require('gulp-uglify'), //压缩js
babel = require("gulp-babel"),
del = require('del');
const workPath={
entry:'./src',
output:'./lib'
}
// 删除文件
function Del(cb) {
// 我这里暂且只有一个文件,因此就这样了
return del([workPath.output+'/parabola.js'], cb);
// 删除 全部的
// return del([workPath.output+'/*'], cb);
}
// 压缩 js 并打包到指定文件夹
function build() {
return gulp.src([workPath.entry+'/**/*.js'])
.pipe(babel({
presets: ['es2015']
}))
.pipe(ugLify())
.pipe(gulp.dest(workPath.output))
}
exports.default = gulp.series(Del, build);
复制代码
"devDependencies": {
+ "gulp": "^4.0.2",
+ "gulp-uglify": "^3.0.2",
+ "gulp-babel": "^8.0.0",
+ "del": "^5.1.0",
+ "babel-preset-es2015": "^6.24.1"
}
复制代码
点我查看 压缩css或热更新gulp
name: test
# 触发的场景
on:
push:
branches:
- master
# 任务
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: npm run test
- run: npm run build
# 发布 npm
publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm ci
# 此为npm的发布命令
- run: npm publish
# npm token 秘钥,须要在 npm 内申请并到github上绑定,绑定在项目设置内
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
复制代码
# 这里必须官方源,建议使用 nrm 管理
npm login
# 项目根目录
npm publish
复制代码
发布失败多是名字重复或者版本号不对。ubuntu
本地测试本身的库可以使用yarn link
,执行后下面会有提示。。。安全