@uni-helper/vite-plugin-uni-pages 0.1.6 → 0.1.8

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
@@ -2,28 +2,33 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- const path = require('path');
5
+ const path$1 = require('path');
6
6
  const child_process = require('child_process');
7
7
  const vite = require('vite');
8
8
  const MagicString = require('magic-string');
9
9
  const chokidar = require('chokidar');
10
+ const path = require('node:path');
10
11
  const unconfig = require('unconfig');
12
+ const uniEnv = require('@uni-helper/uni-env');
11
13
  const Debug = require('debug');
12
- const lodash = require('lodash');
14
+ const lodashUnified = require('lodash-unified');
13
15
  const fg = require('fast-glob');
14
16
  const fs = require('fs');
17
+ const fs$1 = require('node:fs');
15
18
  const JSON5 = require('json5');
16
19
  const yaml = require('yaml');
17
20
  const compilerSfc = require('@vue/compiler-sfc');
18
21
 
19
22
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
20
23
 
21
- const path__default = /*#__PURE__*/_interopDefaultCompat(path);
24
+ const path__default$1 = /*#__PURE__*/_interopDefaultCompat(path$1);
22
25
  const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
23
26
  const chokidar__default = /*#__PURE__*/_interopDefaultCompat(chokidar);
27
+ const path__default = /*#__PURE__*/_interopDefaultCompat(path);
24
28
  const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
25
29
  const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
26
30
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
31
+ const fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
27
32
  const JSON5__default = /*#__PURE__*/_interopDefaultCompat(JSON5);
28
33
 
29
34
  function slash(str) {
@@ -50,6 +55,7 @@ const debug = {
50
55
  routeBlock: Debug__default("vite-plugin-uni-pages:routeBlock"),
51
56
  options: Debug__default("vite-plugin-uni-pages:options"),
52
57
  pages: Debug__default("vite-plugin-uni-pages:pages"),
58
+ subPages: Debug__default("vite-plugin-uni-pages:subPages"),
53
59
  error: Debug__default("vite-plugin-uni-pages:error")
54
60
  };
55
61
  function extsToGlob(extensions) {
@@ -63,7 +69,7 @@ function isConfigFile(path) {
63
69
  return path.includes("pages.config");
64
70
  }
65
71
  function mergePageMetaDataArray(pageMetaData) {
66
- const pageMetaDataObj = lodash.groupBy(pageMetaData, "path");
72
+ const pageMetaDataObj = lodashUnified.groupBy(pageMetaData, "path");
67
73
  const result = [];
68
74
  for (const path in pageMetaDataObj) {
69
75
  const _pageMetaData = pageMetaDataObj[path];
@@ -76,22 +82,24 @@ function mergePageMetaDataArray(pageMetaData) {
76
82
  }
77
83
  return result;
78
84
  }
79
- const getPagesConfigSourcePaths = async () => {
85
+ async function getPagesConfigSourcePaths() {
80
86
  return await fg__default("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
81
87
  deep: 0,
82
88
  onlyFiles: true,
83
89
  absolute: true
84
90
  });
85
- };
91
+ }
86
92
 
87
93
  function resolveOptions(userOptions, viteRoot) {
88
94
  const {
89
95
  mergePages = true,
90
96
  dir = "src/pages",
97
+ subPackages = [],
91
98
  outDir = "src",
92
99
  exclude = ["node_modules", ".git", "**/__*__/**"],
93
100
  routeBlockLang = "json5",
94
101
  minify = false,
102
+ debug = false,
95
103
  onBeforeLoadUserConfig = () => {
96
104
  },
97
105
  onAfterLoadUserConfig = () => {
@@ -111,14 +119,17 @@ function resolveOptions(userOptions, viteRoot) {
111
119
  } = userOptions;
112
120
  const root = viteRoot || slash(process.cwd());
113
121
  const resolvedDirs = resolvePageDirs(dir, root, exclude);
122
+ const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
114
123
  const resolvedOptions = {
115
124
  mergePages,
116
125
  dirs: resolvedDirs,
126
+ subPackages: resolvedSubDirs,
117
127
  outDir,
118
128
  exclude,
119
129
  routeBlockLang,
120
130
  root,
121
131
  minify,
132
+ debug,
122
133
  onBeforeLoadUserConfig,
123
134
  onAfterLoadUserConfig,
124
135
  onBeforeScanPages,
@@ -183,7 +194,7 @@ function parseCustomBlock(block, filePath, options) {
183
194
  };
184
195
  let content;
185
196
  debug.routeBlock(`use ${lang} parser`);
186
- if (lang === "json5") {
197
+ if (lang === "json5" || lang === "jsonc") {
187
198
  try {
188
199
  content = JSON5__default.parse(block.content);
189
200
  } catch (err) {
@@ -219,7 +230,7 @@ ${err.message}`
219
230
  }
220
231
  }
221
232
  async function getRouteBlock(path, options) {
222
- const content = fs__default.readFileSync(path, "utf8");
233
+ const content = fs__default$1.readFileSync(path, "utf8");
223
234
  const parsedSFC = await parseSFC(content);
224
235
  const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
225
236
  if (!blockStr)
@@ -231,12 +242,20 @@ async function getRouteBlock(path, options) {
231
242
  class PageContext {
232
243
  constructor(userOptions, viteRoot = process.cwd()) {
233
244
  this.pagesPath = [];
245
+ this.subPagesPath = {};
234
246
  this.pageMetaData = [];
247
+ this.subPageMetaData = [];
235
248
  this.resolvedPagesJSONPath = "";
236
249
  this.rawOptions = userOptions;
237
250
  this.root = slash(viteRoot);
238
251
  debug.options("root", this.root);
239
252
  this.options = resolveOptions(userOptions, this.root);
253
+ const debugOption = this.options.debug;
254
+ if (debugOption) {
255
+ const prefix = "vite-plugin-uni-pages:";
256
+ const suffix = typeof debugOption === "boolean" ? "*" : debugOption;
257
+ Debug__default.enable(`${prefix}${suffix}`);
258
+ }
240
259
  this.resolvedPagesJSONPath = path__default.join(this.root, this.options.outDir, OUTPUT_NAME);
241
260
  debug.options(this.options);
242
261
  }
@@ -254,19 +273,20 @@ class PageContext {
254
273
  }
255
274
  async scanPages() {
256
275
  const pageDirFiles = this.options.dirs.map((dir) => {
257
- const pagesDirPath = slash(path__default.resolve(this.options.root, dir));
258
- const basePath = slash(path__default.join(this.options.root, this.options.outDir));
259
- const files = getPageFiles(pagesDirPath, this.options);
260
- debug.pages(dir, files);
261
- const pagePaths = files.map((file) => slash(file)).map((file) => ({
262
- relativePath: path__default.relative(basePath, slash(path__default.resolve(pagesDirPath, file))),
263
- absolutePath: slash(path__default.resolve(pagesDirPath, file))
264
- }));
265
- return { dir, files: pagePaths };
276
+ return { dir, files: getPagePaths(dir, this.options) };
266
277
  });
267
278
  this.pagesPath = pageDirFiles.map((page) => page.files).flat();
268
279
  debug.pages(this.pagesPath);
269
280
  }
281
+ async scanSubPages() {
282
+ const subPagesPath = {};
283
+ for (const dir of this.options.subPackages) {
284
+ const pagePaths = getPagePaths(dir, this.options);
285
+ subPagesPath[dir] = pagePaths;
286
+ }
287
+ this.subPagesPath = subPagesPath;
288
+ debug.subPages(this.subPagesPath);
289
+ }
270
290
  setupViteServer(server) {
271
291
  if (this._server === server)
272
292
  return;
@@ -274,7 +294,7 @@ class PageContext {
274
294
  this.setupWatcher(server.watcher);
275
295
  }
276
296
  async setupWatcher(watcher) {
277
- if (process.env.UNI_PLATFORM !== "h5") {
297
+ if (!uniEnv.isH5) {
278
298
  const configs = await getPagesConfigSourcePaths();
279
299
  watcher.add(configs);
280
300
  }
@@ -321,21 +341,36 @@ class PageContext {
321
341
  Object.assign(pageMetaDatum, routeBlock.content);
322
342
  return pageMetaDatum;
323
343
  }
324
- async mergePageMetaData() {
344
+ async parsePages(pages, overrides) {
325
345
  const generatedPageMetaData = await Promise.all(
326
- this.pagesPath.map(async (page) => await this.parsePage(page))
346
+ pages.map(async (page) => await this.parsePage(page))
327
347
  );
328
- const customPageMetaData = this.pagesGlobConfig?.pages || [];
329
- if (!this.options.mergePages) {
330
- this.pageMetaData = customPageMetaData;
331
- } else {
332
- if (this.pagesGlobConfig?.pages)
333
- this.pageMetaData = mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData));
334
- else
335
- this.pageMetaData = generatedPageMetaData;
348
+ const customPageMetaData = overrides || [];
349
+ const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
350
+ result.sort((page) => page.type === "home" ? -1 : 0);
351
+ return result;
352
+ }
353
+ async mergePageMetaData() {
354
+ const pageMetaData = await this.parsePages(this.pagesPath, this.pagesGlobConfig?.pages);
355
+ this.pageMetaData = pageMetaData;
356
+ debug.pages(this.pageMetaData);
357
+ }
358
+ async mergeSubPageMetaData() {
359
+ const subPageMaps = {};
360
+ const subPackages = this.pagesGlobConfig?.subPackages || [];
361
+ for (const [dir, pages] of Object.entries(this.subPagesPath)) {
362
+ const root = path__default.basename(dir);
363
+ const globPackage = subPackages?.find((v) => v.root === root);
364
+ subPageMaps[root] = await this.parsePages(pages, globPackage?.pages);
365
+ subPageMaps[root] = subPageMaps[root].map((page) => ({ ...page, path: slash(path__default.relative(root, page.path)) }));
336
366
  }
337
- this.pageMetaData.sort((page) => page.type === "home" ? -1 : 0);
338
- debug.pages(generatedPageMetaData);
367
+ for (const { root, pages } of subPackages) {
368
+ if (root && !subPageMaps[root])
369
+ subPageMaps[root] = pages || [];
370
+ }
371
+ const subPageMetaData = Object.keys(subPageMaps).map((root) => ({ root, pages: subPageMaps[root] }));
372
+ this.subPageMetaData = subPageMetaData;
373
+ debug.subPages(this.subPageMetaData);
339
374
  }
340
375
  async updatePagesJSON() {
341
376
  checkPagesJsonFile(this.resolvedPagesJSONPath);
@@ -345,13 +380,20 @@ class PageContext {
345
380
  if (this.options.mergePages) {
346
381
  this.options.onBeforeScanPages(this);
347
382
  await this.scanPages();
383
+ await this.scanSubPages();
348
384
  this.options.onAfterScanPages(this);
349
385
  }
350
386
  this.options.onBeforeMergePageMetaData(this);
351
387
  await this.mergePageMetaData();
388
+ await this.mergeSubPageMetaData();
352
389
  this.options.onAfterMergePageMetaData(this);
353
390
  this.options.onBeforeWriteFile(this);
354
- const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, this.options.minify ? void 0 : 2);
391
+ const data = {
392
+ ...this.pagesGlobConfig,
393
+ pages: this.pageMetaData,
394
+ subPackages: this.subPageMetaData
395
+ };
396
+ const pagesJson = JSON.stringify(data, null, this.options.minify ? void 0 : 2);
355
397
  writeFileSync(this.resolvedPagesJSONPath, pagesJson);
356
398
  this.options.onAfterWriteFile(this);
357
399
  }
@@ -362,6 +404,17 @@ class PageContext {
362
404
  return JSON.stringify(this.pageMetaData, null, 2);
363
405
  }
364
406
  }
407
+ function getPagePaths(dir, options) {
408
+ const pagesDirPath = slash(path__default.resolve(options.root, dir));
409
+ const basePath = slash(path__default.join(options.root, options.outDir));
410
+ const files = getPageFiles(pagesDirPath, options);
411
+ debug.pages(dir, files);
412
+ const pagePaths = files.map((file) => slash(file)).map((file) => ({
413
+ relativePath: path__default.relative(basePath, slash(path__default.resolve(pagesDirPath, file))),
414
+ absolutePath: slash(path__default.resolve(pagesDirPath, file))
415
+ }));
416
+ return pagePaths;
417
+ }
365
418
 
366
419
  function defineUniPages(config) {
367
420
  return config;
@@ -383,7 +436,7 @@ async function restart() {
383
436
  }
384
437
  const VitePluginUniPages = (userOptions = {}) => {
385
438
  let ctx;
386
- const resolvedPagesJSONPath = path__default.join(
439
+ const resolvedPagesJSONPath = path__default$1.join(
387
440
  process.cwd(),
388
441
  userOptions.outDir ?? "src",
389
442
  OUTPUT_NAME
package/dist/index.d.ts CHANGED
@@ -639,7 +639,9 @@ declare class PageContext {
639
639
  private _server;
640
640
  pagesGlobConfig: PagesConfig | undefined;
641
641
  pagesPath: PagePath[];
642
+ subPagesPath: Record<string, PagePath[]>;
642
643
  pageMetaData: PageMetaDatum[];
644
+ subPageMetaData: SubPageMetaDatum[];
643
645
  resolvedPagesJSONPath: string;
644
646
  rawOptions: UserOptions;
645
647
  root: string;
@@ -649,20 +651,44 @@ declare class PageContext {
649
651
  setLogger(logger: Logger): void;
650
652
  loadUserPagesConfig(): Promise<void>;
651
653
  scanPages(): Promise<void>;
654
+ scanSubPages(): Promise<void>;
652
655
  setupViteServer(server: ViteDevServer): void;
653
656
  setupWatcher(watcher: FSWatcher): Promise<void>;
654
657
  onUpdate(): void;
655
658
  parsePage(page: PagePath): Promise<PageMetaDatum>;
659
+ parsePages(pages: PagePath[], overrides?: PageMetaDatum[]): Promise<PageMetaDatum[]>;
656
660
  mergePageMetaData(): Promise<void>;
661
+ mergeSubPageMetaData(): Promise<void>;
657
662
  updatePagesJSON(): Promise<void>;
658
663
  virtualModule(): Promise<string>;
659
664
  resolveRoutes(): string;
660
665
  }
661
666
 
667
+ declare function invalidatePagesModule(server: ViteDevServer): void;
668
+ declare const debug: {
669
+ hmr: Debug.Debugger;
670
+ routeBlock: Debug.Debugger;
671
+ options: Debug.Debugger;
672
+ pages: Debug.Debugger;
673
+ subPages: Debug.Debugger;
674
+ error: Debug.Debugger;
675
+ };
676
+ declare function extsToGlob(extensions: string[]): string;
677
+ declare function isTargetFile(path: string): boolean;
678
+ declare function isConfigFile(path: string): boolean;
679
+ /**
680
+ * merge page meta data array by path and assign style
681
+ * @param pageMetaData page meta data array
682
+ * TODO: support merge middleware
683
+ */
684
+ declare function mergePageMetaDataArray(pageMetaData: PageMetaDatum[]): PageMetaDatum[];
685
+ declare function getPagesConfigSourcePaths(): Promise<string[]>;
686
+
662
687
  interface CustomBlock {
663
688
  attr: Record<string, any>;
664
689
  content: Record<string, any>;
665
690
  }
691
+ type debugType = keyof typeof debug;
666
692
  interface Options {
667
693
  /**
668
694
  * Whether to merge pages in pages.json
@@ -674,6 +700,11 @@ interface Options {
674
700
  * @default 'src/pages'
675
701
  */
676
702
  dir: string;
703
+ /**
704
+ * all root directories loaded by subPackages
705
+ * @default []
706
+ */
707
+ subPackages: string[];
677
708
  /**
678
709
  * pages.json dir
679
710
  * @default "src"
@@ -694,6 +725,10 @@ interface Options {
694
725
  * @default false
695
726
  */
696
727
  minify: boolean;
728
+ /**
729
+ * enable debug log
730
+ */
731
+ debug: boolean | debugType;
697
732
  onBeforeLoadUserConfig: (ctx: PageContext) => void;
698
733
  onAfterLoadUserConfig: (ctx: PageContext) => void;
699
734
  onBeforeScanPages: (ctx: PageContext) => void;
@@ -735,31 +770,16 @@ interface PageMetaDatum {
735
770
  needLogin?: boolean;
736
771
  [x: string]: any;
737
772
  }
773
+ interface SubPageMetaDatum {
774
+ root: string;
775
+ pages: PageMetaDatum[];
776
+ }
738
777
 
739
778
  declare const MODULE_ID_VIRTUAL = "virtual:uni-pages";
740
779
  declare const RESOLVED_MODULE_ID_VIRTUAL: string;
741
780
  declare const OUTPUT_NAME = "pages.json";
742
781
  declare const FILE_EXTENSIONS: string[];
743
782
 
744
- declare function invalidatePagesModule(server: ViteDevServer): void;
745
- declare const debug: {
746
- hmr: Debug.Debugger;
747
- routeBlock: Debug.Debugger;
748
- options: Debug.Debugger;
749
- pages: Debug.Debugger;
750
- error: Debug.Debugger;
751
- };
752
- declare function extsToGlob(extensions: string[]): string;
753
- declare function isTargetFile(path: string): boolean;
754
- declare function isConfigFile(path: string): boolean;
755
- /**
756
- * merge page meta data array by path and assign style
757
- * @param pageMetaData page meta data array
758
- * TODO: support merge middleware
759
- */
760
- declare function mergePageMetaDataArray(pageMetaData: PageMetaDatum[]): PageMetaDatum[];
761
- declare const getPagesConfigSourcePaths: () => Promise<string[]>;
762
-
763
783
  /**
764
784
  * Resolves the files that are valid pages for the given context.
765
785
  */
@@ -779,4 +799,4 @@ declare function getRouteBlock(path: string, options: ResolvedOptions): Promise<
779
799
 
780
800
  declare const VitePluginUniPages: (userOptions?: UserOptions) => Plugin;
781
801
 
782
- export { Condition, ConditionItem, CustomBlock, EasyCom, FILE_EXTENSIONS, GlobalStyle, IconFont, MODULE_ID_VIRTUAL, MidButton, OUTPUT_NAME, Options, PageContext, PageMetaDatum, PagePath, PagesConfig, PreloadRule, RESOLVED_MODULE_ID_VIRTUAL, ResolvedOptions, SubPackage, TabBar, TabBarItem, TheWindow, UniIdRouter, UserOptions, UserPagesConfig, VitePluginUniPages, checkPagesJsonFile, debug, VitePluginUniPages as default, defineUniPages, extsToGlob, getPageFiles, getPagesConfigSourcePaths, getRouteBlock, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, writeFileSync };
802
+ export { Condition, ConditionItem, CustomBlock, EasyCom, FILE_EXTENSIONS, GlobalStyle, IconFont, MODULE_ID_VIRTUAL, MidButton, OUTPUT_NAME, Options, PageContext, PageMetaDatum, PagePath, PagesConfig, PreloadRule, RESOLVED_MODULE_ID_VIRTUAL, ResolvedOptions, SubPackage, SubPageMetaDatum, TabBar, TabBarItem, TheWindow, UniIdRouter, UserOptions, UserPagesConfig, VitePluginUniPages, checkPagesJsonFile, debug, debugType, VitePluginUniPages as default, defineUniPages, extsToGlob, getPageFiles, getPagesConfigSourcePaths, getRouteBlock, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, writeFileSync };
package/dist/index.mjs CHANGED
@@ -1,13 +1,16 @@
1
- import path from 'path';
1
+ import path$1 from 'path';
2
2
  import { spawn } from 'child_process';
3
3
  import { normalizePath, createLogger } from 'vite';
4
4
  import MagicString from 'magic-string';
5
5
  import chokidar from 'chokidar';
6
+ import path from 'node:path';
6
7
  import { loadConfig } from 'unconfig';
8
+ import { isH5 } from '@uni-helper/uni-env';
7
9
  import Debug from 'debug';
8
- import { groupBy } from 'lodash';
10
+ import { groupBy } from 'lodash-unified';
9
11
  import fg from 'fast-glob';
10
12
  import fs from 'fs';
13
+ import fs$1 from 'node:fs';
11
14
  import JSON5 from 'json5';
12
15
  import { parse as parse$1 } from 'yaml';
13
16
  import { parse } from '@vue/compiler-sfc';
@@ -36,6 +39,7 @@ const debug = {
36
39
  routeBlock: Debug("vite-plugin-uni-pages:routeBlock"),
37
40
  options: Debug("vite-plugin-uni-pages:options"),
38
41
  pages: Debug("vite-plugin-uni-pages:pages"),
42
+ subPages: Debug("vite-plugin-uni-pages:subPages"),
39
43
  error: Debug("vite-plugin-uni-pages:error")
40
44
  };
41
45
  function extsToGlob(extensions) {
@@ -62,22 +66,24 @@ function mergePageMetaDataArray(pageMetaData) {
62
66
  }
63
67
  return result;
64
68
  }
65
- const getPagesConfigSourcePaths = async () => {
69
+ async function getPagesConfigSourcePaths() {
66
70
  return await fg("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
67
71
  deep: 0,
68
72
  onlyFiles: true,
69
73
  absolute: true
70
74
  });
71
- };
75
+ }
72
76
 
73
77
  function resolveOptions(userOptions, viteRoot) {
74
78
  const {
75
79
  mergePages = true,
76
80
  dir = "src/pages",
81
+ subPackages = [],
77
82
  outDir = "src",
78
83
  exclude = ["node_modules", ".git", "**/__*__/**"],
79
84
  routeBlockLang = "json5",
80
85
  minify = false,
86
+ debug = false,
81
87
  onBeforeLoadUserConfig = () => {
82
88
  },
83
89
  onAfterLoadUserConfig = () => {
@@ -97,14 +103,17 @@ function resolveOptions(userOptions, viteRoot) {
97
103
  } = userOptions;
98
104
  const root = viteRoot || slash(process.cwd());
99
105
  const resolvedDirs = resolvePageDirs(dir, root, exclude);
106
+ const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
100
107
  const resolvedOptions = {
101
108
  mergePages,
102
109
  dirs: resolvedDirs,
110
+ subPackages: resolvedSubDirs,
103
111
  outDir,
104
112
  exclude,
105
113
  routeBlockLang,
106
114
  root,
107
115
  minify,
116
+ debug,
108
117
  onBeforeLoadUserConfig,
109
118
  onAfterLoadUserConfig,
110
119
  onBeforeScanPages,
@@ -169,7 +178,7 @@ function parseCustomBlock(block, filePath, options) {
169
178
  };
170
179
  let content;
171
180
  debug.routeBlock(`use ${lang} parser`);
172
- if (lang === "json5") {
181
+ if (lang === "json5" || lang === "jsonc") {
173
182
  try {
174
183
  content = JSON5.parse(block.content);
175
184
  } catch (err) {
@@ -205,7 +214,7 @@ ${err.message}`
205
214
  }
206
215
  }
207
216
  async function getRouteBlock(path, options) {
208
- const content = fs.readFileSync(path, "utf8");
217
+ const content = fs$1.readFileSync(path, "utf8");
209
218
  const parsedSFC = await parseSFC(content);
210
219
  const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
211
220
  if (!blockStr)
@@ -217,12 +226,20 @@ async function getRouteBlock(path, options) {
217
226
  class PageContext {
218
227
  constructor(userOptions, viteRoot = process.cwd()) {
219
228
  this.pagesPath = [];
229
+ this.subPagesPath = {};
220
230
  this.pageMetaData = [];
231
+ this.subPageMetaData = [];
221
232
  this.resolvedPagesJSONPath = "";
222
233
  this.rawOptions = userOptions;
223
234
  this.root = slash(viteRoot);
224
235
  debug.options("root", this.root);
225
236
  this.options = resolveOptions(userOptions, this.root);
237
+ const debugOption = this.options.debug;
238
+ if (debugOption) {
239
+ const prefix = "vite-plugin-uni-pages:";
240
+ const suffix = typeof debugOption === "boolean" ? "*" : debugOption;
241
+ Debug.enable(`${prefix}${suffix}`);
242
+ }
226
243
  this.resolvedPagesJSONPath = path.join(this.root, this.options.outDir, OUTPUT_NAME);
227
244
  debug.options(this.options);
228
245
  }
@@ -240,19 +257,20 @@ class PageContext {
240
257
  }
241
258
  async scanPages() {
242
259
  const pageDirFiles = this.options.dirs.map((dir) => {
243
- const pagesDirPath = slash(path.resolve(this.options.root, dir));
244
- const basePath = slash(path.join(this.options.root, this.options.outDir));
245
- const files = getPageFiles(pagesDirPath, this.options);
246
- debug.pages(dir, files);
247
- const pagePaths = files.map((file) => slash(file)).map((file) => ({
248
- relativePath: path.relative(basePath, slash(path.resolve(pagesDirPath, file))),
249
- absolutePath: slash(path.resolve(pagesDirPath, file))
250
- }));
251
- return { dir, files: pagePaths };
260
+ return { dir, files: getPagePaths(dir, this.options) };
252
261
  });
253
262
  this.pagesPath = pageDirFiles.map((page) => page.files).flat();
254
263
  debug.pages(this.pagesPath);
255
264
  }
265
+ async scanSubPages() {
266
+ const subPagesPath = {};
267
+ for (const dir of this.options.subPackages) {
268
+ const pagePaths = getPagePaths(dir, this.options);
269
+ subPagesPath[dir] = pagePaths;
270
+ }
271
+ this.subPagesPath = subPagesPath;
272
+ debug.subPages(this.subPagesPath);
273
+ }
256
274
  setupViteServer(server) {
257
275
  if (this._server === server)
258
276
  return;
@@ -260,7 +278,7 @@ class PageContext {
260
278
  this.setupWatcher(server.watcher);
261
279
  }
262
280
  async setupWatcher(watcher) {
263
- if (process.env.UNI_PLATFORM !== "h5") {
281
+ if (!isH5) {
264
282
  const configs = await getPagesConfigSourcePaths();
265
283
  watcher.add(configs);
266
284
  }
@@ -307,21 +325,36 @@ class PageContext {
307
325
  Object.assign(pageMetaDatum, routeBlock.content);
308
326
  return pageMetaDatum;
309
327
  }
310
- async mergePageMetaData() {
328
+ async parsePages(pages, overrides) {
311
329
  const generatedPageMetaData = await Promise.all(
312
- this.pagesPath.map(async (page) => await this.parsePage(page))
330
+ pages.map(async (page) => await this.parsePage(page))
313
331
  );
314
- const customPageMetaData = this.pagesGlobConfig?.pages || [];
315
- if (!this.options.mergePages) {
316
- this.pageMetaData = customPageMetaData;
317
- } else {
318
- if (this.pagesGlobConfig?.pages)
319
- this.pageMetaData = mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData));
320
- else
321
- this.pageMetaData = generatedPageMetaData;
332
+ const customPageMetaData = overrides || [];
333
+ const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
334
+ result.sort((page) => page.type === "home" ? -1 : 0);
335
+ return result;
336
+ }
337
+ async mergePageMetaData() {
338
+ const pageMetaData = await this.parsePages(this.pagesPath, this.pagesGlobConfig?.pages);
339
+ this.pageMetaData = pageMetaData;
340
+ debug.pages(this.pageMetaData);
341
+ }
342
+ async mergeSubPageMetaData() {
343
+ const subPageMaps = {};
344
+ const subPackages = this.pagesGlobConfig?.subPackages || [];
345
+ for (const [dir, pages] of Object.entries(this.subPagesPath)) {
346
+ const root = path.basename(dir);
347
+ const globPackage = subPackages?.find((v) => v.root === root);
348
+ subPageMaps[root] = await this.parsePages(pages, globPackage?.pages);
349
+ subPageMaps[root] = subPageMaps[root].map((page) => ({ ...page, path: slash(path.relative(root, page.path)) }));
322
350
  }
323
- this.pageMetaData.sort((page) => page.type === "home" ? -1 : 0);
324
- debug.pages(generatedPageMetaData);
351
+ for (const { root, pages } of subPackages) {
352
+ if (root && !subPageMaps[root])
353
+ subPageMaps[root] = pages || [];
354
+ }
355
+ const subPageMetaData = Object.keys(subPageMaps).map((root) => ({ root, pages: subPageMaps[root] }));
356
+ this.subPageMetaData = subPageMetaData;
357
+ debug.subPages(this.subPageMetaData);
325
358
  }
326
359
  async updatePagesJSON() {
327
360
  checkPagesJsonFile(this.resolvedPagesJSONPath);
@@ -331,13 +364,20 @@ class PageContext {
331
364
  if (this.options.mergePages) {
332
365
  this.options.onBeforeScanPages(this);
333
366
  await this.scanPages();
367
+ await this.scanSubPages();
334
368
  this.options.onAfterScanPages(this);
335
369
  }
336
370
  this.options.onBeforeMergePageMetaData(this);
337
371
  await this.mergePageMetaData();
372
+ await this.mergeSubPageMetaData();
338
373
  this.options.onAfterMergePageMetaData(this);
339
374
  this.options.onBeforeWriteFile(this);
340
- const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, this.options.minify ? void 0 : 2);
375
+ const data = {
376
+ ...this.pagesGlobConfig,
377
+ pages: this.pageMetaData,
378
+ subPackages: this.subPageMetaData
379
+ };
380
+ const pagesJson = JSON.stringify(data, null, this.options.minify ? void 0 : 2);
341
381
  writeFileSync(this.resolvedPagesJSONPath, pagesJson);
342
382
  this.options.onAfterWriteFile(this);
343
383
  }
@@ -348,6 +388,17 @@ class PageContext {
348
388
  return JSON.stringify(this.pageMetaData, null, 2);
349
389
  }
350
390
  }
391
+ function getPagePaths(dir, options) {
392
+ const pagesDirPath = slash(path.resolve(options.root, dir));
393
+ const basePath = slash(path.join(options.root, options.outDir));
394
+ const files = getPageFiles(pagesDirPath, options);
395
+ debug.pages(dir, files);
396
+ const pagePaths = files.map((file) => slash(file)).map((file) => ({
397
+ relativePath: path.relative(basePath, slash(path.resolve(pagesDirPath, file))),
398
+ absolutePath: slash(path.resolve(pagesDirPath, file))
399
+ }));
400
+ return pagePaths;
401
+ }
351
402
 
352
403
  function defineUniPages(config) {
353
404
  return config;
@@ -369,7 +420,7 @@ async function restart() {
369
420
  }
370
421
  const VitePluginUniPages = (userOptions = {}) => {
371
422
  let ctx;
372
- const resolvedPagesJSONPath = path.join(
423
+ const resolvedPagesJSONPath = path$1.join(
373
424
  process.cwd(),
374
425
  userOptions.outDir ?? "src",
375
426
  OUTPUT_NAME
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@uni-helper/vite-plugin-uni-pages",
3
3
  "type": "module",
4
- "version": "0.1.6",
5
- "packageManager": "pnpm@7.16.1",
4
+ "version": "0.1.8",
6
5
  "description": "File system based routing for uni-app applications using Vite",
7
6
  "author": "KeJun",
8
7
  "license": "MIT",
@@ -32,22 +31,22 @@
32
31
  "client.d.ts"
33
32
  ],
34
33
  "dependencies": {
35
- "@vue/compiler-sfc": "^3.2.45",
34
+ "@uni-helper/uni-env": "^0.0.2",
35
+ "@vue/compiler-sfc": "^3.2.47",
36
36
  "chokidar": "^3.5.3",
37
37
  "debug": "^4.3.4",
38
38
  "fast-glob": "^3.2.12",
39
39
  "json5": "^2.2.3",
40
- "lodash": "^4.17.21",
41
- "magic-string": "^0.27.0",
40
+ "lodash-unified": "^1.0.3",
41
+ "magic-string": "^0.30.0",
42
42
  "unconfig": "^0.3.7",
43
43
  "yaml": "^2.2.1"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@antfu/utils": "^0.7.2",
47
47
  "@types/debug": "^4.1.7",
48
- "@types/lodash": "^4.14.191",
49
- "@types/node": "^18.11.18",
50
- "vite": "^4.0.3"
48
+ "@types/node": "^18.15.11",
49
+ "vite": "^4.2.1"
51
50
  },
52
51
  "scripts": {
53
52
  "build": "unbuild",