@hap-toolkit/server 2.0.9-beta → 2.0.9-beta.2

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.
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
- # @hap-toolkit/server
2
-
3
- 快应用`web`服务模块
1
+ # @hap-toolkit/server
2
+
3
+ 快应用`web`服务模块
@@ -1,8 +1,8 @@
1
- /*
2
- * Copyright (c) 2021-present, the hapjs-platform Project Contributors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
-
6
- body {
7
- background-color: #ffffff;
1
+ /*
2
+ * Copyright (c) 2021-present, the hapjs-platform Project Contributors
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ body {
7
+ background-color: #ffffff;
8
8
  }
@@ -1,117 +1,117 @@
1
- /*
2
- * Copyright (c) 2021-present, the hapjs-platform Project Contributors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
-
6
- base = typeof base === 'undefined' ? '/preview' : base
7
- const eventsource = new EventSource('/preview/__stream')
8
-
9
- // 用以表示页面初始化,还没进行 watch 修改
10
- let isInitial = true
11
-
12
- const ProgressBar = (function (){
13
- class _ProgressBar {
14
- constructor() {
15
- this.basicText = {
16
- 'zh': '正在编译中...',
17
- 'en': 'Compiling...',
18
- }[getLan()]
19
-
20
- const $progressbar = document.createElement('p')
21
- $progressbar.id = 'progress-bar'
22
- document.body.appendChild($progressbar)
23
-
24
- $progressbar.style.color = {
25
- 'dark': 'rgba(255, 255, 255, 0.8)',
26
- 'light': 'rgba(0, 0, 0, 0.8)',
27
- }[getTheme()]
28
-
29
- this.progressbar = $progressbar
30
- }
31
- run(percentage) {
32
- this.progressbar.innerText = this.basicText + parseInt((Number(percentage) * 100).toString()) + '%'
33
- }
34
- }
35
- let instance = null
36
- return function() {
37
- if (!instance) {
38
- instance = new _ProgressBar()
39
- }
40
- return instance
41
- }
42
- })()
43
-
44
- function getLan() {
45
- return window.QUICKAPP_LANGUAGE || 'zh'
46
- }
47
-
48
- function getTheme() {
49
- return window.QUICKAPP_THEME || 'dark'
50
- }
51
-
52
- function showError(error) {
53
- if (typeof $mask === 'undefined') {
54
- $mask = document.createElement('div')
55
- $mask.id = 'mask'
56
- $mask.style.display = 'none'
57
- document.body.appendChild($mask)
58
-
59
- $btn = document.createElement('div')
60
- $btn.id = 'btn'
61
- $btn.innerHTML = '×'
62
- $btn.onclick = function() {
63
- $mask.style.display = 'none'
64
- }
65
- $mask.appendChild($btn)
66
-
67
- $text = document.createElement('pre')
68
- $text.id = 'text'
69
- $mask.appendChild($text)
70
- }
71
- $text.innerHTML = 'ERROR:\n' + error
72
- $mask.style.display = 'block'
73
- }
74
-
75
- eventsource.addEventListener('watch', page => {
76
- // webjs 会清空 body 下所有 dom,所以这里要新建
77
- const $loading = document.createElement('div')
78
- $loading.id = 'loading'
79
- $loading .classList.add(getLan(), getTheme())
80
- document.body.appendChild($loading)
81
- })
82
-
83
- eventsource.addEventListener('progress', page => {
84
- const { percentage } = JSON.parse(page.data)
85
- // 编译过程到 build 文件就会通知更新预览,即在过程 95% 时刷新
86
- // 更新后会有剩下 5% 的进度发过来,此时不需要显示进度了
87
- // 判断是从编译进度 0 开始,才显示进度
88
- if (isInitial && percentage === 0) {
89
- isInitial = false
90
- }
91
- if (isInitial) {
92
- return
93
- }
94
- const progress = new ProgressBar()
95
- progress.run(percentage)
96
- })
97
-
98
- eventsource.addEventListener('reload', ev => {
99
- // page.data: 快应用页面router page,空值为默认模式
100
- window.sessionStorage.removeItem('system.router.params')
101
- window.sessionStorage.removeItem('routes')
102
- const data = JSON.parse(ev.data)
103
- const error = unescape(data.error)
104
- const path = data.path
105
- if (error) {
106
- showError(error)
107
- return
108
- }
109
- if (path) {
110
- location.replace(`${base}/${path}`)
111
- } else {
112
- location.replace(base)
113
- }
114
- })
115
- eventsource.onerror = function(evt) {
116
- console.log('EventSource error', evt)
1
+ /*
2
+ * Copyright (c) 2021-present, the hapjs-platform Project Contributors
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ base = typeof base === 'undefined' ? '/preview' : base
7
+ const eventsource = new EventSource('/preview/__stream')
8
+
9
+ // 用以表示页面初始化,还没进行 watch 修改
10
+ let isInitial = true
11
+
12
+ const ProgressBar = (function (){
13
+ class _ProgressBar {
14
+ constructor() {
15
+ this.basicText = {
16
+ 'zh': '正在编译中...',
17
+ 'en': 'Compiling...',
18
+ }[getLan()]
19
+
20
+ const $progressbar = document.createElement('p')
21
+ $progressbar.id = 'progress-bar'
22
+ document.body.appendChild($progressbar)
23
+
24
+ $progressbar.style.color = {
25
+ 'dark': 'rgba(255, 255, 255, 0.8)',
26
+ 'light': 'rgba(0, 0, 0, 0.8)',
27
+ }[getTheme()]
28
+
29
+ this.progressbar = $progressbar
30
+ }
31
+ run(percentage) {
32
+ this.progressbar.innerText = this.basicText + parseInt((Number(percentage) * 100).toString()) + '%'
33
+ }
34
+ }
35
+ let instance = null
36
+ return function() {
37
+ if (!instance) {
38
+ instance = new _ProgressBar()
39
+ }
40
+ return instance
41
+ }
42
+ })()
43
+
44
+ function getLan() {
45
+ return window.QUICKAPP_LANGUAGE || 'zh'
46
+ }
47
+
48
+ function getTheme() {
49
+ return window.QUICKAPP_THEME || 'dark'
50
+ }
51
+
52
+ function showError(error) {
53
+ if (typeof $mask === 'undefined') {
54
+ $mask = document.createElement('div')
55
+ $mask.id = 'mask'
56
+ $mask.style.display = 'none'
57
+ document.body.appendChild($mask)
58
+
59
+ $btn = document.createElement('div')
60
+ $btn.id = 'btn'
61
+ $btn.innerHTML = '×'
62
+ $btn.onclick = function() {
63
+ $mask.style.display = 'none'
64
+ }
65
+ $mask.appendChild($btn)
66
+
67
+ $text = document.createElement('pre')
68
+ $text.id = 'text'
69
+ $mask.appendChild($text)
70
+ }
71
+ $text.innerHTML = 'ERROR:\n' + error
72
+ $mask.style.display = 'block'
73
+ }
74
+
75
+ eventsource.addEventListener('watch', page => {
76
+ // webjs 会清空 body 下所有 dom,所以这里要新建
77
+ const $loading = document.createElement('div')
78
+ $loading.id = 'loading'
79
+ $loading .classList.add(getLan(), getTheme())
80
+ document.body.appendChild($loading)
81
+ })
82
+
83
+ eventsource.addEventListener('progress', page => {
84
+ const { percentage } = JSON.parse(page.data)
85
+ // 编译过程到 build 文件就会通知更新预览,即在过程 95% 时刷新
86
+ // 更新后会有剩下 5% 的进度发过来,此时不需要显示进度了
87
+ // 判断是从编译进度 0 开始,才显示进度
88
+ if (isInitial && percentage === 0) {
89
+ isInitial = false
90
+ }
91
+ if (isInitial) {
92
+ return
93
+ }
94
+ const progress = new ProgressBar()
95
+ progress.run(percentage)
96
+ })
97
+
98
+ eventsource.addEventListener('reload', ev => {
99
+ // page.data: 快应用页面router page,空值为默认模式
100
+ window.sessionStorage.removeItem('system.router.params')
101
+ window.sessionStorage.removeItem('routes')
102
+ const data = JSON.parse(ev.data)
103
+ const error = unescape(data.error)
104
+ const path = data.path
105
+ if (error) {
106
+ showError(error)
107
+ return
108
+ }
109
+ if (path) {
110
+ location.replace(`${base}/${path}`)
111
+ } else {
112
+ location.replace(base)
113
+ }
114
+ })
115
+ eventsource.onerror = function(evt) {
116
+ console.log('EventSource error', evt)
117
117
  }