用 TS 搭建 开源 前端函数工具库

419 阅读1分钟

初始化项目 npm init -y

创建ts编译文件 tsc --init

{
	"compilerOptions": {
		"target": "ES2015",
		"module": "ES2015",
		"strict": true,
		"esModuleInterop": true,
		"skipLibCheck": true,
		"allowJs": true,
		"forceConsistentCasingInFileNames": true,
		"declaration":true,
		"outDir": "./dist",
		"lib":[
			"ES5",
			"DOM",
			"ES2015.Promise"
		]
	},
	"exclude": [
		"./dist",
		"./src/test",
		"./node_modules",
		"./jest.config.js"
	]
}

编写打包命令

  • package.json
"scripts": {
	 "test": "echo \"Error: no test specified\" && exit 1",
	 "build": "tsc",
	 "jest": "jest",
	 "jest:coverage": "jest --coverage"
},

添加 单元测试

yarn add jest ts-jest  @types/jest

初始化jest配置

yarn ts-jest config:init

测试

  • index.ts
export const a=(a:number):number=>a
  • test/index.test.ts
import { a } from '../index';
test('a函数', () => {
	expect(a(1)).toBe(1);
});

运行 yarn jest && npm run jest

打包 yarn build && npm run build

发布 npm 参考 https://juejin.cn/post/7022302330183745543