设计
需求:实现一个函数,同时支持hook和自定义指令,去监听DOM宽高的变化
攻克点:
- 如何监听DOM宽高变化
- 如何用vite打包库
- 如何发布npm
编码
创建一个基本的工程,作为开发依赖引入vue3和vite后,编写src/index.ts:
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
| import type { App } from "vue"
function useResize(el: HTMLElement, callback: Function) { let resize = new ResizeObserver((entries)=> { callback(entries[0].contentRect) }) resize.observe(el) }
const install = (app:App)=> { app.directive("resize", { mounted(el,binding) { useResize(el,binding.value) } }) }
useResize.install = install
export default useResize
|
编写声明文件index.d.ts:
1 2 3 4 5 6 7 8 9
| import { App } from "vue"
declare const useResize: { (el:HTMLElement,callback:Function):void install:(app:App)=>void }
export default useResize
|
打包成库
配置vite.config.js:
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
| import { defineConfig } from "vite"
export default defineConfig({ build: { lib: { entry: "src/index.ts", name: "useResize" }, rollupOptions: { external: ["vue"], output:{ globals:{ useResize:"useResize" } } } } })
|
使用vite build将包打出来,会在dist文件夹打出两个文件,.mjs是ES Module模式,.umd.js是各种模式的综合。
发布到NPM
修改package.json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| { "name": "v-resize", "version": "0.0.1", "description": "", "main": "dist/v-resize.umd.js", "module": "dist/v-resize.mjs", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "vite build" }, "author": "", "files": [ "dist", "index.d.ts" ], "license": "ISC", "dependencies": { "vite": "^4.3.8", "vue": "^3.3.4" } }
|
然后通过三步把包发到npm上:
- 需要拥有npm账号,没有的话使用npm adduser命令注册
- 登录npm,使用npm login
- 使用npm publish发布