@ray-js/ray 1.3.0-beta.2-beta-2 → 1.3.0-beta.3

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/index.d.ts CHANGED
@@ -5,4 +5,3 @@ export { router as nativeRouter } from '@ray-js/api'
5
5
  export { default as location } from '@ray-js/location'
6
6
  export { default as router } from '@ray-js/router'
7
7
  export { default as platform } from '@ray-js/env'
8
- export * from '@ray-js/capability'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/ray",
3
- "version": "1.3.0-beta.2-beta-2",
3
+ "version": "1.3.0-beta.3",
4
4
  "description": "ray",
5
5
  "keywords": [
6
6
  "ray"
@@ -15,18 +15,19 @@
15
15
  "unified.mini.js",
16
16
  "main",
17
17
  "macro.js",
18
- "macro.d.ts"
18
+ "macro.d.ts",
19
+ "plugins"
19
20
  ],
20
21
  "publishConfig": {
21
22
  "access": "public",
22
23
  "registry": "https://registry.npmjs.org"
23
24
  },
24
25
  "dependencies": {
25
- "@ray-core/macro": "^0.2.6",
26
- "@ray-js/api": "^1.3.0-beta.2-beta-2",
27
- "@ray-js/capability": "^1.3.0-beta.2-beta-2",
28
- "@ray-js/components": "^1.3.0-beta.2-beta-2",
29
- "@ray-js/framework": "^1.3.0-beta.2-beta-2"
26
+ "@ray-core/html2mini-plugin": "0.0.6",
27
+ "@ray-core/macro": "^0.3.0-beta.4",
28
+ "@ray-js/api": "^1.3.0-beta.3",
29
+ "@ray-js/components": "^1.3.0-beta.3",
30
+ "@ray-js/framework": "^1.3.0-beta.3"
30
31
  },
31
32
  "maintainers": [
32
33
  {
@@ -34,6 +35,6 @@
34
35
  "ezmail": "tuyafe@tuya.com"
35
36
  }
36
37
  ],
37
- "gitHead": "067ce2e382196cd995ebc4774fc5e977ac21115c",
38
+ "gitHead": "be72a5dad6214ebf9c6653df02956374c735c98e",
38
39
  "repository": {}
39
40
  }
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ name: 'html2mini-plugin',
3
+ registerRuntimePlugin() {
4
+ return require.resolve('@ray-core/html2mini-plugin')
5
+ },
6
+ }
@@ -0,0 +1,140 @@
1
+ /**
2
+ * i18n plugin
3
+ *
4
+ * 用于将语言包从项目 src/i18n 目录 或 node_modules/@ray-js/i18n 目录
5
+ * 且该目录下的以 strings.{js|json|ts} 结尾的文件转换为 strings.json
6
+ * 并 strings.json 提取到构建目录的 i18n 目录中
7
+ *
8
+ */
9
+
10
+ const pluginName = 'i18n-plugin'
11
+ const path = require('path')
12
+ const fs = require('fs-extra')
13
+ const { requireJSFile } = require('@ray-js/shared')
14
+
15
+ const exts = ['json', 'js', 'ts']
16
+ const names = exts.map((ext) => `strings.${ext}`)
17
+
18
+ const throttle = function (fn, timeout) {
19
+ let id
20
+ return function (...args) {
21
+ clearTimeout(id)
22
+ id = setTimeout(() => fn(...args), timeout)
23
+ }
24
+ }
25
+
26
+ const i18nPkgBuilderCreator = function (root, outputFile) {
27
+ const filenames = names.map((item) => path.join(root, item))
28
+ let cache
29
+ const handle = (name) => {
30
+ if (!fs.existsSync(root)) {
31
+ return
32
+ }
33
+
34
+ let filename
35
+ // 因 fs.watch 无法区别文件删除和文件重命名
36
+ // 所以只能通过判断文件是否存在
37
+
38
+ // 更新时name不为空
39
+ if (name) {
40
+ filename = path.join(root, name)
41
+ // 文件不存在,可能是删除操作
42
+ if (!fs.existsSync(filename)) {
43
+ delete require.cache[filename]
44
+ cache && (cache = null)
45
+ return
46
+ }
47
+ }
48
+
49
+ const files = filenames.filter((item) => fs.existsSync(item))
50
+
51
+ if (files.length > 1) {
52
+ console.warn(`${pluginName}: Has more than one file: ${files.join('\n')}`)
53
+ console.warn(`${pluginName}: Will use first file: ${files[0]}`)
54
+ }
55
+
56
+ if (!filename) {
57
+ // 初始化时filename为空
58
+ if (files.length) {
59
+ filename = files[0] // 取第一个文件
60
+ } else {
61
+ return
62
+ }
63
+ }
64
+
65
+ // 记住上一次使用的语言包文件
66
+ if ((!cache || cache !== filename) && filenames.includes(filename)) {
67
+ cache = filename
68
+ }
69
+ delete require.cache[filename]
70
+ const data = requireJSFile(cache)
71
+
72
+ if (data) {
73
+ fs.outputFile(outputFile, JSON.stringify(data, null, 2), 'utf-8')
74
+ }
75
+ }
76
+ handle.clear = function () {
77
+ cache = null
78
+ }
79
+ return handle
80
+ }
81
+
82
+ const i18nHandleCreator = (root, outputFile) => {
83
+ let i18nDirWatcher
84
+ const i18nDirName = path.join(root, 'i18n')
85
+ const i18nPkgBuilder = i18nPkgBuilderCreator(i18nDirName, outputFile)
86
+ const throttledI18nPkgBuilder = throttle(i18nPkgBuilder, 50)
87
+
88
+ const i18nDirHandle = (name) => {
89
+ if (!i18nDirWatcher && name !== 'i18n') {
90
+ return
91
+ }
92
+
93
+ const exist = fs.existsSync(i18nDirName)
94
+
95
+ // 说明i18n目录被删除了,关闭监听
96
+ if (i18nDirWatcher && !exist) {
97
+ i18nDirWatcher.close()
98
+ i18nDirWatcher = null
99
+ i18nPkgBuilder.clear()
100
+ return
101
+ }
102
+ // i18n 目录删除后又创建了,需要重新监听之
103
+ if (!i18nDirWatcher && exist) {
104
+ i18nDirWatcher = fs.watch(i18nDirName, (_, name) => throttledI18nPkgBuilder(name))
105
+ }
106
+ }
107
+
108
+ const throttledI18nDirHandle = throttle(i18nDirHandle, 50)
109
+
110
+ return {
111
+ i18nWatcherHandle: function () {
112
+ // 初始化时判断是否存在 i18n 目录,若存在则监听之
113
+ const exist = fs.existsSync(i18nDirName)
114
+ if (exist) {
115
+ i18nDirWatcher = fs.watch(i18nDirName, (_, name) => throttledI18nPkgBuilder(name))
116
+ }
117
+ // 监听 root(即src目录),用于动态判断 i18n 目录是否存在,若存在则监听之
118
+ fs.watch(root, (_, name) => throttledI18nDirHandle(name))
119
+ },
120
+ i18nPkgBuilder,
121
+ }
122
+ }
123
+
124
+ module.exports = (ctx) => {
125
+ const { options } = ctx
126
+ const { cwd, source, output, watch } = options
127
+ const i18nDistPath = path.join(output, 'i18n', 'strings.json')
128
+ const srcDir = path.join(cwd, source)
129
+ const { i18nWatcherHandle, i18nPkgBuilder } = i18nHandleCreator(srcDir, i18nDistPath)
130
+
131
+ return {
132
+ name: pluginName,
133
+ setup() {
134
+ i18nPkgBuilder()
135
+ if (watch) {
136
+ i18nWatcherHandle()
137
+ }
138
+ },
139
+ }
140
+ }
package/unified.js CHANGED
@@ -17,4 +17,3 @@ export { router as nativeRouter } from '@ray-js/api'
17
17
  export { default as router } from '@ray-js/router'
18
18
  export { default as location } from '@ray-js/location'
19
19
  export { default as platform } from '@ray-js/env'
20
- export * from '@ray-js/capability'
package/unified.mini.js CHANGED
@@ -5,4 +5,3 @@ export { router as nativeRouter } from '@ray-js/api'
5
5
  export { default as router } from '@ray-js/router'
6
6
  export { default as location } from '@ray-js/location'
7
7
  export { default as platform } from '@ray-js/env'
8
- export * from '@ray-js/capability'