@uni-helper/vite-plugin-uni-pages 0.3.18 → 0.3.20

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.cjs CHANGED
@@ -15,10 +15,10 @@ const commentJson = require('comment-json');
15
15
  const Debug = require('debug');
16
16
  const detectIndent = require('detect-indent');
17
17
  const detectNewline = require('detect-newline');
18
- const lockfile = require('proper-lockfile');
19
18
  const unconfig = require('unconfig');
20
19
  const promises = require('node:fs/promises');
21
20
  const fg = require('fast-glob');
21
+ const lockfile = require('proper-lockfile');
22
22
  const node_module = require('node:module');
23
23
  const vm = require('node:vm');
24
24
  const babelGenerator = require('@babel/generator');
@@ -51,8 +51,8 @@ const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
51
51
  const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
52
52
  const detectIndent__default = /*#__PURE__*/_interopDefaultCompat(detectIndent);
53
53
  const detectNewline__default = /*#__PURE__*/_interopDefaultCompat(detectNewline);
54
- const lockfile__default = /*#__PURE__*/_interopDefaultCompat(lockfile);
55
54
  const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
55
+ const lockfile__default = /*#__PURE__*/_interopDefaultCompat(lockfile);
56
56
  const vm__default = /*#__PURE__*/_interopDefaultCompat(vm);
57
57
  const babelGenerator__default = /*#__PURE__*/_interopDefaultCompat(babelGenerator);
58
58
  const groupBy__default = /*#__PURE__*/_interopDefaultCompat(groupBy);
@@ -777,8 +777,11 @@ class PageContext {
777
777
  const generatedPageMetaData = await Promise.all(jobs);
778
778
  const customPageMetaData = overrides || [];
779
779
  const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
780
- const parseMeta = result.filter(
781
- (page, index, self) => self.findLastIndex((item) => page.path === item.path) === index
780
+ const parseMeta = Array.from(
781
+ result.reduce((map, page) => {
782
+ map.set(page.path, page);
783
+ return map;
784
+ }, /* @__PURE__ */ new Map()).values()
782
785
  );
783
786
  return type === "main" ? this.setHomePage(parseMeta) : parseMeta;
784
787
  }
@@ -932,35 +935,13 @@ class PageContext {
932
935
  debug.declaration("generating");
933
936
  return writeDeclaration(this, this.options.dts);
934
937
  }
935
- async writePagesJSONFile(pagesJson, retry = 3) {
936
- if (retry <= 0) {
937
- debug.error(`${this.resolvedPagesJSONPath} \u83B7\u53D6\u6587\u4EF6\u9501\u5931\u8D25\uFF0C\u5199\u5165\u5931\u8D25`);
938
- return;
939
- }
940
- let relase;
941
- try {
942
- try {
943
- relase = await lockfile__default.lock(this.resolvedPagesJSONPath, { realpath: false });
944
- } catch {
945
- return this.writePagesJSONFile(pagesJson, retry - 1);
946
- }
947
- await fs__default.promises.writeFile(this.resolvedPagesJSONPath, pagesJson, { encoding: "utf-8" });
948
- } finally {
949
- if (relase) {
950
- await relase();
951
- }
952
- }
953
- }
954
938
  async genratePagesJSON() {
955
939
  const content = await fs__default.promises.readFile(this.resolvedPagesJSONPath, { encoding: "utf-8" }).catch(() => "");
956
- let pageJson = commentJson.parse(content || "{}");
957
- const { pages: _, subPackages: __, tabBar: ___, ...others } = this.pagesGlobConfig || {};
958
- pageJson = Object.assign(pageJson, {
959
- ...others
960
- });
940
+ const { pages: oldPages, subPackages: oldSubPackages, tabBar: oldTabBar } = commentJson.parse(content || "{}");
941
+ const { pages: _, subPackages: __, tabBar: ___, ...pageJson } = this.pagesGlobConfig || {};
961
942
  const currentPlatform = uniEnv.platform.toUpperCase();
962
- pageJson.pages = mergePlatformItems(pageJson?.pages, currentPlatform, this.pageMetaData, "path");
963
- pageJson.subPackages = pageJson?.subPackages || new commentJson.CommentArray();
943
+ pageJson.pages = mergePlatformItems(oldPages, currentPlatform, this.pageMetaData, "path");
944
+ pageJson.subPackages = oldSubPackages || new commentJson.CommentArray();
964
945
  const newSubPackages = /* @__PURE__ */ new Map();
965
946
  for (const item of this.subPageMetaData) {
966
947
  newSubPackages.set(item.root, item);
@@ -978,15 +959,17 @@ class PageContext {
978
959
  pages: mergePlatformItems(void 0, currentPlatform, newSub.pages, "path")
979
960
  });
980
961
  }
981
- const tabBar = await this.getTabBarMerged();
982
- if (tabBar) {
983
- const list = mergePlatformItems(pageJson?.tabBar?.list, currentPlatform, tabBar.list, "pagePath");
984
- if (pageJson?.tabBar?.list) {
985
- pageJson.tabBar.list = list;
986
- } else {
987
- pageJson.tabBar = pageJson.tabBar || {};
988
- pageJson.tabBar.list = list;
989
- }
962
+ const { list, ...tabBarOthers } = await this.getTabBarMerged() || {};
963
+ if (list) {
964
+ const { list: oldList } = oldTabBar || {};
965
+ const newList = mergePlatformItems(oldList, currentPlatform, list, "pagePath");
966
+ pageJson.tabBar = {
967
+ ...tabBarOthers,
968
+ // 每次都直接更新除 list 外的其他属性
969
+ list: newList
970
+ };
971
+ } else {
972
+ pageJson.tabBar = void 0;
990
973
  }
991
974
  return pageJson;
992
975
  }
@@ -1056,7 +1039,7 @@ function mergePlatformItems(source, currentPlatform, items, uniqueKeyName) {
1056
1039
  continue;
1057
1040
  }
1058
1041
  const existing = tmpMap.get(uniqueKey) || [];
1059
- existing.push({ item, platforms, platformStr: platforms.join(" || ") });
1042
+ existing.push({ item, itemStr: JSON.stringify(item), platforms, platformStr: platforms.join(" || ") });
1060
1043
  tmpMap.set(uniqueKey, existing);
1061
1044
  }
1062
1045
  for (const item of items) {
@@ -1068,6 +1051,7 @@ function mergePlatformItems(source, currentPlatform, items, uniqueKeyName) {
1068
1051
  if (!tmpMap.has(uniqueKey)) {
1069
1052
  tmpMap.set(uniqueKey, [{
1070
1053
  item: newItem,
1054
+ itemStr: JSON.stringify(newItem),
1071
1055
  platforms: [currentPlatform],
1072
1056
  platformStr: currentPlatform
1073
1057
  }]);
@@ -1075,13 +1059,14 @@ function mergePlatformItems(source, currentPlatform, items, uniqueKeyName) {
1075
1059
  }
1076
1060
  const existing = tmpMap.get(uniqueKey);
1077
1061
  const newItemStr = JSON.stringify(newItem);
1078
- const equalObj = existing.find((val) => JSON.stringify(val.item) === newItemStr);
1062
+ const equalObj = existing.find((val) => val.itemStr === newItemStr);
1079
1063
  if (equalObj) {
1080
1064
  equalObj.platforms.push(currentPlatform);
1081
1065
  equalObj.platformStr = equalObj.platforms.join(" || ");
1082
1066
  } else {
1083
1067
  existing.push({
1084
1068
  item: newItem,
1069
+ itemStr: newItemStr,
1085
1070
  platforms: [currentPlatform],
1086
1071
  platformStr: currentPlatform
1087
1072
  });
package/dist/index.d.cts CHANGED
@@ -2995,7 +2995,6 @@ declare class PageContext {
2995
2995
  resolveRoutes(): string;
2996
2996
  resolveSubRoutes(): string;
2997
2997
  generateDeclaration(): Promise<void> | undefined;
2998
- private writePagesJSONFile;
2999
2998
  private genratePagesJSON;
3000
2999
  private readInfoFromPagesJSON;
3001
3000
  private getIndent;
@@ -3153,6 +3152,9 @@ interface PageMetaDatum {
3153
3152
  needLogin?: boolean;
3154
3153
  [x: string]: any;
3155
3154
  }
3155
+ type ExcludeIndexSignature<T> = {
3156
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
3157
+ };
3156
3158
  interface SubPageMetaDatum {
3157
3159
  root: string;
3158
3160
  pages: PageMetaDatum[];
@@ -3215,4 +3217,4 @@ declare function resolvePageDirs(dir: string, root: string, exclude: string[]):
3215
3217
  declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
3216
3218
 
3217
3219
  export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, Page, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, babelGenerate, checkPagesJsonFileSync, debug, VitePluginUniPages as default, definePage, defineUniPages, extsToGlob, findImports, findMacro, getPageFiles, getRouteBlock, getRouteSfcBlock, invalidatePagesModule, isTargetFile, mergePageMetaDataArray, parseCode, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, sleep, tryPageMetaFromCustomBlock, tryPageMetaFromMacro, writeFileWithLock };
3218
- export type { Condition, ConditionItem, ConfigSource, CustomBlock, DefinePage, EasyCom, GlobalStyle, MaybeCallable, MaybePromise, MaybePromiseCallable, Options, PageMetaDatum, PagePath, PagesConfig, ResolvedOptions, RouteBlockLang, SubPackage, SubPackages, SubPageMetaDatum, TabBar, TabBarIconFont, TabBarItem, TabBarMidButton, TheWindow, UniIdRouter, UserOptions, UserPageMeta, UserPagesConfig, UserTabBarItem, debugType };
3220
+ export type { Condition, ConditionItem, ConfigSource, CustomBlock, DefinePage, EasyCom, ExcludeIndexSignature, GlobalStyle, MaybeCallable, MaybePromise, MaybePromiseCallable, Options, PageMetaDatum, PagePath, PagesConfig, ResolvedOptions, RouteBlockLang, SubPackage, SubPackages, SubPageMetaDatum, TabBar, TabBarIconFont, TabBarItem, TabBarMidButton, TheWindow, UniIdRouter, UserOptions, UserPageMeta, UserPagesConfig, UserTabBarItem, debugType };
package/dist/index.d.mts CHANGED
@@ -2995,7 +2995,6 @@ declare class PageContext {
2995
2995
  resolveRoutes(): string;
2996
2996
  resolveSubRoutes(): string;
2997
2997
  generateDeclaration(): Promise<void> | undefined;
2998
- private writePagesJSONFile;
2999
2998
  private genratePagesJSON;
3000
2999
  private readInfoFromPagesJSON;
3001
3000
  private getIndent;
@@ -3153,6 +3152,9 @@ interface PageMetaDatum {
3153
3152
  needLogin?: boolean;
3154
3153
  [x: string]: any;
3155
3154
  }
3155
+ type ExcludeIndexSignature<T> = {
3156
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
3157
+ };
3156
3158
  interface SubPageMetaDatum {
3157
3159
  root: string;
3158
3160
  pages: PageMetaDatum[];
@@ -3215,4 +3217,4 @@ declare function resolvePageDirs(dir: string, root: string, exclude: string[]):
3215
3217
  declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
3216
3218
 
3217
3219
  export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, Page, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, babelGenerate, checkPagesJsonFileSync, debug, VitePluginUniPages as default, definePage, defineUniPages, extsToGlob, findImports, findMacro, getPageFiles, getRouteBlock, getRouteSfcBlock, invalidatePagesModule, isTargetFile, mergePageMetaDataArray, parseCode, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, sleep, tryPageMetaFromCustomBlock, tryPageMetaFromMacro, writeFileWithLock };
3218
- export type { Condition, ConditionItem, ConfigSource, CustomBlock, DefinePage, EasyCom, GlobalStyle, MaybeCallable, MaybePromise, MaybePromiseCallable, Options, PageMetaDatum, PagePath, PagesConfig, ResolvedOptions, RouteBlockLang, SubPackage, SubPackages, SubPageMetaDatum, TabBar, TabBarIconFont, TabBarItem, TabBarMidButton, TheWindow, UniIdRouter, UserOptions, UserPageMeta, UserPagesConfig, UserTabBarItem, debugType };
3220
+ export type { Condition, ConditionItem, ConfigSource, CustomBlock, DefinePage, EasyCom, ExcludeIndexSignature, GlobalStyle, MaybeCallable, MaybePromise, MaybePromiseCallable, Options, PageMetaDatum, PagePath, PagesConfig, ResolvedOptions, RouteBlockLang, SubPackage, SubPackages, SubPageMetaDatum, TabBar, TabBarIconFont, TabBarItem, TabBarMidButton, TheWindow, UniIdRouter, UserOptions, UserPageMeta, UserPagesConfig, UserTabBarItem, debugType };
package/dist/index.d.ts CHANGED
@@ -2995,7 +2995,6 @@ declare class PageContext {
2995
2995
  resolveRoutes(): string;
2996
2996
  resolveSubRoutes(): string;
2997
2997
  generateDeclaration(): Promise<void> | undefined;
2998
- private writePagesJSONFile;
2999
2998
  private genratePagesJSON;
3000
2999
  private readInfoFromPagesJSON;
3001
3000
  private getIndent;
@@ -3153,6 +3152,9 @@ interface PageMetaDatum {
3153
3152
  needLogin?: boolean;
3154
3153
  [x: string]: any;
3155
3154
  }
3155
+ type ExcludeIndexSignature<T> = {
3156
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
3157
+ };
3156
3158
  interface SubPageMetaDatum {
3157
3159
  root: string;
3158
3160
  pages: PageMetaDatum[];
@@ -3215,4 +3217,4 @@ declare function resolvePageDirs(dir: string, root: string, exclude: string[]):
3215
3217
  declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
3216
3218
 
3217
3219
  export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, Page, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, babelGenerate, checkPagesJsonFileSync, debug, VitePluginUniPages as default, definePage, defineUniPages, extsToGlob, findImports, findMacro, getPageFiles, getRouteBlock, getRouteSfcBlock, invalidatePagesModule, isTargetFile, mergePageMetaDataArray, parseCode, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, sleep, tryPageMetaFromCustomBlock, tryPageMetaFromMacro, writeFileWithLock };
3218
- export type { Condition, ConditionItem, ConfigSource, CustomBlock, DefinePage, EasyCom, GlobalStyle, MaybeCallable, MaybePromise, MaybePromiseCallable, Options, PageMetaDatum, PagePath, PagesConfig, ResolvedOptions, RouteBlockLang, SubPackage, SubPackages, SubPageMetaDatum, TabBar, TabBarIconFont, TabBarItem, TabBarMidButton, TheWindow, UniIdRouter, UserOptions, UserPageMeta, UserPagesConfig, UserTabBarItem, debugType };
3220
+ export type { Condition, ConditionItem, ConfigSource, CustomBlock, DefinePage, EasyCom, ExcludeIndexSignature, GlobalStyle, MaybeCallable, MaybePromise, MaybePromiseCallable, Options, PageMetaDatum, PagePath, PagesConfig, ResolvedOptions, RouteBlockLang, SubPackage, SubPackages, SubPageMetaDatum, TabBar, TabBarIconFont, TabBarItem, TabBarMidButton, TheWindow, UniIdRouter, UserOptions, UserPageMeta, UserPagesConfig, UserTabBarItem, debugType };
package/dist/index.mjs CHANGED
@@ -11,10 +11,10 @@ import { parse, assign, stringify, CommentArray } from 'comment-json';
11
11
  import Debug from 'debug';
12
12
  import detectIndent from 'detect-indent';
13
13
  import detectNewline from 'detect-newline';
14
- import lockfile from 'proper-lockfile';
15
14
  import { loadConfig } from 'unconfig';
16
15
  import { readFile, mkdir, writeFile as writeFile$1 } from 'node:fs/promises';
17
16
  import fg from 'fast-glob';
17
+ import lockfile from 'proper-lockfile';
18
18
  import { createRequire } from 'node:module';
19
19
  import vm from 'node:vm';
20
20
  import babelGenerator from '@babel/generator';
@@ -742,8 +742,11 @@ class PageContext {
742
742
  const generatedPageMetaData = await Promise.all(jobs);
743
743
  const customPageMetaData = overrides || [];
744
744
  const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
745
- const parseMeta = result.filter(
746
- (page, index, self) => self.findLastIndex((item) => page.path === item.path) === index
745
+ const parseMeta = Array.from(
746
+ result.reduce((map, page) => {
747
+ map.set(page.path, page);
748
+ return map;
749
+ }, /* @__PURE__ */ new Map()).values()
747
750
  );
748
751
  return type === "main" ? this.setHomePage(parseMeta) : parseMeta;
749
752
  }
@@ -897,35 +900,13 @@ class PageContext {
897
900
  debug.declaration("generating");
898
901
  return writeDeclaration(this, this.options.dts);
899
902
  }
900
- async writePagesJSONFile(pagesJson, retry = 3) {
901
- if (retry <= 0) {
902
- debug.error(`${this.resolvedPagesJSONPath} \u83B7\u53D6\u6587\u4EF6\u9501\u5931\u8D25\uFF0C\u5199\u5165\u5931\u8D25`);
903
- return;
904
- }
905
- let relase;
906
- try {
907
- try {
908
- relase = await lockfile.lock(this.resolvedPagesJSONPath, { realpath: false });
909
- } catch {
910
- return this.writePagesJSONFile(pagesJson, retry - 1);
911
- }
912
- await fs.promises.writeFile(this.resolvedPagesJSONPath, pagesJson, { encoding: "utf-8" });
913
- } finally {
914
- if (relase) {
915
- await relase();
916
- }
917
- }
918
- }
919
903
  async genratePagesJSON() {
920
904
  const content = await fs.promises.readFile(this.resolvedPagesJSONPath, { encoding: "utf-8" }).catch(() => "");
921
- let pageJson = parse(content || "{}");
922
- const { pages: _, subPackages: __, tabBar: ___, ...others } = this.pagesGlobConfig || {};
923
- pageJson = Object.assign(pageJson, {
924
- ...others
925
- });
905
+ const { pages: oldPages, subPackages: oldSubPackages, tabBar: oldTabBar } = parse(content || "{}");
906
+ const { pages: _, subPackages: __, tabBar: ___, ...pageJson } = this.pagesGlobConfig || {};
926
907
  const currentPlatform = platform.toUpperCase();
927
- pageJson.pages = mergePlatformItems(pageJson?.pages, currentPlatform, this.pageMetaData, "path");
928
- pageJson.subPackages = pageJson?.subPackages || new CommentArray();
908
+ pageJson.pages = mergePlatformItems(oldPages, currentPlatform, this.pageMetaData, "path");
909
+ pageJson.subPackages = oldSubPackages || new CommentArray();
929
910
  const newSubPackages = /* @__PURE__ */ new Map();
930
911
  for (const item of this.subPageMetaData) {
931
912
  newSubPackages.set(item.root, item);
@@ -943,15 +924,17 @@ class PageContext {
943
924
  pages: mergePlatformItems(void 0, currentPlatform, newSub.pages, "path")
944
925
  });
945
926
  }
946
- const tabBar = await this.getTabBarMerged();
947
- if (tabBar) {
948
- const list = mergePlatformItems(pageJson?.tabBar?.list, currentPlatform, tabBar.list, "pagePath");
949
- if (pageJson?.tabBar?.list) {
950
- pageJson.tabBar.list = list;
951
- } else {
952
- pageJson.tabBar = pageJson.tabBar || {};
953
- pageJson.tabBar.list = list;
954
- }
927
+ const { list, ...tabBarOthers } = await this.getTabBarMerged() || {};
928
+ if (list) {
929
+ const { list: oldList } = oldTabBar || {};
930
+ const newList = mergePlatformItems(oldList, currentPlatform, list, "pagePath");
931
+ pageJson.tabBar = {
932
+ ...tabBarOthers,
933
+ // 每次都直接更新除 list 外的其他属性
934
+ list: newList
935
+ };
936
+ } else {
937
+ pageJson.tabBar = void 0;
955
938
  }
956
939
  return pageJson;
957
940
  }
@@ -1021,7 +1004,7 @@ function mergePlatformItems(source, currentPlatform, items, uniqueKeyName) {
1021
1004
  continue;
1022
1005
  }
1023
1006
  const existing = tmpMap.get(uniqueKey) || [];
1024
- existing.push({ item, platforms, platformStr: platforms.join(" || ") });
1007
+ existing.push({ item, itemStr: JSON.stringify(item), platforms, platformStr: platforms.join(" || ") });
1025
1008
  tmpMap.set(uniqueKey, existing);
1026
1009
  }
1027
1010
  for (const item of items) {
@@ -1033,6 +1016,7 @@ function mergePlatformItems(source, currentPlatform, items, uniqueKeyName) {
1033
1016
  if (!tmpMap.has(uniqueKey)) {
1034
1017
  tmpMap.set(uniqueKey, [{
1035
1018
  item: newItem,
1019
+ itemStr: JSON.stringify(newItem),
1036
1020
  platforms: [currentPlatform],
1037
1021
  platformStr: currentPlatform
1038
1022
  }]);
@@ -1040,13 +1024,14 @@ function mergePlatformItems(source, currentPlatform, items, uniqueKeyName) {
1040
1024
  }
1041
1025
  const existing = tmpMap.get(uniqueKey);
1042
1026
  const newItemStr = JSON.stringify(newItem);
1043
- const equalObj = existing.find((val) => JSON.stringify(val.item) === newItemStr);
1027
+ const equalObj = existing.find((val) => val.itemStr === newItemStr);
1044
1028
  if (equalObj) {
1045
1029
  equalObj.platforms.push(currentPlatform);
1046
1030
  equalObj.platformStr = equalObj.platforms.join(" || ");
1047
1031
  } else {
1048
1032
  existing.push({
1049
1033
  item: newItem,
1034
+ itemStr: newItemStr,
1050
1035
  platforms: [currentPlatform],
1051
1036
  platformStr: currentPlatform
1052
1037
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@uni-helper/vite-plugin-uni-pages",
3
3
  "type": "module",
4
- "version": "0.3.18",
4
+ "version": "0.3.20",
5
5
  "description": "File system based routing for uni-app applications using Vite",
6
6
  "author": "KeJun",
7
7
  "maintainers": [