@bowenyao/my-component 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +285 -0
  2. package/package.json +37 -0
package/README.md ADDED
@@ -0,0 +1,285 @@
1
+ # My Component
2
+
3
+ 这是一个基于 Vue 3 + Vite 开发的可复用公共组件库。
4
+
5
+ ## ✨ 特性
6
+
7
+ - 🎨 支持 Vue 3 Composition API
8
+ - 📦 多种打包格式(ESM, CJS, UMD, IIFE)
9
+ - 🎯 开箱即用的插件式安装
10
+ - 🌐 支持通过 npm、CDN 等多种方式引入
11
+ - 💅 支持样式独立导入
12
+ - 🔧 基于 Vite 构建工具
13
+
14
+ ## 📦 安装
15
+
16
+ ### npm 安装
17
+
18
+ ```bash
19
+ npm install @cery/my-component
20
+ ```
21
+
22
+ ### yarn 安装
23
+
24
+ ```bash
25
+ yarn add @cery/my-component
26
+ ```
27
+
28
+ ### pnpm 安装
29
+
30
+ ```bash
31
+ pnpm add @cery/my-component
32
+ ```
33
+
34
+ ## 🚀 使用方法
35
+
36
+ ### 方式一:完整安装(推荐)
37
+
38
+ ```javascript
39
+ import { createApp } from 'vue'
40
+ import App from './App.vue'
41
+ import MyComponent from '@cery/my-component'
42
+
43
+ const app = createApp(App)
44
+ app.use(MyComponent)
45
+ app.mount('#app')
46
+ ```
47
+
48
+ 在模板中使用:
49
+
50
+ ```vue
51
+ <template>
52
+ <MyComponent />
53
+ </template>
54
+ ```
55
+
56
+ ### 方式二:按需引入单个组件
57
+
58
+ ```javascript
59
+ import { MyComponent } from '@cery/my-component'
60
+
61
+ export default {
62
+ components: {
63
+ MyComponent
64
+ }
65
+ }
66
+ ```
67
+
68
+ ### 方式三:通过 CDN 使用(适用于非构建项目)
69
+
70
+ ```html
71
+ <!DOCTYPE html>
72
+ <html>
73
+ <head>
74
+ <meta charset="UTF-8">
75
+ <title>MyComponent Demo</title>
76
+ <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
77
+ <script src="https://unpkg.com/@cery/my-component/dist/umd/index.umd.js"></script>
78
+ <link rel="stylesheet" href="https://unpkg.com/@cery/my-component/dist/index.css">
79
+ </head>
80
+ <body>
81
+ <div id="app">
82
+ <MyComponent />
83
+ </div>
84
+ <script>
85
+ const { createApp } = Vue
86
+ const { MyComponent } = window.MyComponent
87
+
88
+ createApp().use(MyComponent).mount('#app')
89
+ </script>
90
+ </body>
91
+ </html>
92
+ ```
93
+
94
+ ### 引入样式
95
+
96
+ 如果组件有独立样式,可以单独引入:
97
+
98
+ ```javascript
99
+ import '@cery/my-component/dist/style'
100
+ ```
101
+
102
+
103
+
104
+ ```javascript
105
+ import '@cery/my-component/dist/index.css'
106
+ ```
107
+
108
+ ## 🏗️ 打包说明
109
+
110
+ ### 打包命令
111
+
112
+ ```bash
113
+ # 开发模式
114
+ pnpm dev
115
+
116
+ # 生产构建
117
+ pnpm build
118
+
119
+ # 预览构建结果
120
+ pnpm preview
121
+ ```
122
+
123
+ ### 打包配置说明
124
+
125
+ 本项目使用 Vite 进行打包,支持以下格式:
126
+
127
+ - **ESM (ES Module)**: `dist/es/index.es.js` - 用于现代打包工具(webpack, rollup, vite)
128
+ - **CJS (CommonJS)**: `dist/cjs/index.cjs.js` - 用于 Node.js 环境
129
+ - **UMD**: `dist/umd/index.umd.js` - 通用格式,支持 AMD、CommonJS 和全局变量
130
+ - **IIFE**: `dist/iife/index.iife.js` - 立即执行函数,用于浏览器直接引用
131
+
132
+ ### package.json 字段说明
133
+
134
+ ```json
135
+ {
136
+ "name": "@cery/my-component", // 包名
137
+ "version": "0.0.6", // 版本号
138
+ "type": "module", // 模块类型
139
+ "main": "./dist/cjs/index.cjs.js", // CommonJS 入口
140
+ "module": "./dist/es/index.es.js", // ESM 入口
141
+ "unpkg": "./dist/umd/index.umd.js", // unpkg CDN 入口
142
+ "jsdelivr": "./dist/umd/index.umd.js", // jsdelivr CDN 入口
143
+ "exports": { // 导出配置
144
+ ".": {
145
+ "script": "./dist/umd/index.umd.js",
146
+ "import": "./dist/es/index.es.js",
147
+ "require": "./dist/cjs/index.cjs.js"
148
+ },
149
+ "./style": "./dist/index.css" // 样式导出
150
+ },
151
+ "files": ["dist"], // 发布到 npm 的文件
152
+ "publishConfig": {
153
+ "access": "public" // 公开包
154
+ }
155
+ }
156
+ ```
157
+
158
+ ### Vite 配置关键点
159
+
160
+ 1. **多格式打包**: 通过 `lib.formats` 配置输出多种格式
161
+ 2. **外部依赖**: 将 `vue` 和 `element-plus` 标记为外部依赖,避免重复打包
162
+ 3. **静态资源复制**: 使用 `vite-plugin-static-copy` 复制 package.json 和 README.md
163
+ 4. **CSS 独立输出**: 样式文件输出到 `dist/index.css`
164
+
165
+ ## 📤 发布到 npm
166
+
167
+ ### 1. 准备工作
168
+
169
+ 确保你有 npm 账号,如果没有请先注册:
170
+ ```bash
171
+ npm adduser
172
+ ```
173
+
174
+ ### 2. 登录 npm
175
+
176
+ ```bash
177
+ npm login
178
+ ```
179
+
180
+ ### 3. 构建项目
181
+
182
+ ```bash
183
+ pnpm build
184
+ ```
185
+
186
+ ### 4. 发布
187
+
188
+ ```bash
189
+ # 正式发布
190
+ npm publish
191
+
192
+ # 或使用 pnpm
193
+ pnpm publish
194
+ ```
195
+
196
+ ### 5. 更新版本号
197
+
198
+ 修改 `package.json` 中的 `version` 字段,或使用命令:
199
+
200
+ ```bash
201
+ # 补丁版本(0.0.x)
202
+ npm version patch
203
+
204
+ # 次版本(0.x.0)
205
+ npm version minor
206
+
207
+ # 主版本(x.0.0)
208
+ npm version major
209
+ ```
210
+
211
+ ### 6. 发布作用域包
212
+
213
+ 由于包名是 `@cery/my-component`(作用域包),确保:
214
+
215
+ 1. `publishConfig.access` 设置为 `"public"`
216
+ 2. 或者创建私有作用域:`npm publish --access restricted`
217
+
218
+ ## 📁 目录结构
219
+
220
+ ```
221
+ my-component/
222
+ ├── src/ # 源代码目录
223
+ │ ├── components/ # 组件目录
224
+ │ │ └── MyComponent.vue
225
+ │ ├── index.js # 入口文件
226
+ │ ├── App.vue # 示例应用
227
+ │ └── main.js # 开发入口
228
+ ├── my-component/ # 打包输出目录
229
+ │ ├── dist/ # 构建产物
230
+ │ │ ├── es/ # ESM 格式
231
+ │ │ ├── cjs/ # CommonJS 格式
232
+ │ │ ├── umd/ # UMD 格式
233
+ │ │ ├── iife/ # IIFE 格式
234
+ │ │ └── index.css # 样式文件
235
+ │ ├── package.json # 发布用的 package.json
236
+ │ └── README.md # 说明文档
237
+ ├── index.html # 开发 HTML
238
+ ├── vite.config.js # Vite 配置文件
239
+ ├── package.json # 项目配置
240
+ └── README.md # 项目说明
241
+ ```
242
+
243
+ ## 🔧 开发指南
244
+
245
+ ### 组件开发规范
246
+
247
+ 1. **入口文件** (`src/index.js`):
248
+
249
+ ```javascript
250
+ import MyComponent from './components/MyComponent.vue'
251
+
252
+ // 导出一个带有 install 方法的对象
253
+ const plugin = {
254
+ install(app) {
255
+ // 注册组件名称
256
+ app.component('MyComponent', MyComponent)
257
+ }
258
+ }
259
+
260
+ // 支持单独导入组件
261
+ plugin.MyComponent = MyComponent
262
+
263
+ export default plugin
264
+ ```
265
+
266
+ 2. **组件开发**: 在 `src/components/` 目录下开发 Vue 组件
267
+
268
+ ### 添加新组件
269
+
270
+ 1. 在 `src/components/` 创建新组件
271
+ 2. 在 `src/index.js` 中注册新组件
272
+ 3. 更新 `vite.config.js` 中的 `build.lib.name`
273
+ 4. 重新构建并发布
274
+
275
+ ## 📝 注意事项
276
+
277
+ 1. **版本管理**: 遵循语义化版本规范
278
+ 2. **依赖声明**: 只在 `dependencies` 中声明运行时依赖
279
+ 3. **外部依赖**: 将 `vue`、`element-plus` 等核心库标记为外部依赖
280
+ 4. **样式隔离**: 确保组件样式不会污染全局环境
281
+ 5. **文档更新**: 每次发布更新 README.md 和版本说明
282
+
283
+ ## 📄 License
284
+
285
+ MIT
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@bowenyao/my-component",
3
+ "version": "0.0.6",
4
+ "type": "module",
5
+ "main": "./dist/cjs/index.cjs.js",
6
+ "module": "./dist/es/index.es.js",
7
+ "unpkg": "./dist/umd/index.umd.js",
8
+ "jsdelivr": "./dist/umd/index.umd.js",
9
+ "exports": {
10
+ ".": {
11
+ "script": "./dist/umd/index.umd.js",
12
+ "import": "./dist/es/index.es.js",
13
+ "require": "./dist/cjs/index.cjs.js"
14
+ },
15
+ "./style": "./dist/index.css"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "dev": "vite",
22
+ "build": "vite build",
23
+ "preview": "vite preview"
24
+ },
25
+ "devDependencies": {
26
+ "@vitejs/plugin-vue": "^6.0.4",
27
+ "vite": "^7.3.1",
28
+ "vite-plugin-static-copy": "^3.2.0"
29
+ },
30
+ "dependencies": {
31
+ "element-plus": "^2.13.2",
32
+ "vue": "^3.5.29"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ }
37
+ }