|
| 1 | +### 自定义组件库(Vue3) |
| 2 | + |
| 3 | +- https://juejin.cn/column/7118932817119019015 |
| 4 | + |
| 5 | +#### 初始化monorepo项目 |
| 6 | + |
| 7 | +- 参考 https://github.com/bin-K/monorepo-pnpm-vue |
| 8 | + |
| 9 | +#### 组件库的环境配置 |
| 10 | + |
| 11 | +- 技术选型:使用 Vite+Ts 开发的是 Vue3 组件库 |
| 12 | +- typescript、vue3 、sass等 |
| 13 | + |
| 14 | +```shell |
| 15 | +pnpm add vue@next typescript sass -D -w |
| 16 | +``` |
| 17 | + |
| 18 | +##### 初始化ts |
| 19 | + |
| 20 | +- 执行npx tsc --init 生成tsconfig.json ts 的配置文件 |
| 21 | + |
| 22 | +```shell |
| 23 | +npx tsc --init |
| 24 | +``` |
| 25 | + |
| 26 | +```json |
| 27 | +{ |
| 28 | + "compilerOptions": { |
| 29 | + "baseUrl": ".", |
| 30 | + "jsx": "preserve", |
| 31 | + "strict": true, |
| 32 | + "target": "ES2015", |
| 33 | + "module": "ESNext", |
| 34 | + "skipLibCheck": true, |
| 35 | + "esModuleInterop": true, |
| 36 | + "moduleResolution": "Node", |
| 37 | + "lib": ["esnext", "dom"] |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +##### 搭建基于vite的vue3项目 |
| 43 | + |
| 44 | +- 新建play 文件夹,初始化pnpm init, 后续的组件调试就在这个项目下进行 |
| 45 | +- 安装依赖,在play安装vite和vitejs/plugin-vue插件,@vitejs/plugin-vue插件是为了解析后缀为.vue文件的 |
| 46 | +- 新建vite.config.ts |
| 47 | + |
| 48 | +```shell |
| 49 | +pnpm add vite @vitejs/plugin-vue -D |
| 50 | + |
| 51 | +touch vite.config.ts |
| 52 | +``` |
| 53 | + |
| 54 | +```ts |
| 55 | +import { defineConfig } from 'vite' |
| 56 | +import vue from '@vitejs/plugin-vue' |
| 57 | + |
| 58 | +export default defineConfig({ |
| 59 | + plugins: [vue()], |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +- 新建index.html |
| 64 | + |
| 65 | +```html |
| 66 | +<!doctype html> |
| 67 | +<html lang="en"> |
| 68 | + <head> |
| 69 | + <meta charset="UTF-8" /> |
| 70 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 71 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 72 | + <title>play</title> |
| 73 | + </head> |
| 74 | + <body> |
| 75 | + <div id="app"></div> |
| 76 | + <script src="main.ts" type="module"></script> |
| 77 | + </body> |
| 78 | +</html> |
| 79 | +``` |
| 80 | + |
| 81 | +- 新建app.vue |
| 82 | + |
| 83 | +```vue |
| 84 | +<template> |
| 85 | + <div>启动测试</div> |
| 86 | +</template> |
| 87 | +``` |
| 88 | + |
| 89 | +- 新建入口文件 main.ts |
| 90 | + |
| 91 | +```ts |
| 92 | +import { createApp } from 'vue' |
| 93 | +import App from './app.vue' |
| 94 | + |
| 95 | +const app = createApp(App) |
| 96 | + |
| 97 | +app.mount('#app') |
| 98 | +``` |
| 99 | + |
| 100 | +- play 项目需要测试本地的组件库,所以也需要将 play 和我们的组件库关联在一起。修改一下pnpm-workspace.yaml文件 |
| 101 | + |
| 102 | +```yml |
| 103 | +packages: |
| 104 | + - 'packages/**' |
| 105 | + - 'play' |
| 106 | +``` |
| 107 | +
|
| 108 | +- package.json |
| 109 | +
|
| 110 | +```json |
| 111 | +{ |
| 112 | + "workspaces": ["packages/**", "play"] |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +- 新建一个声明文件vite.d.ts |
| 117 | + |
| 118 | +```ts |
| 119 | +declare module '*.vue' { |
| 120 | + import type { DefineComponent } from 'vue' |
| 121 | + const component: DefineComponent<{}, {}, any> |
| 122 | +} |
| 123 | +``` |
0 commit comments