@xfe-repo/routes-define-webpack-plugin 0.0.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.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@xfe-repo/routes-define-webpack-plugin`
2
+
3
+ webpack plugin for routes define
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") {
27
+ for (let key of __getOwnPropNames(from))
28
+ if (!__hasOwnProp.call(to, key) && key !== except)
29
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
30
+ }
31
+ return to;
32
+ };
33
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
34
+ // If the importer is in node compatibility mode or this is not an ESM
35
+ // file that has been converted to a CommonJS file using a Babel-
36
+ // compatible transform (i.e. "__esModule" has not been set), then set
37
+ // "default" to the CommonJS "module.exports" for node compatibility.
38
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
39
+ mod
40
+ ));
41
+
42
+ // src/index.ts
43
+ var import_path = __toESM(require("path"));
44
+ var import_fs_extra = __toESM(require("fs-extra"));
45
+ var PluginName = "RoutesDefineWebpackPlugin";
46
+ var RoutesDefineWebpackPlugin = class {
47
+ constructor(options = {}) {
48
+ this.routesMap = /* @__PURE__ */ new Map();
49
+ const { pagesPath = "src/pages", routePrefix = "", routeSuffix = "" } = options;
50
+ const { outputPath = "src", fileName = "routes.d.ts" } = options;
51
+ this.options = {
52
+ pagesPath,
53
+ routePrefix,
54
+ routeSuffix,
55
+ outputPath,
56
+ fileName
57
+ };
58
+ }
59
+ // 生成路由类型文件
60
+ generateRouteTypeFile() {
61
+ const { outputPath, fileName } = this.options;
62
+ import_fs_extra.default.ensureDirSync(outputPath);
63
+ const routesDefTemplate = import_fs_extra.default.readFileSync(import_path.default.resolve(__dirname, "routes.d.ts"), "utf-8");
64
+ const routesMap = Object.fromEntries(this.routesMap);
65
+ const routesType = JSON.stringify(routesMap, null, 2);
66
+ const routesDef = routesDefTemplate.replace(`'ROUTES_DEFINE_TEMPLATE'`, routesType);
67
+ import_fs_extra.default.writeFileSync(import_path.default.resolve(outputPath, fileName), routesDef);
68
+ console.log("Generate routes.d.ts success!");
69
+ }
70
+ // 搜集所有路由模块
71
+ collectRoutes(modules) {
72
+ const { pagesPath, routePrefix, routeSuffix } = this.options;
73
+ const _tempRoutesModuleMap = /* @__PURE__ */ new Map();
74
+ modules.forEach((module2) => {
75
+ const { resource } = module2;
76
+ if (resource) return;
77
+ if (resource.includes(pagesPath)) return;
78
+ const relativePath = import_path.default.relative(pagesPath, resource);
79
+ if (relativePath.includes("component")) return;
80
+ const pathDir = import_path.default.dirname(relativePath);
81
+ const currentRouteModule = _tempRoutesModuleMap.get(pathDir) || {};
82
+ if (/index\.tsx$/.test(relativePath)) {
83
+ _tempRoutesModuleMap.set(pathDir, __spreadProps(__spreadValues({}, currentRouteModule), { routeIndexModule: module2 }));
84
+ } else if (/index\.config\.ts$/.test(relativePath)) {
85
+ _tempRoutesModuleMap.set(pathDir, __spreadProps(__spreadValues({}, currentRouteModule), { routeConfigModule: module2 }));
86
+ }
87
+ });
88
+ _tempRoutesModuleMap.forEach((routeModule, routePath) => {
89
+ const { routeConfigModule } = routeModule;
90
+ const route = `/${routePrefix}/${routePath}/${routeSuffix}`.replace(/\/{2,}/g, "/");
91
+ const routeConfigString = (routeConfigModule == null ? void 0 : routeConfigModule._source._valueAsString.match(/export\s+default\s+(\{[\s\S]+})/)[1]) || "{}";
92
+ const routeConfigJson = JSON.parse(routeConfigString);
93
+ this.routesMap.set(route, routeConfigJson);
94
+ });
95
+ this.routesMap = new Map([...this.routesMap].sort());
96
+ }
97
+ apply(compiler) {
98
+ compiler.hooks.done.tapAsync(PluginName, (stats, callback) => {
99
+ this.collectRoutes(stats.compilation.modules);
100
+ this.generateRouteTypeFile();
101
+ callback();
102
+ });
103
+ }
104
+ };
105
+ module.exports = RoutesDefineWebpackPlugin;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@xfe-repo/routes-define-webpack-plugin",
3
+ "version": "0.0.1",
4
+ "sideEffects": false,
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "dependencies": {
11
+ "fs-extra": "^11.2.0"
12
+ },
13
+ "devDependencies": {
14
+ "@types/fs-extra": "^11.0.4",
15
+ "@types/node": "^20.16.5",
16
+ "webpack": "^5.96.1",
17
+ "@xfe-repo/typescript-config": "0.0.6",
18
+ "@xfe-repo/eslint-config": "0.0.5"
19
+ },
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "dev": "tsup --watch",
23
+ "lint": "eslint \"src/**/*.ts*\"",
24
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
25
+ }
26
+ }