@heathenjs/taro-router-plugin 1.0.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,39 @@
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
+ exports.generateAppConfig = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const generateAppConfig = ({ pages, config, mainPackageName }) => {
9
+ const subPackages = config.packages
10
+ .filter((pkg) => pkg.name !== mainPackageName)
11
+ .map((pkg) => {
12
+ return {
13
+ root: path_1.default.resolve(process.cwd(), pkg.pagePath),
14
+ pages: [],
15
+ };
16
+ });
17
+ const appConfig = { pages: [], subPackages };
18
+ pages.forEach((currentPage) => {
19
+ if (currentPage.packageName === mainPackageName) {
20
+ appConfig.pages.push(currentPage.path.slice(1));
21
+ }
22
+ else {
23
+ const targetSubPackage = appConfig.subPackages.find((pkg) => currentPage.path.startsWith(pkg.root));
24
+ if (targetSubPackage) {
25
+ targetSubPackage.pages.push(currentPage.path.replace(`${targetSubPackage.root}${path_1.default.sep}`, ''));
26
+ }
27
+ }
28
+ });
29
+ if (config.defaultPage) {
30
+ appConfig.pages.sort((left) => {
31
+ if (left === config.defaultPage)
32
+ return -1;
33
+ return 0;
34
+ });
35
+ }
36
+ return appConfig;
37
+ };
38
+ exports.generateAppConfig = generateAppConfig;
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generate-app-config/index.ts"],"names":[],"mappings":";;;;;;AACA,gDAAwB;AASjB,MAAM,iBAAiB,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAA2B,EAAE,EAAE;IAC/F,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ;SAChC,MAAM,CAAC,CAAC,GAAG,EAAE,EAA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SAAS,CAAC;AACnB,CAAC,CAAC;AAnCW,QAAA,iBAAiB,qBAmC5B"}
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
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 taro_router_generator_1 = require("@heathen/taro-router-generator");
7
+ const generate_app_config_1 = require("./generate-app-config");
8
+ const path_1 = __importDefault(require("path"));
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const CONFIG_FILE_NAME = 'router.config.json';
11
+ exports.default = (ctx) => {
12
+ var _a, _b;
13
+ const configFilePath = path_1.default.resolve(process.cwd(), `./${CONFIG_FILE_NAME}`);
14
+ const isConfigFileExist = fs_1.default.existsSync(configFilePath);
15
+ let config = {};
16
+ if (isConfigFileExist) {
17
+ const configSource = fs_1.default.readFileSync(configFilePath, { encoding: 'utf8' });
18
+ config = JSON.parse(configSource);
19
+ }
20
+ else {
21
+ throw `找不到 ${configFilePath},请检查配置文件及执行路径。`;
22
+ }
23
+ config.packages = (_a = config.packages) !== null && _a !== void 0 ? _a : [];
24
+ config.ignore = (_b = config.ignore) !== null && _b !== void 0 ? _b : ['.DS_Store'];
25
+ ctx.onBuildStart(() => {
26
+ const generator = new taro_router_generator_1.Generator(config);
27
+ generator.generateRouter();
28
+ ctx.modifyAppConfig(({ appConfig }) => {
29
+ Object.assign(appConfig, (0, generate_app_config_1.generateAppConfig)({ config, pages: generator.getPages(), mainPackageName: generator.getMainPackageName() }));
30
+ });
31
+ console.log();
32
+ });
33
+ };
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,0EAA2D;AAE3D,+DAA0D;AAE1D,gDAAwB;AACxB,4CAAoB;AAEpB,MAAM,gBAAgB,GAAG,oBAAoB,CAAC;AAE9C,kBAA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Property(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":""}
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/no-require-imports */
2
+
3
+ module.exports = require('./dist/index.js').default;
4
+
5
+ module.exports.default = module.exports;
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@heathenjs/taro-router-plugin",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "prod": "tsc -p ./tsconfig.json",
8
+ "clean": "rimraf dist",
9
+ "build": "pnpm run clean && pnpm run prod",
10
+ "tsc": "tsc -p ./tsconfig.json --noEmit",
11
+ "lint": "eslint --ext .ts --max-warnings 0 \"./src\" "
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "peerDependencies": {
17
+ "@heathenjs/taro-router-generator": "workspace:*",
18
+ "@tarojs/service": "^3.6.35",
19
+ "@tarojs/taro": "^3.6.35"
20
+ }
21
+ }
@@ -0,0 +1,46 @@
1
+ import type { AppConfig } from '@tarojs/taro';
2
+ import path from 'path';
3
+ import { Config, Page } from '../type';
4
+
5
+ type GenerateAppConfigParams = {
6
+ pages: Page[];
7
+ config: Config;
8
+ mainPackageName: string;
9
+ };
10
+
11
+ export const generateAppConfig = ({ pages, config, mainPackageName }: GenerateAppConfigParams) => {
12
+ const subPackages = config.packages
13
+ .filter((pkg) => pkg.name !== mainPackageName)
14
+ .map((pkg) => {
15
+ return {
16
+ root: path.resolve(process.cwd(), pkg.pagePath),
17
+ pages: [] as string[],
18
+ };
19
+ });
20
+
21
+ const appConfig: Required<Pick<AppConfig, 'pages' | 'subPackages'>> = { pages: [], subPackages };
22
+
23
+ pages.forEach((currentPage) => {
24
+ if (currentPage.packageName === mainPackageName) {
25
+ // 主包的页面去除头部的 /
26
+ appConfig.pages.push(currentPage.path.slice(1));
27
+ } else {
28
+ // 对于分包,判断所属的分包并加入
29
+ const targetSubPackage = appConfig.subPackages.find((pkg) => currentPage.path.startsWith(pkg.root));
30
+ if (targetSubPackage) {
31
+ targetSubPackage.pages.push(currentPage.path.replace(`${targetSubPackage.root}${path.sep}`, ''));
32
+ }
33
+ }
34
+ });
35
+
36
+ // 配置了默认页面时,调整主包页面顺序
37
+ if (config.defaultPage) {
38
+ appConfig.pages.sort((left) => {
39
+ if (left === config.defaultPage) return -1;
40
+
41
+ return 0;
42
+ });
43
+ }
44
+
45
+ return appConfig;
46
+ };
package/src/index.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { Generator } from '@heathenjs/taro-router-generator';
2
+ import { IPluginContext } from '@tarojs/service';
3
+ import { generateAppConfig } from './generate-app-config';
4
+ import { Config } from './type';
5
+ import path from 'path';
6
+ import fs from 'fs';
7
+
8
+ const CONFIG_FILE_NAME = 'router.config.json';
9
+
10
+ export default (ctx: IPluginContext) => {
11
+ const configFilePath = path.resolve(process.cwd(), `./${CONFIG_FILE_NAME}`);
12
+
13
+ const isConfigFileExist = fs.existsSync(configFilePath);
14
+
15
+ let config: Config = {} as Config;
16
+ if (isConfigFileExist) {
17
+ const configSource = fs.readFileSync(configFilePath, { encoding: 'utf8' });
18
+
19
+ config = JSON.parse(configSource);
20
+ } else {
21
+ throw `找不到 ${configFilePath},请检查配置文件及执行路径。`;
22
+ }
23
+
24
+ config.packages = config.packages ?? [];
25
+ config.ignore = config.ignore ?? ['.DS_Store'];
26
+
27
+ ctx.onBuildStart(() => {
28
+ const generator = new Generator(config);
29
+ generator.generateRouter();
30
+
31
+ ctx.modifyAppConfig(({ appConfig }) => {
32
+ Object.assign(
33
+ appConfig,
34
+ generateAppConfig({ config, pages: generator.getPages(), mainPackageName: generator.getMainPackageName() }),
35
+ );
36
+ });
37
+
38
+ console.log();
39
+ });
40
+ };
package/src/type.ts ADDED
@@ -0,0 +1,31 @@
1
+ export interface ConfigPackage {
2
+ /** 包名 */
3
+ name: string;
4
+ /** 包页面根路径 */
5
+ pagePath: string;
6
+ }
7
+
8
+ export type Config = {
9
+ /** 排除的文件 */
10
+ ignore: string[];
11
+ /**
12
+ * 应用的默认页面,不指定时使用 pages 的第一项
13
+ * - 注意 pages 是自动生成的,默认按字典序
14
+ */
15
+ defaultPage?: string;
16
+ /** 包配置 */
17
+ packages: [ConfigPackage & { isMainPackage: true }, ...(ConfigPackage & { isMainPackage?: false })[]];
18
+ };
19
+
20
+ export type Page = {
21
+ dirName: string;
22
+ dirPath: string;
23
+ path: string;
24
+ pageFileName: string;
25
+ packageName: string;
26
+ routeConfig?: {
27
+ params?: string;
28
+ backData?: string;
29
+ ext?: string;
30
+ };
31
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "extends": "../../tsconfig",
3
+ "compilerOptions": {
4
+ "target": "es2015",
5
+ "module": "CommonJS",
6
+ "experimentalDecorators": false,
7
+ "outDir": "dist",
8
+ "rootDir": "./src",
9
+ "baseUrl": ".",
10
+ "removeComments": true,
11
+ "downlevelIteration": true,
12
+ "sourceMap": true,
13
+ "isolatedModules": false,
14
+ "noEmit": false
15
+ },
16
+ "include": ["./**/*.ts", "./**/*.tsx"],
17
+ "exclude": ["node_modules", "dist"]
18
+ }