@moluoxixi/vite-config 0.0.26 → 0.0.27

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/es/index.mjs +97 -58
  2. package/package.json +1 -1
package/es/index.mjs CHANGED
@@ -829,6 +829,17 @@ function createVirtualPlugin(userConfig) {
829
829
  ...restHooks
830
830
  };
831
831
  }
832
+ function cleanRoute(route) {
833
+ const cleaned = { ...route };
834
+ if (cleaned.children) {
835
+ if (Array.isArray(cleaned.children) && cleaned.children.length > 0) {
836
+ cleaned.children = cleaned.children.map(cleanRoute);
837
+ } else {
838
+ delete cleaned.children;
839
+ }
840
+ }
841
+ return cleaned;
842
+ }
832
843
  function findParentRouteHandle(modules2, parentPath) {
833
844
  for (const route of modules2) {
834
845
  if (route.path === parentPath) {
@@ -842,24 +853,48 @@ function findParentRouteHandle(modules2, parentPath) {
842
853
  }
843
854
  return void 0;
844
855
  }
856
+ function parseModulePath(modulePath) {
857
+ const pathArr = modulePath.split("/").filter((item) => item && !item.includes("."));
858
+ if (pathArr.at(-1) === "src") {
859
+ pathArr.pop();
860
+ }
861
+ const componentName = pathArr.at(-1);
862
+ const path2 = `/${pathArr.join("/")}`;
863
+ const parentPath = `/${pathArr.slice(0, -1).join("/")}`;
864
+ return { pathArr, componentName, path: path2, parentPath };
865
+ }
866
+ function processComponent(componentLoader, componentName, eager) {
867
+ if (eager) {
868
+ return {
869
+ component: componentLoader,
870
+ metaTitle: (componentLoader == null ? void 0 : componentLoader.name) || componentName
871
+ };
872
+ }
873
+ return {
874
+ component: typeof componentLoader === "function" ? componentLoader : componentLoader,
875
+ metaTitle: componentName
876
+ };
877
+ }
845
878
  function generateRoutes(files, prefix = "", baseRoute, eager = false) {
846
879
  const newBaseRoute = typeof baseRoute === "string" ? { name: baseRoute } : baseRoute;
847
880
  const modules2 = newBaseRoute ? [newBaseRoute] : [];
848
- return Object.keys(files).sort((a, b) => {
881
+ const fileKeys = Object.keys(files);
882
+ if (fileKeys.length === 0) {
883
+ return [];
884
+ }
885
+ return fileKeys.sort((a, b) => {
849
886
  const aLength = a.split("/").length;
850
887
  const bLength = b.split("/").length;
851
888
  return bLength > aLength ? -1 : 1;
852
889
  }).reduce((modules22 = [], modulePath) => {
853
890
  const componentLoader = files[modulePath];
854
- if (!componentLoader || modulePath === "install")
891
+ if (!componentLoader || modulePath === "install") {
892
+ return modules22;
893
+ }
894
+ const { path: path2, parentPath, componentName } = parseModulePath(modulePath);
895
+ if (!componentName) {
855
896
  return modules22;
856
- const pathArr = modulePath.split("/").filter((item) => item && !item.includes("."));
857
- if (pathArr.at(-1) === "src") {
858
- pathArr.pop();
859
897
  }
860
- const componentName = pathArr.at(-1);
861
- const path2 = `/${pathArr.join("/")}`;
862
- const parentPath = `/${pathArr.slice(0, -1).join("/")}`;
863
898
  let parentRoute;
864
899
  if (newBaseRoute) {
865
900
  newBaseRoute.children = newBaseRoute.children || [];
@@ -869,56 +904,64 @@ function generateRoutes(files, prefix = "", baseRoute, eager = false) {
869
904
  } else {
870
905
  parentRoute = findParentRouteHandle(modules22, parentPath);
871
906
  }
872
- let component;
873
- let metaTitle = componentName;
874
- if (eager) {
875
- component = componentLoader;
876
- metaTitle = (component == null ? void 0 : component.name) || componentName;
877
- } else {
878
- component = typeof componentLoader === "function" ? componentLoader : componentLoader;
879
- metaTitle = componentName;
880
- }
907
+ const { component, metaTitle } = processComponent(componentLoader, componentName, eager);
908
+ const routeItem = {
909
+ path: path2,
910
+ name: componentName,
911
+ meta: {
912
+ title: metaTitle
913
+ },
914
+ component
915
+ };
881
916
  if (parentRoute) {
882
- if (!parentRoute.children)
917
+ if (!parentRoute.children) {
883
918
  parentRoute.children = [];
884
- parentRoute.children.push({
885
- path: path2,
886
- name: componentName,
887
- meta: {
888
- title: metaTitle
889
- },
890
- component
891
- });
919
+ }
920
+ parentRoute.children.push(routeItem);
892
921
  } else {
893
- modules22.push({
894
- path: path2,
895
- name: componentName,
896
- meta: {
897
- title: metaTitle
898
- },
899
- component
900
- });
922
+ modules22.push(routeItem);
901
923
  }
902
924
  return modules22;
903
- }, modules2);
925
+ }, modules2).map(cleanRoute).filter((route) => {
926
+ return !(route.children && Array.isArray(route.children) && route.children.length === 0);
927
+ });
904
928
  }
905
929
  function findDefaultRouteHandle(routes) {
906
930
  var _a, _b, _c;
931
+ if (!routes || routes.length === 0) {
932
+ return "";
933
+ }
907
934
  for (const route of routes) {
908
935
  if ((_a = route.meta) == null ? void 0 : _a.default) {
909
- return route.path;
910
- } else {
911
- if ((_b = route.children) == null ? void 0 : _b.length) {
912
- return findDefaultRouteHandle(route.children);
936
+ return route.path || "";
937
+ }
938
+ if ((_b = route.children) == null ? void 0 : _b.length) {
939
+ const childPath = findDefaultRouteHandle(route.children);
940
+ if (childPath) {
941
+ return childPath;
913
942
  }
914
943
  }
915
944
  }
916
- return (_c = routes == null ? void 0 : routes[0]) == null ? void 0 : _c.path;
945
+ return ((_c = routes[0]) == null ? void 0 : _c.path) || "";
946
+ }
947
+ function extractGlob(globVal) {
948
+ return globVal.glob || globVal;
949
+ }
950
+ function getEagerOption(globVal, globalEager) {
951
+ return globVal.eager ?? globalEager ?? false;
952
+ }
953
+ function generateImportCode(varName, glob, eager) {
954
+ if (eager) {
955
+ return `const ${varName} = import.meta.glob(${JSON.stringify(glob)}, { eager: true, import: 'default' });
956
+ `;
957
+ }
958
+ return `const ${varName} = import.meta.glob(${JSON.stringify(glob)});
959
+ `;
917
960
  }
918
961
  function createAutoRoutesPlugin({ routeConfig, virtualModuleId, dts, root, eager: globalEager }) {
919
962
  const VIRTUAL_MODULE_ID = virtualModuleId || "virtual:auto-routes";
920
963
  const watchGlobs = Object.values(routeConfig).flatMap((globVal) => {
921
- const g = globVal.glob || globVal;
964
+ const g = extractGlob(globVal);
922
965
  return Array.isArray(g) ? g : [g];
923
966
  });
924
967
  return createVirtualPlugin(
@@ -934,33 +977,29 @@ function createAutoRoutesPlugin({ routeConfig, virtualModuleId, dts, root, eager
934
977
  const routes = [];
935
978
  Object.entries(routeConfig).forEach(([prefix, globVal], index2) => {
936
979
  const varName = `files${index2}`;
937
- const glob = globVal.glob || globVal;
938
- const eager = globVal.eager ?? globalEager ?? false;
939
- if (eager) {
940
- imports.push(
941
- `const ${varName} = import.meta.glob(${JSON.stringify(glob)}, { eager: true, import: 'default' });
942
- `
943
- );
944
- } else {
945
- imports.push(
946
- `const ${varName} = import.meta.glob(${JSON.stringify(glob)});
947
- `
948
- );
949
- }
980
+ const glob = extractGlob(globVal);
981
+ const eager = getEagerOption(globVal, globalEager);
950
982
  const baseRoute = globVal.baseRoute;
951
- routes.push(`...generateRoutes(${varName}, '${prefix}',${JSON.stringify(baseRoute)}, ${eager})`);
983
+ const baseRouteParam = baseRoute !== void 0 ? JSON.stringify(baseRoute) : "undefined";
984
+ imports.push(generateImportCode(varName, glob, eager));
985
+ routes.push(`...generateRoutes(${varName}, '${prefix}',${baseRouteParam}, ${eager})`);
952
986
  });
987
+ const routesCode = routes.length > 0 ? `[${routes.join(",\n")}]` : "[]";
953
988
  return `
954
989
  ${imports.join("\n")}
990
+ ${findParentRouteHandle}
991
+ ${parseModulePath}
992
+ ${processComponent}
993
+ ${cleanRoute}
994
+ ${findDefaultRouteHandle}
995
+
955
996
  const findParentRoute = ${findParentRouteHandle}
956
997
 
957
998
  const generateRoutes = ${generateRoutes};
958
999
 
959
1000
  const findDefaultRoute = ${findDefaultRouteHandle};
960
1001
 
961
- ${findParentRouteHandle}
962
- ${findDefaultRouteHandle}
963
- const routes = [${routes.join(",\n")}];
1002
+ const routes = ${routesCode}.flat().filter(Boolean);
964
1003
 
965
1004
  export { routes, findDefaultRoute };
966
1005
  export default routes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/vite-config",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "description": "ViteConfig 组件",
5
5
  "sideEffects": [
6
6
  "*.css",