@ray-js/build-plugin-router 0.3.0-beta.1c347991

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 ADDED
@@ -0,0 +1,6 @@
1
+ # @ray-js/build-plugin-router
2
+
3
+ 该插件根据构建目标 target 字段,将 routes.config.ts 文件进行转换
4
+
5
+ - 小程序(微信、涂鸦) - 生成 `src/app.config.ts` 临时文件提供给 remax 进行构建
6
+ - web 端 - 由 `.ray` 直接调用,不进行处理
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { PluginIns, API } from '@ray-js/types';
2
+ export default function PluginRouter(api: API): PluginIns;
package/lib/index.js ADDED
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const chokidar_1 = __importDefault(require("chokidar"));
7
+ const colors_1 = __importDefault(require("colors"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const lodash_1 = __importDefault(require("lodash"));
10
+ const plugin_utils_1 = require("@ray-js/plugin-utils");
11
+ const shared_1 = require("@ray-js/shared");
12
+ colors_1.default.enable();
13
+ const LOG_PREFIX = `build-plugin-router`;
14
+ function normalizeRoute(route) {
15
+ return (0, shared_1.paramCase)(route);
16
+ }
17
+ function PluginRouter(api) {
18
+ return {
19
+ name: '@ray-js/build-plugin-router',
20
+ setup() {
21
+ shared_1.log.verbose(LOG_PREFIX, 'setup');
22
+ this.buildRoutesConfig(true);
23
+ if (api.options.watch) {
24
+ chokidar_1.default
25
+ .watch(['**/*.config.ts', '**/*.config.js', '!app.config.{js,ts}'], {
26
+ cwd: path_1.default.join(api.options.cwd, api.options.source),
27
+ })
28
+ .on('change', (file) => {
29
+ const filePath = path_1.default.join(api.options.cwd, api.options.source, file);
30
+ // 配置文件引用存在 cache
31
+ delete require.cache[require.resolve(filePath)];
32
+ // FIXME: 引用文件变更也需要重新构建
33
+ this.buildRoutesConfig(false);
34
+ });
35
+ }
36
+ },
37
+ buildRoutesConfig(immediate) {
38
+ const routesConfigFile = api.searchJSFile('src/routes.config');
39
+ const { target } = api.options;
40
+ if (!routesConfigFile) {
41
+ throw new Error(`missing configuration file (routes.config.ts)`);
42
+ }
43
+ const globalConfigFile = api.searchJSFile('src/global.config');
44
+ const routesConfig = api.requireJSFile(routesConfigFile);
45
+ const globalConfig = globalConfigFile ? api.requireJSFile(globalConfigFile, '*') : {};
46
+ // TODO: 小程序端统一生成 app.config 需要过滤 targets 的描述页面对象
47
+ if (!['tuya', 'wechat'].includes(target)) {
48
+ return this.generateWebRoutes(routesConfig);
49
+ }
50
+ const wechatAppConfig = this.getWechatAppConfigData(routesConfig, globalConfig['wechat'] || {});
51
+ const tuyaAppConfig = this.getTuyaAppConfigData(routesConfig, globalConfig['tuya'] || {});
52
+ // FIX: 修复初始化时找不到配置文件
53
+ // 初始化时,需要立即生成配置文件
54
+ if (lodash_1.default.isEmpty(wechatAppConfig.tabBar)) {
55
+ delete wechatAppConfig.tabBar;
56
+ }
57
+ if (immediate) {
58
+ return this.generateAppConfig({ wechat: wechatAppConfig, tuya: tuyaAppConfig });
59
+ }
60
+ // FIX: 修复间接(更新routes.config.ts)更新app.config.ts文件,无法触发重新编译,导致小程序app.json未更新
61
+ // 原因:/node_modules/@remax/cli/lib/build/watch.js 中的变量isRunning为true
62
+ // 在更新routes.config.ts触发的编译未完成,又因间接更新app.config.ts的触发重新编译被remax忽略导致
63
+ // TODO: 此为临时方案,无法确定第一次更新什么时候完成,默认延时1s
64
+ setTimeout(() => {
65
+ this.generateAppConfig({ wechat: wechatAppConfig, tuya: tuyaAppConfig });
66
+ }, 1000);
67
+ },
68
+ /**
69
+ * 生成微信小程序的 app.config 配置
70
+ * @param routesConfig - routes.config 配置内容
71
+ * @param appConfig - 已存在的应用配置
72
+ */
73
+ getWechatAppConfigData(routesConfig, globalConfig) {
74
+ const { routes, tabBar } = routesConfig;
75
+ const pages = routes.map((r) => r.path.replace(/^\//, ''));
76
+ const tabBarConfig = {};
77
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.textColor) {
78
+ tabBarConfig.color = tabBar.textColor;
79
+ }
80
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.borderStyle) {
81
+ tabBarConfig.borderStyle = tabBar.borderStyle;
82
+ }
83
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.selectedColor) {
84
+ tabBarConfig.selectedColor = tabBar.selectedColor;
85
+ }
86
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.backgroundColor) {
87
+ tabBarConfig.backgroundColor = tabBar.backgroundColor;
88
+ }
89
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) {
90
+ tabBarConfig.list = tabBar.list.map((tab) => {
91
+ const pagePath = routes.find((route) => route.id === tab.id);
92
+ return {
93
+ pagePath: pagePath.path.replace(/^\//, ''),
94
+ text: tab.text,
95
+ iconPath: tab.icon,
96
+ selectedIconPath: tab.activeIcon,
97
+ };
98
+ });
99
+ }
100
+ const config = Object.assign({}, globalConfig, { pages, tabBar: tabBarConfig });
101
+ return config;
102
+ },
103
+ getTuyaAppConfigData(routesConfig, globalConfig) {
104
+ const { routes, tabBar } = routesConfig;
105
+ const pages = routes.map((r) => r.path.replace(/^\//, ''));
106
+ const tabBarConfig = {};
107
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.textColor) {
108
+ tabBarConfig.color = tabBar.textColor;
109
+ }
110
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.borderStyle) {
111
+ tabBarConfig.borderStyle = tabBar.borderStyle;
112
+ }
113
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.selectedColor) {
114
+ tabBarConfig.selectedColor = tabBar.selectedColor;
115
+ }
116
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.backgroundColor) {
117
+ tabBarConfig.backgroundColor = tabBar.backgroundColor;
118
+ }
119
+ if (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) {
120
+ tabBarConfig.list = tabBar.list.map((tab) => {
121
+ const pagePath = routes.find((route) => route.id === tab.id);
122
+ return {
123
+ pagePath: pagePath.path.replace(/^\//, ''),
124
+ text: tab.text,
125
+ iconPath: tab.icon,
126
+ selectedIconPath: tab.activeIcon,
127
+ };
128
+ });
129
+ }
130
+ const config = Object.assign({}, globalConfig, { pages, tabBar: tabBarConfig });
131
+ return config;
132
+ },
133
+ generateAppConfig(data) {
134
+ const templateFile = path_1.default.join(__dirname, '../templates/app.config.ejs');
135
+ const context = (0, plugin_utils_1.ejsRender)(templateFile, { appConfig: data });
136
+ api.writeFile('src/app.config.ts', context);
137
+ shared_1.log.verbose(LOG_PREFIX, `generate`, 'src/app.config.ts'.underline.green, `for wechat platform`);
138
+ },
139
+ generateWebRoutes(params) {
140
+ const { routes } = params;
141
+ const routesConfig = this.routesAdditionConfig(routes);
142
+ const templateFile = path_1.default.join(__dirname, '../templates/routes.config.ejs');
143
+ const pages = routesConfig.map((item) => {
144
+ const chunkName = normalizeRoute(item.id || item.route);
145
+ return {
146
+ id: item.id || '',
147
+ route: item.route,
148
+ path: item.path,
149
+ config: item.config,
150
+ chunkName,
151
+ };
152
+ });
153
+ const context = (0, plugin_utils_1.ejsRender)(templateFile, { pages });
154
+ api.writeFile('.ray/router.config.ts', context);
155
+ shared_1.log.verbose(LOG_PREFIX, `generate`, '.ray/router.config.ts'.green.underline, `for web platform`);
156
+ },
157
+ /**
158
+ * 附加路由的配置信息
159
+ * @param routes - 路由配置
160
+ */
161
+ routesAdditionConfig(routes) {
162
+ return routes.map((route) => {
163
+ let config = {};
164
+ const configFilePath = api.searchJSFile(api.options.source + route.path + '.config');
165
+ if (configFilePath) {
166
+ config = `require('@${route.path + '.config'}').web || {}`;
167
+ }
168
+ return Object.assign(Object.assign({}, route), { config: config });
169
+ });
170
+ },
171
+ };
172
+ }
173
+ exports.default = PluginRouter;
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@ray-js/build-plugin-router",
3
+ "version": "0.3.0-beta.1c347991",
4
+ "description": "Ray build plugin for router",
5
+ "keywords": [
6
+ "ray"
7
+ ],
8
+ "author": "子长 <zichang.nong@tuya.com>",
9
+ "license": "MIT",
10
+ "main": "lib/index.js",
11
+ "files": [
12
+ "lib",
13
+ "templates"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public",
17
+ "registry": "https://registry.npmjs.org"
18
+ },
19
+ "scripts": {
20
+ "clean": "rm -rf lib",
21
+ "build": "tsc -p ./tsconfig.build.json",
22
+ "watch": "tsc -p ./tsconfig.build.json --watch"
23
+ },
24
+ "peerDependencies": {
25
+ "@ray-js/cli": "^0.0.0"
26
+ },
27
+ "dependencies": {
28
+ "@ray-js/plugin-utils": "^0.3.0-beta.1c347991",
29
+ "@ray-js/shared": "^0.3.0-beta.1c347991",
30
+ "chokidar": "^3.5.2",
31
+ "colors": "1.4.0"
32
+ },
33
+ "devDependencies": {
34
+ "@ray-js/types": "^0.3.0-beta.1c347991"
35
+ },
36
+ "maintainers": [
37
+ {
38
+ "name": "tuyafe",
39
+ "email": "tuyafe@tuya.com"
40
+ }
41
+ ],
42
+ "gitHead": "e0bd013022ddda63380d3c9e20fd8cadb46cd61f",
43
+ "repository": {}
44
+ }
@@ -0,0 +1,6 @@
1
+ // this file generate by @ray-js/build-plugin-router.
2
+ // do not modify this file!!!
3
+ <% Object.keys(appConfig).forEach(function(target) { _%>
4
+ export const <%= target %> = <%- JSON.stringify(appConfig[target], null, 2)%>;
5
+ <%_ }); _%>
6
+
@@ -0,0 +1,17 @@
1
+ // this file generate by @ray-js/build-plugin-router.
2
+ // do not modify this file!!!
3
+ export const PAGES = [
4
+ <%_ pages.forEach(function(page) { _%>
5
+ {
6
+ id: '<%= page.id %>',
7
+ route: '<%= page.route %>',
8
+ path: '<%= page.path %>',
9
+ config: <%- typeof page.config === 'string' ? page.config : JSON.stringify(page.config) %>,
10
+ component: function () {
11
+ return import(/* webpackChunkName: "<%= page.chunkName %>" */ '@<%= page.path %>').then(
12
+ (module) => module.default
13
+ );
14
+ },
15
+ },
16
+ <%_ }); _%>
17
+ ];