Skip to content

Commit a2462d1

Browse files
committed
适配vue3版本改造
1 parent 481686a commit a2462d1

File tree

8 files changed

+110
-334
lines changed

8 files changed

+110
-334
lines changed

README.md

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,41 @@
99
npm安装vue-web-terminal
1010

1111
```shell
12-
npm install vue-web-terminal --save
12+
// vue2安装
13+
npm install [email protected] --save
14+
15+
// vue3安装
16+
npm install [email protected] --save
1317
```
1418

1519
main.js中载入 Terminal 插件
1620

1721
```js
1822
import Terminal from 'vue-web-terminal'
1923

24+
// for vue2
2025
Vue.use(Terminal)
26+
27+
// for vue3
28+
const app = createApp(App)
29+
app.use(Terminal)
2130
```
2231

2332
使用示例
2433

2534
```vue
26-
2735
<template>
2836
<div id="app">
2937
<terminal name="my-terminal" @execCmd="onExecCmd"></terminal>
3038
</div>
3139
</template>
3240
3341
<script>
42+
import Terminal from "vue-web-terminal"
3443
3544
export default {
3645
name: 'App',
46+
components:{Terminal},
3747
methods: {
3848
onExecCmd(key, command, success, failed) {
3949
if (key === 'fail') {
@@ -115,7 +125,7 @@ terminal标签支持属性参数表
115125
| warnLogLimitEnable | 是否开启日志限制警告 | boolean | true |
116126
| auto-help | 是否打开命令行自动搜索提示功能 | boolean | true |
117127
| help-style | help自定义样式 | string | |
118-
| command-store | 自定义的命令行库,见[命令行定义格式](#命令行定义) | array | [内置命令行](#内置命令行) |
128+
| command-store | 自定义的命令库,见[命令定义格式](#命令定义) | array | [内置命令行](#内置命令行) |
119129
| command-store-sort | 命令行库排序 | function | function(a,b) {} |
120130

121131
## Select Events
@@ -133,14 +143,11 @@ terminal标签支持事件表
133143

134144
本插件提供了一些 Api 可以使用 Vue 主动向插件发起事件请求。
135145

136-
Terminal在 Vue 的prototype中定义了一个变量可以获取Terminal对象
137-
138146
```js
139-
this.$terminal
147+
import Terminal from "vue-web-terminal"
140148

141-
// or
142-
143-
Vue.prototype.$terminal
149+
// 获取api
150+
Terminal.$api
144151
```
145152

146153
### 向Terminal推送一条消息
@@ -153,22 +160,23 @@ let message = {
153160
content: 'This is a wanning message.'
154161
}
155162

156-
this.$terminal.pushMessage(name, message)
163+
Terminal.$api.pushMessage(name, message)
157164
```
158165

159166
### 修改上下文
160167

161168
比如当前输入行`$ /vue-web-terminal/tzfun > `*/vue-web-terminal/tzfun* 就是上下文,上下文文本可以由开发者自由设置 ,但是需使用`.sync`绑定一个变量
162169

163170
```vue
164-
165171
<template>
166172
<div id="app">
167173
<terminal name="my-terminal" :context.sync="context"></terminal>
168174
</div>
169175
</template>
170176
171177
<script>
178+
import Terminal from "vue-web-terminal"
179+
172180
export default {
173181
name: 'App',
174182
data() {
@@ -178,7 +186,7 @@ export default {
178186
},
179187
methods: {
180188
_updateContext(newCtx) {
181-
this.$terminal.updateContext("my-terminal", newCtx)
189+
Terminal.$api.updateContext("my-terminal", newCtx)
182190
}
183191
}
184192
}
@@ -344,22 +352,22 @@ execCmd(key, command, success)
344352
}
345353
```
346354

347-
## 命令行定义
355+
## 命令定义
348356

349-
如果开启了命令行帮助搜索功能,在实例化Terminal之前需要传入自定义命令行库,传入的命令行库为 N 个命令行的数组,以下是命令行格式定义规则
357+
如果开启了命令帮助搜索功能,在实例化Terminal之前需要传入自定义命令库,传入的命令库为 N 个命令的数组,以下是命令格式定义规则
350358

351-
| 参数 | 说明 | 类型 |
352-
|-------------|---------------------------|--------|
353-
| key | 命令行关键字,必填 | string |
354-
| title | 显示标题 | string |
355-
| group | 分组,可自定义,默认为 `local` | string |
356-
| usage | 使用方法 | string |
357-
| description | 详细描述 | string |
358-
| example | 使用示例,见[命令行示例格式](#命令行示例格式) | array |
359+
| 参数 | 说明 | 类型 |
360+
|-------------|-------------------------|--------|
361+
| key | 命令关键字,必填 | string |
362+
| title | 显示标题 | string |
363+
| group | 分组,可自定义,默认为 `local` | string |
364+
| usage | 使用方法 | string |
365+
| description | 详细描述 | string |
366+
| example | 使用示例,见[命令示例格式](#命令示例格式) | array |
359367

360-
### 命令行示例格式
368+
### 命令示例格式
361369

362-
示例格式比较简单,`des`为描述,`cmd`为具体的命令行,json格式如下:
370+
示例格式比较简单,`des`为描述,`cmd`为具体的命令,json格式如下:
363371

364372
```json
365373
[
@@ -374,7 +382,7 @@ execCmd(key, command, success)
374382
]
375383
```
376384

377-
### 内置命令行
385+
### 内置命令
378386

379387
Terminal默认内置有以下命令,且不可替代
380388

modify_lib.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
obj_name = 'vue-web-terminal'
4+
5+
if os.path.exists('lib/demo.html'):
6+
os.remove('lib/demo.html')
7+
print('removed demo.html')
8+
9+
if os.path.exists('lib/' + obj_name + '.common.js'):
10+
os.remove('lib/' + obj_name + '.common.js')
11+
print('removed ' + obj_name + 'common.js')
12+
13+
if os.path.exists('lib/' + obj_name + '.umd.js'):
14+
os.remove('lib/' + obj_name + '.umd.js')
15+
print('removed ' + obj_name + 'umd.js')
16+
17+
if os.path.exists('lib/' + obj_name + '.umd.min.js'):
18+
os.rename('lib/' + obj_name + '.umd.min.js', 'lib/' + obj_name + '.js')
19+
print('renamed js to ' + obj_name + '.js')

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"name": "vue-web-terminal",
3-
"version": "1.3.0",
3+
"version": "2.0.0",
4+
"description": "网页端命令行插件,适配vue3",
45
"private": false,
56
"scripts": {
67
"serve": "vue-cli-service serve",
7-
"build-cli": "vue-cli-service build --dest lib src/index.js",
8+
"build-cli": "vue-cli-service build --target lib --dest lib src/index.js && python modify_lib.py",
89
"build": "webpack --mode=production --config webpack.config.js",
910
"lint": "vue-cli-service lint"
1011
},
1112
"dependencies": {
1213
"object-sizeof": "^1.6.2",
13-
"vue-json-viewer": "^2.2.21"
14+
"vue-json-viewer": "^2.2.22"
1415
},
1516
"devDependencies": {
1617
"@vue/cli-plugin-babel": "~4.5.0",
@@ -26,7 +27,7 @@
2627
"vue": "^2.6.11",
2728
"vue-loader": "^15.9.8",
2829
"vue-template-compiler": "^2.6.11",
29-
"webpack": "^5.69.0",
30+
"webpack": "^4.46.0",
3031
"webpack-cli": "^4.9.2",
3132
"uglifyjs-webpack-plugin": "^2.1.3"
3233
},

src/Terminal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'vue-json-viewer/style.css'
21
import sizeof from 'object-sizeof'
32
import {_dateFormat, _isEmpty, _nonEmpty, _sleep} from "./Util.js";
43
import historyStore from "./HistoryStore.js";

src/Terminal.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@
164164
</template>
165165

166166
<script>
167-
import TerminalJs from './Terminal.js'
168167
import './css/style.css'
168+
import 'vue-json-viewer/style.css'
169+
import TerminalJs from './Terminal.js'
169170
170171
export default TerminalJs
171172

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import Terminal from './Terminal.vue'
22
import TerminalObj from './TerminalObj.js'
33
import JsonViewer from 'vue-json-viewer'
44

5-
let terminal = {}
6-
terminal.install = function (Vue, options) {
5+
Terminal.install = function (Vue, options) {
76
Vue.use(JsonViewer)
87
if (options != null) {
98
TerminalObj.setOptions(options)
109
}
11-
Vue.component(Terminal.name, Terminal)
10+
Terminal.$api = TerminalObj
1211
}
13-
export default terminal
12+
13+
export default Terminal

vue.config.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
const path = require("path");
21
const name = "vue-web-terminal"
2+
let path = require('path')
3+
34
module.exports = {
5+
publicPath: './',
6+
css: {
7+
extract: false
8+
},
9+
runtimeCompiler:true,
10+
productionSourceMap: false,
11+
filenameHashing: false,
412
configureWebpack: {
513
output: {
614
filename: `${name}.js`,
@@ -9,13 +17,11 @@ module.exports = {
917
umdNamedDefine: true
1018
}
1119
},
12-
css: {
13-
extract: {
14-
filename: `${name}.css`,
15-
chunkFilename: `${name}.chunk.css`
16-
}
17-
},
1820
chainWebpack: (config) => {
21+
config.plugins.delete('preload')
22+
config.plugins.delete('prefetch')
23+
config.plugins.delete('html')
24+
1925
config.module
2026
.rule("js")
2127
.include.add(path.resolve(__dirname, "packages"))
@@ -25,5 +31,15 @@ module.exports = {
2531
.tap((options) => {
2632
return options;
2733
});
28-
}
34+
35+
config.optimization
36+
.minimize(true)
37+
.minimizer('terser')
38+
.tap(args => {
39+
let {terserOptions} = args[0];
40+
terserOptions.compress.drop_console = false;
41+
terserOptions.compress.drop_debugger = true;
42+
return args
43+
});
44+
},
2945
};

0 commit comments

Comments
 (0)