@xfe-repo/routes-define-webpack-plugin 0.0.1 → 0.0.2

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/dist/index.d.ts CHANGED
@@ -1,2 +1,19 @@
1
+ import { WebpackPluginInstance, Module, Compiler } from 'webpack';
1
2
 
2
- export { }
3
+ type RoutesDefineWebpackPluginOptions = {
4
+ pagesFolderName: string;
5
+ routePrefix: string;
6
+ routeSuffix: string;
7
+ outputPath: string;
8
+ fileName: string;
9
+ };
10
+ declare class RoutesDefineWebpackPlugin implements WebpackPluginInstance {
11
+ constructor(options?: Partial<RoutesDefineWebpackPluginOptions>);
12
+ options: RoutesDefineWebpackPluginOptions;
13
+ routesMap: Map<string, any>;
14
+ generateRouteTypeFile(): void;
15
+ collectRoutes(modules: Set<Module>): void;
16
+ apply(compiler: Compiler): void;
17
+ }
18
+
19
+ export { RoutesDefineWebpackPlugin, RoutesDefineWebpackPlugin as default };
package/dist/index.js CHANGED
@@ -22,6 +22,10 @@ var __spreadValues = (a, b) => {
22
22
  return a;
23
23
  };
24
24
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __export = (target, all) => {
26
+ for (var name in all)
27
+ __defProp(target, name, { get: all[name], enumerable: true });
28
+ };
25
29
  var __copyProps = (to, from, except, desc) => {
26
30
  if (from && typeof from === "object" || typeof from === "function") {
27
31
  for (let key of __getOwnPropNames(from))
@@ -38,18 +42,25 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
38
42
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
39
43
  mod
40
44
  ));
45
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
41
46
 
42
47
  // src/index.ts
48
+ var src_exports = {};
49
+ __export(src_exports, {
50
+ RoutesDefineWebpackPlugin: () => RoutesDefineWebpackPlugin,
51
+ default: () => src_default
52
+ });
53
+ module.exports = __toCommonJS(src_exports);
43
54
  var import_path = __toESM(require("path"));
44
55
  var import_fs_extra = __toESM(require("fs-extra"));
45
56
  var PluginName = "RoutesDefineWebpackPlugin";
46
57
  var RoutesDefineWebpackPlugin = class {
47
58
  constructor(options = {}) {
48
59
  this.routesMap = /* @__PURE__ */ new Map();
49
- const { pagesPath = "src/pages", routePrefix = "", routeSuffix = "" } = options;
60
+ const { pagesFolderName = "pages", routePrefix = "", routeSuffix = "" } = options;
50
61
  const { outputPath = "src", fileName = "routes.d.ts" } = options;
51
62
  this.options = {
52
- pagesPath,
63
+ pagesFolderName,
53
64
  routePrefix,
54
65
  routeSuffix,
55
66
  outputPath,
@@ -60,7 +71,7 @@ var RoutesDefineWebpackPlugin = class {
60
71
  generateRouteTypeFile() {
61
72
  const { outputPath, fileName } = this.options;
62
73
  import_fs_extra.default.ensureDirSync(outputPath);
63
- const routesDefTemplate = import_fs_extra.default.readFileSync(import_path.default.resolve(__dirname, "routes.d.ts"), "utf-8");
74
+ const routesDefTemplate = import_fs_extra.default.readFileSync(import_path.default.resolve(__dirname, "../routes.d.ts"), "utf-8");
64
75
  const routesMap = Object.fromEntries(this.routesMap);
65
76
  const routesType = JSON.stringify(routesMap, null, 2);
66
77
  const routesDef = routesDefTemplate.replace(`'ROUTES_DEFINE_TEMPLATE'`, routesType);
@@ -69,27 +80,27 @@ var RoutesDefineWebpackPlugin = class {
69
80
  }
70
81
  // 搜集所有路由模块
71
82
  collectRoutes(modules) {
72
- const { pagesPath, routePrefix, routeSuffix } = this.options;
83
+ const { pagesFolderName, routePrefix, routeSuffix } = this.options;
73
84
  const _tempRoutesModuleMap = /* @__PURE__ */ new Map();
74
85
  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
+ const { resource = "" } = module2;
87
+ if (!resource) return;
88
+ if (!resource.includes(pagesFolderName)) return;
89
+ const pageFilesPath = resource.split(pagesFolderName)[1] || "";
90
+ if (!pageFilesPath || pageFilesPath.includes("component")) return;
91
+ const pageRoutePath = import_path.default.dirname(pageFilesPath);
92
+ const currentRouteModule = _tempRoutesModuleMap.get(pageRoutePath) || {};
93
+ if (/index\.tsx$/.test(pageFilesPath)) {
94
+ _tempRoutesModuleMap.set(pageRoutePath, __spreadProps(__spreadValues({}, currentRouteModule), { routeIndexModule: module2 }));
95
+ } else if (/index\.config\.ts$/.test(pageFilesPath)) {
96
+ _tempRoutesModuleMap.set(pageRoutePath, __spreadProps(__spreadValues({}, currentRouteModule), { routeConfigModule: module2 }));
86
97
  }
87
98
  });
88
99
  _tempRoutesModuleMap.forEach((routeModule, routePath) => {
89
100
  const { routeConfigModule } = routeModule;
90
101
  const route = `/${routePrefix}/${routePath}/${routeSuffix}`.replace(/\/{2,}/g, "/");
91
102
  const routeConfigString = (routeConfigModule == null ? void 0 : routeConfigModule._source._valueAsString.match(/export\s+default\s+(\{[\s\S]+})/)[1]) || "{}";
92
- const routeConfigJson = JSON.parse(routeConfigString);
103
+ const routeConfigJson = new Function(`return ${routeConfigString} || {}`)();
93
104
  this.routesMap.set(route, routeConfigJson);
94
105
  });
95
106
  this.routesMap = new Map([...this.routesMap].sort());
@@ -102,4 +113,8 @@ var RoutesDefineWebpackPlugin = class {
102
113
  });
103
114
  }
104
115
  };
105
- module.exports = RoutesDefineWebpackPlugin;
116
+ var src_default = RoutesDefineWebpackPlugin;
117
+ // Annotate the CommonJS export names for ESM import in node:
118
+ 0 && (module.exports = {
119
+ RoutesDefineWebpackPlugin
120
+ });
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@xfe-repo/routes-define-webpack-plugin",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "sideEffects": false,
5
+ "main": "dist/index.js",
5
6
  "module": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "files": [
@@ -14,8 +15,11 @@
14
15
  "@types/fs-extra": "^11.0.4",
15
16
  "@types/node": "^20.16.5",
16
17
  "webpack": "^5.96.1",
17
- "@xfe-repo/typescript-config": "0.0.6",
18
- "@xfe-repo/eslint-config": "0.0.5"
18
+ "@xfe-repo/eslint-config": "0.0.5",
19
+ "@xfe-repo/typescript-config": "0.0.6"
20
+ },
21
+ "publishConfig": {
22
+ "registry": "https://registry.npmjs.org/"
19
23
  },
20
24
  "scripts": {
21
25
  "build": "tsup",