t20-common-lib 0.11.4 → 0.12.0

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.
@@ -0,0 +1,53 @@
1
+ /* 引入不被打包的配置文件 */
2
+ import stripJsonc from 'strip-json-comments'
3
+ import realUrl from './realUrl'
4
+ /* 前端(内存)缓存 */
5
+ const jsoncs = {}
6
+
7
+ export default function getJsonc(url, sync,rest) {
8
+ const _url = realUrl(url, rest)
9
+ if (sync) {
10
+ if (jsoncs[url + '?sync']) {
11
+ return jsoncs[url + '?sync']
12
+ } else {
13
+ const oAjax = new XMLHttpRequest()
14
+ oAjax.open('GET', _url, false)
15
+ oAjax.send()
16
+ if (oAjax.readyState === 4 && oAjax.status === 200) {
17
+ try {
18
+ const _data = JSON.parse(stripJsonc(oAjax.responseText))
19
+ jsoncs[url + '?sync'] = _data
20
+ jsoncs[url] = Promise.resolve(_data)
21
+ } catch (error) {
22
+ throw { error: url + ' illegal file', content: oAjax.responseText }
23
+ }
24
+ }
25
+ return jsoncs[url + '?sync']
26
+ }
27
+ } else {
28
+ if (jsoncs[url]) {
29
+ return jsoncs[url]
30
+ } else {
31
+ const oAjax = new XMLHttpRequest()
32
+ oAjax.open('GET', _url, true)
33
+ oAjax.send()
34
+ jsoncs[url] = new Promise((resolve, reject) => {
35
+ oAjax.onreadystatechange = () => {
36
+ if (oAjax.readyState === 4 && oAjax.status === 200) {
37
+ try {
38
+ const _data = JSON.parse(stripJsonc(oAjax.responseText))
39
+ jsoncs[url + '?sync'] = _data
40
+ resolve(_data)
41
+ } catch (error) {
42
+ throw {
43
+ error: url + ' illegal file',
44
+ content: oAjax.responseText
45
+ }
46
+ }
47
+ }
48
+ }
49
+ })
50
+ return jsoncs[url]
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,14 @@
1
+ /* 前缀,针对乾坤做了处理 */
2
+ const prefix = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || process.env.BASE_URL || '/'
3
+
4
+ export default function realUrl(url, rest = false) {
5
+ if (/^http(s)?:|\/\//.test(url)) {
6
+ return url
7
+ } else if (/^\//.test(url)) {
8
+ return prefix + url.replace(/^\//, '')
9
+ } else if (rest) {
10
+ return '/' + url
11
+ } else {
12
+ return url
13
+ }
14
+ }