Skip to content

Commit 35662dd

Browse files
author
waterfly
committed
Add other
1 parent 304fe6f commit 35662dd

17 files changed

+1024
-15
lines changed

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
doc/
2+
examples/
3+
packages/
4+
public/
5+
babel.config.js
6+
vue.config.js

README.md

Lines changed: 161 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,169 @@
1+
[toc]
2+
13
# VueComponentDemo
4+
25
[Vue CLI](https://cli.vuejs.org/zh/)创建Vue组件示例
36

47

58

6-
1. 下载Vue CLI,并创建工程
9+
## 创建步骤
10+
11+
### 下载Vue CLI,并创建工程
12+
13+
```
14+
// 1.瞎子啊 vue/cli
15+
npm install -g @vue/cli
16+
or
17+
yarn global add @vue/cli
18+
19+
// 2.使用Vue CLI创建工程
20+
vue create vue-component-demo
21+
22+
// 或使用图形化创建
23+
vue ui
24+
```
25+
26+
### 修改目录结构
27+
28+
* 修改`scr`目录为`examples`,用来做Demo
29+
* 增加 `packages`目录,存放组件源码
30+
31+
### 创建`vue.config.js`
32+
33+
添加以下配置支持`packages`目录编译
34+
35+
```
36+
module.exports = {
37+
// 修改src目录为examples目录
38+
pages: {
39+
index: {
40+
entry: "examples/main.js",
41+
template: "public/index.html",
42+
filename: "index.html",
43+
},
44+
},
45+
// 扩展webpack配置,使packages加入编译
46+
chainWebpack: (config) => {
47+
config.module
48+
.rule("js")
49+
.include.add("/packages")
50+
.end()
51+
.use("babel")
52+
.loader("babel-loader")
53+
.tap((options) => options);
54+
},
55+
productionSourceMap: true,
56+
};
57+
58+
```
59+
60+
### `packages`目录下编写组件
61+
62+
添加组件`HelloVueComponent.vue``index.js`,在`index.js`里实现`HelloVueComponent`的install方法
63+
64+
```javascript
65+
import HelloVueComponent from "./HelloVueComponent.vue";
66+
67+
HelloVueComponent.install = function (Vue) {
68+
Vue.component(HelloVueComponent.name, HelloVueComponent);
69+
};
70+
71+
export default HelloVueComponent;
72+
73+
```
74+
75+
### 增加打包命令
76+
77+
`package.json`文件里增加组件打包命令`lib`
78+
79+
```javascript
80+
"scripts": {
81+
"serve": "vue-cli-service serve",
82+
"build": "vue-cli-service build",
83+
"lib": "vue-cli-service build --target lib --name vue-component-demo --dest lib packages/index.js",
84+
"lint": "vue-cli-service lint"
85+
},
86+
```
87+
88+
89+
90+
### 配置json文件参数
91+
92+
`package.json`增加配置,增加以下字段:
93+
94+
* description,库作用描述
95+
* keywords,关键字,用于搜索
96+
* license,证书
97+
* author,作者
98+
* repository,仓库说明
99+
* homepage,主页
100+
* main,组件库的主入口地址
101+
102+
```javascript
103+
// 示例
104+
"description": "Vue组件Demo",
105+
"keywords": [
106+
"Vue",
107+
"组件",
108+
"Demo"
109+
],
110+
"license": "ISC",
111+
"author": {
112+
"name": "waterFly",
113+
"email": "[email protected]"
114+
},
115+
"repository": {
116+
"type": "git",
117+
"url": "[email protected]:waterfly/vue-component-demo.git"
118+
},
119+
"homepage": "https://github.com/waterfly/vue-component-demo",
120+
"main": "lib/vue-component-demo.umd.min.js",
121+
```
122+
123+
注:详见[package.json](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#homepage)文件字段解释
124+
125+
### 增加`.npmignore`文件
126+
127+
`.npmignore`用于过滤不需要发布的文件或文件夹。
128+
129+
```
130+
doc/
131+
examples/
132+
packages/
133+
public/
134+
babel.config.js
135+
vue.config.js
136+
```
137+
138+
过滤文件有三种方式:
139+
140+
1. 设置`.gitignore`文件,在`git``npm publish`都会被过滤
141+
2. 设置`.npmignore`文件,在`npm publish`会被过滤
142+
3. 设置`package.json`文件的`files`字段,用来设置发布那些文件或目录
143+
144+
优先级: `files` > `.npmignore` > `.gitignore`
145+
146+
147+
148+
### 发布
149+
150+
```shell
151+
// 1. 编译
152+
yarn lib
153+
154+
// 2. 登录
155+
yarn login
156+
157+
// 3. 发布
158+
yarn publish
159+
160+
```
161+
162+
7163

8-
```
9-
// 1.瞎子啊 vue/cli
10-
npm install -g @vue/cli
11-
or
12-
yarn global add @vue/cli
13-
14-
// 2.使用Vue CLI创建工程
15-
vue create vue-component-demo
16-
17-
// 或使用图形化创建
18-
vue ui
19-
```
164+
## 附录
20165

21-
166+
* [Vue CLI](https://cli.vuejs.org/zh/)
167+
* [package.json](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#homepage)
168+
* [从零到一教你基于vue开发一个组件库](https://juejin.cn/post/6844904085808742407)
22169

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

lib/demo.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<meta charset="utf-8">
2+
<title>vue-component-demo demo</title>
3+
<script src="./vue-component-demo.umd.js"></script>
4+
5+
<link rel="stylesheet" href="./vue-component-demo.css">
6+
7+
8+
<script>
9+
console.log(vue-component-demo)
10+
</script>

0 commit comments

Comments
 (0)