@uni-helper/vite-plugin-uni-pages 0.1.7 → 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 +76 -26
- package/dist/index.d.ts +40 -20
- package/dist/index.mjs +75 -26
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -14,6 +14,7 @@ const Debug = require('debug');
|
|
|
14
14
|
const lodashUnified = require('lodash-unified');
|
|
15
15
|
const fg = require('fast-glob');
|
|
16
16
|
const fs = require('fs');
|
|
17
|
+
const fs$1 = require('node:fs');
|
|
17
18
|
const JSON5 = require('json5');
|
|
18
19
|
const yaml = require('yaml');
|
|
19
20
|
const compilerSfc = require('@vue/compiler-sfc');
|
|
@@ -27,6 +28,7 @@ const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
|
27
28
|
const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
|
|
28
29
|
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
29
30
|
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
31
|
+
const fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
|
|
30
32
|
const JSON5__default = /*#__PURE__*/_interopDefaultCompat(JSON5);
|
|
31
33
|
|
|
32
34
|
function slash(str) {
|
|
@@ -53,6 +55,7 @@ const debug = {
|
|
|
53
55
|
routeBlock: Debug__default("vite-plugin-uni-pages:routeBlock"),
|
|
54
56
|
options: Debug__default("vite-plugin-uni-pages:options"),
|
|
55
57
|
pages: Debug__default("vite-plugin-uni-pages:pages"),
|
|
58
|
+
subPages: Debug__default("vite-plugin-uni-pages:subPages"),
|
|
56
59
|
error: Debug__default("vite-plugin-uni-pages:error")
|
|
57
60
|
};
|
|
58
61
|
function extsToGlob(extensions) {
|
|
@@ -79,22 +82,24 @@ function mergePageMetaDataArray(pageMetaData) {
|
|
|
79
82
|
}
|
|
80
83
|
return result;
|
|
81
84
|
}
|
|
82
|
-
|
|
85
|
+
async function getPagesConfigSourcePaths() {
|
|
83
86
|
return await fg__default("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
|
|
84
87
|
deep: 0,
|
|
85
88
|
onlyFiles: true,
|
|
86
89
|
absolute: true
|
|
87
90
|
});
|
|
88
|
-
}
|
|
91
|
+
}
|
|
89
92
|
|
|
90
93
|
function resolveOptions(userOptions, viteRoot) {
|
|
91
94
|
const {
|
|
92
95
|
mergePages = true,
|
|
93
96
|
dir = "src/pages",
|
|
97
|
+
subPackages = [],
|
|
94
98
|
outDir = "src",
|
|
95
99
|
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
96
100
|
routeBlockLang = "json5",
|
|
97
101
|
minify = false,
|
|
102
|
+
debug = false,
|
|
98
103
|
onBeforeLoadUserConfig = () => {
|
|
99
104
|
},
|
|
100
105
|
onAfterLoadUserConfig = () => {
|
|
@@ -114,14 +119,17 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
114
119
|
} = userOptions;
|
|
115
120
|
const root = viteRoot || slash(process.cwd());
|
|
116
121
|
const resolvedDirs = resolvePageDirs(dir, root, exclude);
|
|
122
|
+
const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
|
|
117
123
|
const resolvedOptions = {
|
|
118
124
|
mergePages,
|
|
119
125
|
dirs: resolvedDirs,
|
|
126
|
+
subPackages: resolvedSubDirs,
|
|
120
127
|
outDir,
|
|
121
128
|
exclude,
|
|
122
129
|
routeBlockLang,
|
|
123
130
|
root,
|
|
124
131
|
minify,
|
|
132
|
+
debug,
|
|
125
133
|
onBeforeLoadUserConfig,
|
|
126
134
|
onAfterLoadUserConfig,
|
|
127
135
|
onBeforeScanPages,
|
|
@@ -186,7 +194,7 @@ function parseCustomBlock(block, filePath, options) {
|
|
|
186
194
|
};
|
|
187
195
|
let content;
|
|
188
196
|
debug.routeBlock(`use ${lang} parser`);
|
|
189
|
-
if (lang === "json5") {
|
|
197
|
+
if (lang === "json5" || lang === "jsonc") {
|
|
190
198
|
try {
|
|
191
199
|
content = JSON5__default.parse(block.content);
|
|
192
200
|
} catch (err) {
|
|
@@ -222,7 +230,7 @@ ${err.message}`
|
|
|
222
230
|
}
|
|
223
231
|
}
|
|
224
232
|
async function getRouteBlock(path, options) {
|
|
225
|
-
const content = fs__default.readFileSync(path, "utf8");
|
|
233
|
+
const content = fs__default$1.readFileSync(path, "utf8");
|
|
226
234
|
const parsedSFC = await parseSFC(content);
|
|
227
235
|
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
|
|
228
236
|
if (!blockStr)
|
|
@@ -234,12 +242,20 @@ async function getRouteBlock(path, options) {
|
|
|
234
242
|
class PageContext {
|
|
235
243
|
constructor(userOptions, viteRoot = process.cwd()) {
|
|
236
244
|
this.pagesPath = [];
|
|
245
|
+
this.subPagesPath = {};
|
|
237
246
|
this.pageMetaData = [];
|
|
247
|
+
this.subPageMetaData = [];
|
|
238
248
|
this.resolvedPagesJSONPath = "";
|
|
239
249
|
this.rawOptions = userOptions;
|
|
240
250
|
this.root = slash(viteRoot);
|
|
241
251
|
debug.options("root", this.root);
|
|
242
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
|
+
}
|
|
243
259
|
this.resolvedPagesJSONPath = path__default.join(this.root, this.options.outDir, OUTPUT_NAME);
|
|
244
260
|
debug.options(this.options);
|
|
245
261
|
}
|
|
@@ -257,19 +273,20 @@ class PageContext {
|
|
|
257
273
|
}
|
|
258
274
|
async scanPages() {
|
|
259
275
|
const pageDirFiles = this.options.dirs.map((dir) => {
|
|
260
|
-
|
|
261
|
-
const basePath = slash(path__default.join(this.options.root, this.options.outDir));
|
|
262
|
-
const files = getPageFiles(pagesDirPath, this.options);
|
|
263
|
-
debug.pages(dir, files);
|
|
264
|
-
const pagePaths = files.map((file) => slash(file)).map((file) => ({
|
|
265
|
-
relativePath: path__default.relative(basePath, slash(path__default.resolve(pagesDirPath, file))),
|
|
266
|
-
absolutePath: slash(path__default.resolve(pagesDirPath, file))
|
|
267
|
-
}));
|
|
268
|
-
return { dir, files: pagePaths };
|
|
276
|
+
return { dir, files: getPagePaths(dir, this.options) };
|
|
269
277
|
});
|
|
270
278
|
this.pagesPath = pageDirFiles.map((page) => page.files).flat();
|
|
271
279
|
debug.pages(this.pagesPath);
|
|
272
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
|
+
}
|
|
273
290
|
setupViteServer(server) {
|
|
274
291
|
if (this._server === server)
|
|
275
292
|
return;
|
|
@@ -324,21 +341,36 @@ class PageContext {
|
|
|
324
341
|
Object.assign(pageMetaDatum, routeBlock.content);
|
|
325
342
|
return pageMetaDatum;
|
|
326
343
|
}
|
|
327
|
-
async
|
|
344
|
+
async parsePages(pages, overrides) {
|
|
328
345
|
const generatedPageMetaData = await Promise.all(
|
|
329
|
-
|
|
346
|
+
pages.map(async (page) => await this.parsePage(page))
|
|
330
347
|
);
|
|
331
|
-
const customPageMetaData =
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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)) }));
|
|
339
366
|
}
|
|
340
|
-
|
|
341
|
-
|
|
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);
|
|
342
374
|
}
|
|
343
375
|
async updatePagesJSON() {
|
|
344
376
|
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
@@ -348,13 +380,20 @@ class PageContext {
|
|
|
348
380
|
if (this.options.mergePages) {
|
|
349
381
|
this.options.onBeforeScanPages(this);
|
|
350
382
|
await this.scanPages();
|
|
383
|
+
await this.scanSubPages();
|
|
351
384
|
this.options.onAfterScanPages(this);
|
|
352
385
|
}
|
|
353
386
|
this.options.onBeforeMergePageMetaData(this);
|
|
354
387
|
await this.mergePageMetaData();
|
|
388
|
+
await this.mergeSubPageMetaData();
|
|
355
389
|
this.options.onAfterMergePageMetaData(this);
|
|
356
390
|
this.options.onBeforeWriteFile(this);
|
|
357
|
-
const
|
|
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);
|
|
358
397
|
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
359
398
|
this.options.onAfterWriteFile(this);
|
|
360
399
|
}
|
|
@@ -365,6 +404,17 @@ class PageContext {
|
|
|
365
404
|
return JSON.stringify(this.pageMetaData, null, 2);
|
|
366
405
|
}
|
|
367
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
|
+
}
|
|
368
418
|
|
|
369
419
|
function defineUniPages(config) {
|
|
370
420
|
return config;
|
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
|
@@ -10,6 +10,7 @@ import Debug from 'debug';
|
|
|
10
10
|
import { groupBy } from 'lodash-unified';
|
|
11
11
|
import fg from 'fast-glob';
|
|
12
12
|
import fs from 'fs';
|
|
13
|
+
import fs$1 from 'node:fs';
|
|
13
14
|
import JSON5 from 'json5';
|
|
14
15
|
import { parse as parse$1 } from 'yaml';
|
|
15
16
|
import { parse } from '@vue/compiler-sfc';
|
|
@@ -38,6 +39,7 @@ const debug = {
|
|
|
38
39
|
routeBlock: Debug("vite-plugin-uni-pages:routeBlock"),
|
|
39
40
|
options: Debug("vite-plugin-uni-pages:options"),
|
|
40
41
|
pages: Debug("vite-plugin-uni-pages:pages"),
|
|
42
|
+
subPages: Debug("vite-plugin-uni-pages:subPages"),
|
|
41
43
|
error: Debug("vite-plugin-uni-pages:error")
|
|
42
44
|
};
|
|
43
45
|
function extsToGlob(extensions) {
|
|
@@ -64,22 +66,24 @@ function mergePageMetaDataArray(pageMetaData) {
|
|
|
64
66
|
}
|
|
65
67
|
return result;
|
|
66
68
|
}
|
|
67
|
-
|
|
69
|
+
async function getPagesConfigSourcePaths() {
|
|
68
70
|
return await fg("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
|
|
69
71
|
deep: 0,
|
|
70
72
|
onlyFiles: true,
|
|
71
73
|
absolute: true
|
|
72
74
|
});
|
|
73
|
-
}
|
|
75
|
+
}
|
|
74
76
|
|
|
75
77
|
function resolveOptions(userOptions, viteRoot) {
|
|
76
78
|
const {
|
|
77
79
|
mergePages = true,
|
|
78
80
|
dir = "src/pages",
|
|
81
|
+
subPackages = [],
|
|
79
82
|
outDir = "src",
|
|
80
83
|
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
81
84
|
routeBlockLang = "json5",
|
|
82
85
|
minify = false,
|
|
86
|
+
debug = false,
|
|
83
87
|
onBeforeLoadUserConfig = () => {
|
|
84
88
|
},
|
|
85
89
|
onAfterLoadUserConfig = () => {
|
|
@@ -99,14 +103,17 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
99
103
|
} = userOptions;
|
|
100
104
|
const root = viteRoot || slash(process.cwd());
|
|
101
105
|
const resolvedDirs = resolvePageDirs(dir, root, exclude);
|
|
106
|
+
const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
|
|
102
107
|
const resolvedOptions = {
|
|
103
108
|
mergePages,
|
|
104
109
|
dirs: resolvedDirs,
|
|
110
|
+
subPackages: resolvedSubDirs,
|
|
105
111
|
outDir,
|
|
106
112
|
exclude,
|
|
107
113
|
routeBlockLang,
|
|
108
114
|
root,
|
|
109
115
|
minify,
|
|
116
|
+
debug,
|
|
110
117
|
onBeforeLoadUserConfig,
|
|
111
118
|
onAfterLoadUserConfig,
|
|
112
119
|
onBeforeScanPages,
|
|
@@ -171,7 +178,7 @@ function parseCustomBlock(block, filePath, options) {
|
|
|
171
178
|
};
|
|
172
179
|
let content;
|
|
173
180
|
debug.routeBlock(`use ${lang} parser`);
|
|
174
|
-
if (lang === "json5") {
|
|
181
|
+
if (lang === "json5" || lang === "jsonc") {
|
|
175
182
|
try {
|
|
176
183
|
content = JSON5.parse(block.content);
|
|
177
184
|
} catch (err) {
|
|
@@ -207,7 +214,7 @@ ${err.message}`
|
|
|
207
214
|
}
|
|
208
215
|
}
|
|
209
216
|
async function getRouteBlock(path, options) {
|
|
210
|
-
const content = fs.readFileSync(path, "utf8");
|
|
217
|
+
const content = fs$1.readFileSync(path, "utf8");
|
|
211
218
|
const parsedSFC = await parseSFC(content);
|
|
212
219
|
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
|
|
213
220
|
if (!blockStr)
|
|
@@ -219,12 +226,20 @@ async function getRouteBlock(path, options) {
|
|
|
219
226
|
class PageContext {
|
|
220
227
|
constructor(userOptions, viteRoot = process.cwd()) {
|
|
221
228
|
this.pagesPath = [];
|
|
229
|
+
this.subPagesPath = {};
|
|
222
230
|
this.pageMetaData = [];
|
|
231
|
+
this.subPageMetaData = [];
|
|
223
232
|
this.resolvedPagesJSONPath = "";
|
|
224
233
|
this.rawOptions = userOptions;
|
|
225
234
|
this.root = slash(viteRoot);
|
|
226
235
|
debug.options("root", this.root);
|
|
227
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
|
+
}
|
|
228
243
|
this.resolvedPagesJSONPath = path.join(this.root, this.options.outDir, OUTPUT_NAME);
|
|
229
244
|
debug.options(this.options);
|
|
230
245
|
}
|
|
@@ -242,19 +257,20 @@ class PageContext {
|
|
|
242
257
|
}
|
|
243
258
|
async scanPages() {
|
|
244
259
|
const pageDirFiles = this.options.dirs.map((dir) => {
|
|
245
|
-
|
|
246
|
-
const basePath = slash(path.join(this.options.root, this.options.outDir));
|
|
247
|
-
const files = getPageFiles(pagesDirPath, this.options);
|
|
248
|
-
debug.pages(dir, files);
|
|
249
|
-
const pagePaths = files.map((file) => slash(file)).map((file) => ({
|
|
250
|
-
relativePath: path.relative(basePath, slash(path.resolve(pagesDirPath, file))),
|
|
251
|
-
absolutePath: slash(path.resolve(pagesDirPath, file))
|
|
252
|
-
}));
|
|
253
|
-
return { dir, files: pagePaths };
|
|
260
|
+
return { dir, files: getPagePaths(dir, this.options) };
|
|
254
261
|
});
|
|
255
262
|
this.pagesPath = pageDirFiles.map((page) => page.files).flat();
|
|
256
263
|
debug.pages(this.pagesPath);
|
|
257
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
|
+
}
|
|
258
274
|
setupViteServer(server) {
|
|
259
275
|
if (this._server === server)
|
|
260
276
|
return;
|
|
@@ -309,21 +325,36 @@ class PageContext {
|
|
|
309
325
|
Object.assign(pageMetaDatum, routeBlock.content);
|
|
310
326
|
return pageMetaDatum;
|
|
311
327
|
}
|
|
312
|
-
async
|
|
328
|
+
async parsePages(pages, overrides) {
|
|
313
329
|
const generatedPageMetaData = await Promise.all(
|
|
314
|
-
|
|
330
|
+
pages.map(async (page) => await this.parsePage(page))
|
|
315
331
|
);
|
|
316
|
-
const customPageMetaData =
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
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)) }));
|
|
324
350
|
}
|
|
325
|
-
|
|
326
|
-
|
|
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);
|
|
327
358
|
}
|
|
328
359
|
async updatePagesJSON() {
|
|
329
360
|
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
@@ -333,13 +364,20 @@ class PageContext {
|
|
|
333
364
|
if (this.options.mergePages) {
|
|
334
365
|
this.options.onBeforeScanPages(this);
|
|
335
366
|
await this.scanPages();
|
|
367
|
+
await this.scanSubPages();
|
|
336
368
|
this.options.onAfterScanPages(this);
|
|
337
369
|
}
|
|
338
370
|
this.options.onBeforeMergePageMetaData(this);
|
|
339
371
|
await this.mergePageMetaData();
|
|
372
|
+
await this.mergeSubPageMetaData();
|
|
340
373
|
this.options.onAfterMergePageMetaData(this);
|
|
341
374
|
this.options.onBeforeWriteFile(this);
|
|
342
|
-
const
|
|
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);
|
|
343
381
|
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
344
382
|
this.options.onAfterWriteFile(this);
|
|
345
383
|
}
|
|
@@ -350,6 +388,17 @@ class PageContext {
|
|
|
350
388
|
return JSON.stringify(this.pageMetaData, null, 2);
|
|
351
389
|
}
|
|
352
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
|
+
}
|
|
353
402
|
|
|
354
403
|
function defineUniPages(config) {
|
|
355
404
|
return config;
|