@ray-js/build-plugin-router 0.6.22-beta-1 → 0.6.23

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.
Files changed (2) hide show
  1. package/lib/index.js +90 -69
  2. package/package.json +4 -4
package/lib/index.js CHANGED
@@ -72,79 +72,99 @@ const standardAppConfigFieldMap = [
72
72
  'backgroundColor',
73
73
  ];
74
74
  function normalizeTabBarConfig(config, routes, fields) {
75
- const _a = config || {}, { list } = _a, rest = __rest(_a, ["list"]);
75
+ let _a = config || {}, { list = [] } = _a, rest = __rest(_a, ["list"]);
76
76
  let cfg;
77
- if (list && list.length) {
78
- const _cfg = {};
79
- // tabBar.list[number].pagePath 必须在 routes[number].page 中,即tab.pagePath === routes[number].page
80
- // tabBar.list[number].route 可不配置
81
- // tabBar.list[number].route 若未配置,则设置 tabBar.list[number].route = routes[number].page(非模式匹配)或tabBar.list[number].pagePath
82
- // tabBar.list[number].route 不能用模式,如不能用/xxx/:id等,必须是明确的 如/xxx/123
83
- // tabBar.list[number].route 必须要能命中对应routes[number].route 路由规则,规则详见 path-to-regexp 模块
84
- const tabs = list
85
- .map((tab, index) => {
86
- var _a;
87
- const m = routes.find((i) => tab.pagePath === i.path);
88
- if (!m) {
89
- printError(`tabBar.list[${index}].pagePath: ${tab.pagePath} 需要在路由配置中: \n${JSON.stringify(routes, null, 2)}`.red);
90
- return;
91
- }
92
- let pagePath = tab.pagePath.replace(/^\//, '');
93
- const urlObj = url_1.default.parse(pagePath);
94
- if (urlObj.hash || urlObj.search) {
95
- printError(`tabBar.list[${index}]: ${tab.pagePath} 不能携带参数`.red);
96
- return;
97
- }
98
- pagePath = urlObj.pathname;
99
- if (!tab.route) {
100
- // 判断 routes[number].route 是否为模式匹配规则
101
- tab.route = m.route && (0, path_to_regexp_1.parse)(m.route).length === 1 ? m.route : m.path;
102
- }
103
- // 判断是否能被路由命中
104
- const matched = (0, path_to_regexp_1.match)((_a = m.route) !== null && _a !== void 0 ? _a : m.path)(tab.route);
105
- if (!matched) {
106
- printError(`tabBar.list[${index}]\n.route: ${tab.route}\n.pagePath: ${tab.pagePath}\n无法被路由命中: \n${JSON.stringify(m, null, 2)}`.red);
107
- return;
108
- }
109
- // 判断是否还被其他路由命中
110
- const otherMatched = routes
111
- .filter((r) => tab.pagePath !== r.path)
112
- .filter((r) => { var _a; return (0, path_to_regexp_1.match)((_a = r.route) !== null && _a !== void 0 ? _a : r.path)(tab.route); });
113
- const unusable = otherMatched.filter((r) => r.path !== tab.pagePath);
114
- if (unusable.length) {
115
- printError(`tabBar.list[${index}]\n.route: ${tab.route}\n.pagePath: ${tab.pagePath}\n应被路由命中:\n${JSON.stringify(m, null, 2)}\n但同时又被其它路由命中:\n${JSON.stringify(unusable, null, 2)}`.red);
116
- return;
77
+ if (!lodash_1.default.isArray(list)) {
78
+ printError(`tabBar.list 数据结构不对`.red);
79
+ return cfg;
80
+ }
81
+ list = list.map((tab) => {
82
+ for (const r of routes) {
83
+ // 兼容老项目
84
+ if (tab.id === r.id) {
85
+ return Object.assign(Object.assign({}, tab), { pagePath: r.path });
117
86
  }
118
- return {
119
- pagePath,
120
- text: tab.text,
121
- iconPath: tab.icon,
122
- selectedIconPath: tab.activeIcon,
123
- };
124
- })
125
- .filter((tab) => !!tab);
126
- if (tabs.length < 2 || tabs.length > 5) {
127
- printError(`tabBar.list 最少 2 个、最多 5 个 tab`.red);
128
- return cfg;
129
87
  }
130
- _cfg.list = tabs;
131
- // 找出 tabBar.list[number].pagePath 重复的配置
132
- const _list = duplicates(tabs, (a, b) => (a.pagePath > b.pagePath ? 1 : a.pagePath == b.pagePath ? 0 : -1), 'pagePath');
133
- if (_list.length) {
134
- console.error(LOG_PREFIX, `tabBar.list不能有重复的页面: \n${JSON.stringify(_list, null, 2)}`.red);
88
+ return tab;
89
+ });
90
+ const _cfg = {};
91
+ // tabBar.list[number].pagePath 必须在 routes[number].page 中,即tab.pagePath === routes[number].page
92
+ // tabBar.list[number].route 可不配置
93
+ // tabBar.list[number].route 若未配置,则设置 tabBar.list[number].route = routes[number].page(非模式匹配)或tabBar.list[number].pagePath
94
+ // tabBar.list[number].route 不能用模式,如不能用/xxx/:id等,必须是明确的 如/xxx/123
95
+ // tabBar.list[number].route 必须要能命中对应routes[number].route 路由规则,规则详见 path-to-regexp 模块
96
+ const tabs = list
97
+ .map((tab, index) => {
98
+ var _a;
99
+ const m = routes.find((i) => {
100
+ return (tab.pagePath === i.path ||
101
+ // 兼容老项目
102
+ tab.id === i.id);
103
+ });
104
+ if (!m) {
105
+ printError(`tabBar.list[${index}].pagePath: ${tab.pagePath} 需要在路由配置中: \n${JSON.stringify(routes, null, 2)}`.red);
106
+ return;
135
107
  }
136
- cfg = fields.reduce((o, next) => {
137
- const [src, t] = typeof next === 'string' ? [next] : next;
138
- if (rest[src]) {
139
- o[t || src] = rest[src];
140
- }
141
- return o;
142
- }, _cfg);
108
+ let pagePath = tab.pagePath.replace(/^\//, '');
109
+ const urlObj = url_1.default.parse(pagePath);
110
+ if (urlObj.hash || urlObj.search) {
111
+ printError(`tabBar.list[${index}]: ${tab.pagePath} 不能携带参数`.red);
112
+ return;
113
+ }
114
+ pagePath = urlObj.pathname;
115
+ if (!tab.route) {
116
+ // 判断 routes[number].route 是否为模式匹配规则
117
+ tab.route = m.route && (0, path_to_regexp_1.parse)(m.route).length === 1 ? m.route : m.path;
118
+ }
119
+ // 判断是否能被路由命中
120
+ const matched = (0, path_to_regexp_1.match)((_a = m.route) !== null && _a !== void 0 ? _a : m.path)(tab.route);
121
+ if (!matched) {
122
+ printError(`tabBar.list[${index}]\n.route: ${tab.route}\n.pagePath: ${tab.pagePath}\n无法被路由命中: \n${JSON.stringify(m, null, 2)}`.red);
123
+ return;
124
+ }
125
+ // 判断是否还被其他路由命中
126
+ const otherMatched = routes
127
+ .filter((r) => tab.pagePath !== r.path)
128
+ .filter((r) => { var _a; return (0, path_to_regexp_1.match)((_a = r.route) !== null && _a !== void 0 ? _a : r.path)(tab.route); });
129
+ const unusable = otherMatched.filter((r) => r.path !== tab.pagePath);
130
+ if (unusable.length) {
131
+ printError(`tabBar.list[${index}]\n.route: ${tab.route}\n.pagePath: ${tab.pagePath}\n应被路由命中:\n${JSON.stringify(m, null, 2)}\n但同时又被其它路由命中:\n${JSON.stringify(unusable, null, 2)}`.red);
132
+ return;
133
+ }
134
+ // tab中配置的页面.route 匹配的页面为模式匹配,需给出告警
135
+ if (!lodash_1.default.isEmpty(matched.params)) {
136
+ console.warn(LOG_PREFIX, `tabBar.list[${index}].route: ${tab.route} 命中的路由 \n${JSON.stringify(m, null, 2)}.route: ${m.route} 为模式匹配\ntab页不能携带参数`.yellow);
137
+ }
138
+ return {
139
+ pagePath,
140
+ text: tab.text,
141
+ iconPath: tab.icon,
142
+ selectedIconPath: tab.activeIcon,
143
+ };
144
+ })
145
+ .filter((tab) => !!tab);
146
+ if (tabs.length < 2 || tabs.length > 5) {
147
+ printError(`tabBar.list 最少 2 个、最多 5 个 tab`.red);
148
+ return cfg;
143
149
  }
150
+ _cfg.list = tabs;
151
+ // 找出 tabBar.list[number].pagePath 重复的配置
152
+ const _list = duplicates(tabs, (a, b) => (a.pagePath > b.pagePath ? 1 : a.pagePath == b.pagePath ? 0 : -1), 'pagePath');
153
+ if (_list.length) {
154
+ console.error(LOG_PREFIX, `tabBar.list不能有重复的页面: \n${JSON.stringify(_list, null, 2)}`.red);
155
+ }
156
+ cfg = fields.reduce((o, next) => {
157
+ const [src, t] = typeof next === 'string' ? [next] : next;
158
+ if (rest[src]) {
159
+ o[t || src] = rest[src];
160
+ }
161
+ return o;
162
+ }, _cfg);
144
163
  return cfg;
145
164
  }
146
165
  function PluginRouter(api) {
147
166
  let cacheThemeLocation = {};
167
+ let timerId;
148
168
  return {
149
169
  name: '@ray-js/build-plugin-router',
150
170
  setup() {
@@ -175,6 +195,7 @@ function PluginRouter(api) {
175
195
  return { routesConfig, globalConfig };
176
196
  },
177
197
  buildRoutesConfig(immediate) {
198
+ clearTimeout(timerId);
178
199
  const { routesConfig, globalConfig } = this.readConfigFromConfigFile();
179
200
  // target 在入口处已经统一处理,tuya 会转成 thing
180
201
  const { target } = api.options;
@@ -188,14 +209,14 @@ function PluginRouter(api) {
188
209
  // FIX: 修复初始化时找不到配置文件
189
210
  // 初始化时,需要立即生成配置文件
190
211
  if (immediate) {
191
- return this.generateAppConfig({ [target]: appConfig });
212
+ return this.generateAppConfig(target, appConfig);
192
213
  }
193
214
  // FIX: 修复间接(更新routes.config.ts)更新app.config.ts文件,无法触发重新编译,导致小程序app.json未更新
194
215
  // 原因:/node_modules/@ray-core/cli/lib/build/watch.js 中的变量isRunning为true
195
216
  // 在更新routes.config.ts触发的编译未完成,又因间接更新app.config.ts的触发重新编译被remax忽略导致
196
217
  // TODO: 此为临时方案,无法确定第一次更新什么时候完成,默认延时1s
197
- setTimeout(() => {
198
- this.generateAppConfig({ [target]: appConfig });
218
+ timerId = setTimeout(() => {
219
+ this.generateAppConfig(target, appConfig);
199
220
  }, 1000);
200
221
  },
201
222
  /**
@@ -234,9 +255,9 @@ function PluginRouter(api) {
234
255
  * 生成小程序app.config.{js|ts}文件
235
256
  * @param data
236
257
  */
237
- generateAppConfig(data) {
258
+ generateAppConfig(target, data) {
238
259
  const templateFile = path_1.default.join(__dirname, '../templates/app.config.ejs');
239
- const context = ejsRender(templateFile, { appConfig: data });
260
+ const context = ejsRender(templateFile, { appConfig: { [target]: data } });
240
261
  api.writeFile('src/app.config.ts', context);
241
262
  shared_1.log.verbose(LOG_PREFIX, `generate`, 'src/app.config.ts'.underline.green, `for wechat platform`);
242
263
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/build-plugin-router",
3
- "version": "0.6.22-beta-1",
3
+ "version": "0.6.23",
4
4
  "description": "Ray build plugin for router",
5
5
  "keywords": [
6
6
  "ray"
@@ -21,7 +21,7 @@
21
21
  "watch": "tsc -p ./tsconfig.build.json --watch"
22
22
  },
23
23
  "dependencies": {
24
- "@ray-js/shared": "^0.6.22-beta-1",
24
+ "@ray-js/shared": "^0.6.23",
25
25
  "chokidar": "^3.5.2",
26
26
  "colors": "1.4.0",
27
27
  "ejs": "^3.1.6",
@@ -30,7 +30,7 @@
30
30
  "url": "^0.11.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@ray-js/types": "^0.6.22-beta-1"
33
+ "@ray-js/types": "^0.6.23"
34
34
  },
35
35
  "maintainers": [
36
36
  {
@@ -38,6 +38,6 @@
38
38
  "email": "tuyafe@tuya.com"
39
39
  }
40
40
  ],
41
- "gitHead": "2e905a1820d0adfc0a6ca8cf3ecf1f3a584a0d44",
41
+ "gitHead": "e60b0be5d086d6d24238c12b29eebc3cbbd3f0dd",
42
42
  "repository": {}
43
43
  }