@ray-js/build-plugin-router 0.5.5 → 0.5.6-beta-1

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 +44 -7
  2. package/package.json +5 -5
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ const chokidar_1 = __importDefault(require("chokidar"));
7
7
  const colors_1 = __importDefault(require("colors"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const lodash_1 = __importDefault(require("lodash"));
10
+ const fs_extra_1 = __importDefault(require("fs-extra"));
10
11
  const plugin_utils_1 = require("@ray-js/plugin-utils");
11
12
  const shared_1 = require("@ray-js/shared");
12
13
  colors_1.default.enable();
@@ -15,6 +16,7 @@ function normalizeRoute(route) {
15
16
  return (0, shared_1.paramCase)(route);
16
17
  }
17
18
  function PluginRouter(api) {
19
+ let cacheThemeLocation = {};
18
20
  return {
19
21
  name: '@ray-js/build-plugin-router',
20
22
  setup() {
@@ -35,6 +37,7 @@ function PluginRouter(api) {
35
37
  }
36
38
  },
37
39
  buildRoutesConfig(immediate) {
40
+ var _a, _b;
38
41
  const routesConfigFile = api.searchJSFile('src/routes.config');
39
42
  const { target } = api.options;
40
43
  if (!routesConfigFile) {
@@ -47,22 +50,23 @@ function PluginRouter(api) {
47
50
  if (!['tuya', 'wechat'].includes(target)) {
48
51
  return this.generateWebRoutes(routesConfig);
49
52
  }
50
- const wechatAppConfig = this.getWechatAppConfigData(routesConfig, globalConfig['wechat'] || {});
51
- const tuyaAppConfig = this.getTuyaAppConfigData(routesConfig, globalConfig['tuya'] || {});
53
+ const _target = lodash_1.default.capitalize(target);
54
+ const appConfig = (_a = this[`get${_target}AppConfigData`]) === null || _a === void 0 ? void 0 : _a.call(this, routesConfig, (_b = globalConfig[target]) !== null && _b !== void 0 ? _b : {});
55
+ if (lodash_1.default.isEmpty(appConfig.tabBar)) {
56
+ delete appConfig.tabBar; // 微信小程序tabBar不能为空对象,所以当为空对象时,索性所有小程序都删了
57
+ }
58
+ this.generateThemeConfig(appConfig);
52
59
  // FIX: 修复初始化时找不到配置文件
53
60
  // 初始化时,需要立即生成配置文件
54
- if (lodash_1.default.isEmpty(wechatAppConfig.tabBar)) {
55
- delete wechatAppConfig.tabBar;
56
- }
57
61
  if (immediate) {
58
- return this.generateAppConfig({ wechat: wechatAppConfig, tuya: tuyaAppConfig });
62
+ return this.generateAppConfig({ [target]: appConfig });
59
63
  }
60
64
  // FIX: 修复间接(更新routes.config.ts)更新app.config.ts文件,无法触发重新编译,导致小程序app.json未更新
61
65
  // 原因:/node_modules/@ray-core/cli/lib/build/watch.js 中的变量isRunning为true
62
66
  // 在更新routes.config.ts触发的编译未完成,又因间接更新app.config.ts的触发重新编译被remax忽略导致
63
67
  // TODO: 此为临时方案,无法确定第一次更新什么时候完成,默认延时1s
64
68
  setTimeout(() => {
65
- this.generateAppConfig({ wechat: wechatAppConfig, tuya: tuyaAppConfig });
69
+ this.generateAppConfig({ [target]: appConfig });
66
70
  }, 1000);
67
71
  },
68
72
  /**
@@ -179,6 +183,39 @@ function PluginRouter(api) {
179
183
  return Object.assign(Object.assign({}, route), { config: config });
180
184
  });
181
185
  },
186
+ generateThemeConfig(appConfig) {
187
+ const src = this.checkThemeConfig(appConfig.themeLocation);
188
+ if (cacheThemeLocation.source && cacheThemeLocation.source !== src) {
189
+ fs_extra_1.default.unlinkSync(cacheThemeLocation.dest);
190
+ fs_extra_1.default.unwatchFile(cacheThemeLocation.source);
191
+ cacheThemeLocation = {};
192
+ }
193
+ if (src) {
194
+ appConfig.themeLocation = path_1.default.basename(src);
195
+ handle();
196
+ fs_extra_1.default.watchFile(src, handle);
197
+ }
198
+ function handle() {
199
+ const { cwd, target } = api.options;
200
+ const output = path_1.default.join(cwd, 'dist', target, path_1.default.basename(src));
201
+ const data = fs_extra_1.default.readFileSync(src).toString('utf-8');
202
+ fs_extra_1.default.outputFile(output, data, 'utf-8');
203
+ cacheThemeLocation.dest = output;
204
+ cacheThemeLocation.source = src;
205
+ }
206
+ },
207
+ checkThemeConfig(src) {
208
+ if (!src) {
209
+ return;
210
+ }
211
+ const { cwd, source } = api.options;
212
+ const inputs = [path_1.default.resolve(cwd, source, src), path_1.default.resolve(cwd, src)];
213
+ for (const input of inputs) {
214
+ if (fs_extra_1.default.pathExistsSync(input)) {
215
+ return input;
216
+ }
217
+ }
218
+ },
182
219
  };
183
220
  }
184
221
  exports.default = PluginRouter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/build-plugin-router",
3
- "version": "0.5.5",
3
+ "version": "0.5.6-beta-1",
4
4
  "description": "Ray build plugin for router",
5
5
  "keywords": [
6
6
  "ray"
@@ -21,13 +21,13 @@
21
21
  "watch": "tsc -p ./tsconfig.build.json --watch"
22
22
  },
23
23
  "dependencies": {
24
- "@ray-js/plugin-utils": "^0.5.5",
25
- "@ray-js/shared": "^0.5.5",
24
+ "@ray-js/plugin-utils": "^0.5.6-beta-1",
25
+ "@ray-js/shared": "^0.5.6-beta-1",
26
26
  "chokidar": "^3.5.2",
27
27
  "colors": "1.4.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@ray-js/types": "^0.5.5"
30
+ "@ray-js/types": "^0.5.6-beta-1"
31
31
  },
32
32
  "maintainers": [
33
33
  {
@@ -35,6 +35,6 @@
35
35
  "email": "tuyafe@tuya.com"
36
36
  }
37
37
  ],
38
- "gitHead": "a8ddeab1f4782e6b3772dc2deeae3e3e3af0de00",
38
+ "gitHead": "46f291d3c38bb9fa1af739d01f9513c39e3b6be9",
39
39
  "repository": {}
40
40
  }