@uni-helper/vite-plugin-uni-pages 0.3.11 → 0.3.13
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 +106 -52
- package/dist/index.d.cts +27 -8
- package/dist/index.d.mts +27 -8
- package/dist/index.d.ts +27 -8
- package/dist/index.mjs +104 -50
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -404,68 +404,84 @@ var __publicField$1 = (obj, key, value) => {
|
|
|
404
404
|
return value;
|
|
405
405
|
};
|
|
406
406
|
class Page {
|
|
407
|
-
constructor(ctx, path) {
|
|
407
|
+
constructor(ctx, path$1) {
|
|
408
408
|
__publicField$1(this, "ctx");
|
|
409
409
|
__publicField$1(this, "path");
|
|
410
|
-
__publicField$1(this, "
|
|
411
|
-
__publicField$1(this, "
|
|
410
|
+
__publicField$1(this, "uri");
|
|
411
|
+
__publicField$1(this, "changed", true);
|
|
412
|
+
__publicField$1(this, "raw", "");
|
|
413
|
+
__publicField$1(this, "meta");
|
|
412
414
|
this.ctx = ctx;
|
|
413
|
-
this.path = path;
|
|
415
|
+
this.path = path$1;
|
|
416
|
+
this.uri = vite.normalizePath(path$1.relativePath.replace(path.extname(path$1.relativePath), ""));
|
|
414
417
|
}
|
|
415
418
|
async getPageMeta(forceUpdate = false) {
|
|
416
|
-
if (forceUpdate || !this.
|
|
417
|
-
await this.
|
|
419
|
+
if (forceUpdate || !this.meta) {
|
|
420
|
+
await this.read();
|
|
418
421
|
}
|
|
419
|
-
|
|
422
|
+
const { path, tabBar: _, ...others } = this.meta;
|
|
423
|
+
return {
|
|
424
|
+
path: path ?? this.uri,
|
|
425
|
+
...others
|
|
426
|
+
};
|
|
420
427
|
}
|
|
421
|
-
async
|
|
422
|
-
|
|
423
|
-
|
|
428
|
+
async getTabBar(forceUpdate = false) {
|
|
429
|
+
if (forceUpdate || !this.meta) {
|
|
430
|
+
await this.read();
|
|
431
|
+
}
|
|
432
|
+
const { tabBar } = this.meta;
|
|
433
|
+
if (tabBar === void 0) {
|
|
434
|
+
return void 0;
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
...tabBar,
|
|
438
|
+
pagePath: tabBar.pagePath || this.uri,
|
|
439
|
+
index: tabBar.index || 0
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
hasChanged() {
|
|
443
|
+
return this.changed;
|
|
424
444
|
}
|
|
425
|
-
async
|
|
445
|
+
async read() {
|
|
446
|
+
let meta;
|
|
426
447
|
try {
|
|
427
|
-
|
|
428
|
-
const { path: path$1, ...others } = await this.readPageMetaFromFile();
|
|
429
|
-
this.options = {
|
|
430
|
-
path: path$1 ?? vite.normalizePath(relativePath.replace(path.extname(relativePath), "")),
|
|
431
|
-
...others
|
|
432
|
-
};
|
|
433
|
-
const raw = this.options ? JSON.stringify(this.options) : "";
|
|
434
|
-
const hasChanged = this.rawOptions !== raw;
|
|
435
|
-
this.rawOptions = raw;
|
|
436
|
-
return {
|
|
437
|
-
options: this.options,
|
|
438
|
-
hasChanged
|
|
439
|
-
};
|
|
448
|
+
meta = await this.readPageMetaFromFile();
|
|
440
449
|
} catch (err) {
|
|
441
|
-
|
|
442
|
-
|
|
450
|
+
debug.error(err);
|
|
451
|
+
return;
|
|
443
452
|
}
|
|
453
|
+
let raw = "";
|
|
454
|
+
try {
|
|
455
|
+
raw = JSON.stringify(meta);
|
|
456
|
+
} catch {
|
|
457
|
+
}
|
|
458
|
+
this.changed = this.raw !== raw;
|
|
459
|
+
this.meta = meta;
|
|
460
|
+
this.raw = raw;
|
|
444
461
|
}
|
|
445
462
|
async readPageMetaFromFile() {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
463
|
+
try {
|
|
464
|
+
const content = fs.readFileSync(this.path.absolutePath, "utf-8");
|
|
465
|
+
const sfc = parseSFC(content, { filename: this.path.absolutePath });
|
|
466
|
+
const meta = await tryPageMetaFromMacro(sfc);
|
|
467
|
+
if (meta) {
|
|
468
|
+
return meta;
|
|
469
|
+
}
|
|
470
|
+
return tryPageMetaFromCustomBlock(sfc, this.ctx.options.routeBlockLang);
|
|
471
|
+
} catch (err) {
|
|
472
|
+
throw new Error(`Read page meta fail in ${this.path.relativePath}
|
|
473
|
+
${err.message}`);
|
|
451
474
|
}
|
|
452
|
-
return tryPageMetaFromCustomBlock(sfc, this.ctx.options.routeBlockLang);
|
|
453
475
|
}
|
|
454
476
|
}
|
|
455
477
|
function parseSFC(code, options) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
});
|
|
464
|
-
} catch (error) {
|
|
465
|
-
throw new Error(`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required.
|
|
466
|
-
Original error:
|
|
467
|
-
${error}`);
|
|
468
|
-
}
|
|
478
|
+
return compilerSfc.parse(code, {
|
|
479
|
+
pad: "space",
|
|
480
|
+
...options
|
|
481
|
+
}).descriptor || compilerSfc.parse({
|
|
482
|
+
source: code,
|
|
483
|
+
...options
|
|
484
|
+
});
|
|
469
485
|
}
|
|
470
486
|
async function tryPageMetaFromMacro(sfc) {
|
|
471
487
|
const sfcScript = sfc.scriptSetup || sfc.script;
|
|
@@ -477,7 +493,7 @@ async function tryPageMetaFromMacro(sfc) {
|
|
|
477
493
|
});
|
|
478
494
|
const macro = findMacro(ast.body, sfc.filename);
|
|
479
495
|
if (macro) {
|
|
480
|
-
const imports = findImports(ast.body).map((imp) => babelGenerate(imp).code);
|
|
496
|
+
const imports = findImports(ast.body).filter((imp) => !!imp.specifiers.length).map((imp) => babelGenerate(imp).code);
|
|
481
497
|
const [macroOption] = macro.arguments;
|
|
482
498
|
const code = babelGenerate(macroOption).code;
|
|
483
499
|
const result = await execScript({
|
|
@@ -499,7 +515,10 @@ async function tryPageMetaFromCustomBlock(sfc, routeBlockLang) {
|
|
|
499
515
|
type: routeBlock?.attr.type ?? "page"
|
|
500
516
|
};
|
|
501
517
|
if (routeBlock) {
|
|
502
|
-
|
|
518
|
+
try {
|
|
519
|
+
commentJson.assign(pageMeta, routeBlock.content);
|
|
520
|
+
} catch {
|
|
521
|
+
}
|
|
503
522
|
}
|
|
504
523
|
return pageMeta;
|
|
505
524
|
}
|
|
@@ -524,7 +543,13 @@ function findMacro(stmts, filename) {
|
|
|
524
543
|
return macro;
|
|
525
544
|
}
|
|
526
545
|
function findImports(stmts) {
|
|
527
|
-
|
|
546
|
+
const imports = [];
|
|
547
|
+
for (const stmt of stmts) {
|
|
548
|
+
if (t__namespace.isImportDeclaration(stmt)) {
|
|
549
|
+
imports.push(stmt);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return imports;
|
|
528
553
|
}
|
|
529
554
|
|
|
530
555
|
var __defProp = Object.defineProperty;
|
|
@@ -735,6 +760,31 @@ class PageContext {
|
|
|
735
760
|
this.subPageMetaData = subPageMetaData;
|
|
736
761
|
debug.subPages(this.subPageMetaData);
|
|
737
762
|
}
|
|
763
|
+
async getTabBarMerged() {
|
|
764
|
+
const tabBar = {
|
|
765
|
+
...this.pagesGlobConfig?.tabBar,
|
|
766
|
+
list: this.pagesGlobConfig?.tabBar?.list || []
|
|
767
|
+
};
|
|
768
|
+
const pagePaths = /* @__PURE__ */ new Map();
|
|
769
|
+
for (const item of tabBar.list) {
|
|
770
|
+
pagePaths.set(item.pagePath, true);
|
|
771
|
+
}
|
|
772
|
+
const tabBarItems = [];
|
|
773
|
+
for (const [_, page] of this.pages) {
|
|
774
|
+
const tabbar = await page.getTabBar();
|
|
775
|
+
if (tabbar) {
|
|
776
|
+
tabBarItems.push(tabbar);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
tabBarItems.sort((a, b) => a.index - b.index);
|
|
780
|
+
for (const item of tabBarItems) {
|
|
781
|
+
if (!pagePaths.has(item.pagePath)) {
|
|
782
|
+
const { index: _, ...tabbar } = item;
|
|
783
|
+
tabBar.list.push(tabbar);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return tabBar;
|
|
787
|
+
}
|
|
738
788
|
async updatePagesJSON(filepath) {
|
|
739
789
|
if (filepath) {
|
|
740
790
|
let page = this.pages.get(filepath);
|
|
@@ -748,9 +798,12 @@ class PageContext {
|
|
|
748
798
|
}
|
|
749
799
|
page = subPage;
|
|
750
800
|
}
|
|
751
|
-
if (page
|
|
752
|
-
|
|
753
|
-
|
|
801
|
+
if (page) {
|
|
802
|
+
await page.read();
|
|
803
|
+
if (!page.hasChanged()) {
|
|
804
|
+
debug.cache(`The route block on page ${filepath} did not send any changes, skipping`);
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
754
807
|
}
|
|
755
808
|
}
|
|
756
809
|
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
@@ -778,7 +831,8 @@ class PageContext {
|
|
|
778
831
|
const data = {
|
|
779
832
|
...this.pagesGlobConfig,
|
|
780
833
|
pages: this.pageMetaData,
|
|
781
|
-
subPackages: this.subPageMetaData
|
|
834
|
+
subPackages: this.subPageMetaData,
|
|
835
|
+
tabBar: await this.getTabBarMerged()
|
|
782
836
|
};
|
|
783
837
|
const pagesJson = commentJson.stringify(
|
|
784
838
|
data,
|
package/dist/index.d.cts
CHANGED
|
@@ -2533,6 +2533,7 @@ interface SubPackage {
|
|
|
2533
2533
|
[pluginName: string]: {
|
|
2534
2534
|
version: string;
|
|
2535
2535
|
provider: string;
|
|
2536
|
+
export?: string;
|
|
2536
2537
|
[key: string]: any;
|
|
2537
2538
|
};
|
|
2538
2539
|
};
|
|
@@ -2926,15 +2927,17 @@ declare function defineUniPages(config: UserPagesConfig): UserPagesConfig;
|
|
|
2926
2927
|
declare class Page {
|
|
2927
2928
|
ctx: PageContext;
|
|
2928
2929
|
path: PagePath;
|
|
2929
|
-
|
|
2930
|
-
|
|
2930
|
+
uri: string;
|
|
2931
|
+
changed: boolean;
|
|
2932
|
+
private raw;
|
|
2933
|
+
private meta;
|
|
2931
2934
|
constructor(ctx: PageContext, path: PagePath);
|
|
2932
2935
|
getPageMeta(forceUpdate?: boolean): Promise<PageMetaDatum>;
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2936
|
+
getTabBar(forceUpdate?: boolean): Promise<TabBarItem & {
|
|
2937
|
+
index: number;
|
|
2938
|
+
} | undefined>;
|
|
2939
|
+
hasChanged(): boolean;
|
|
2940
|
+
read(): Promise<void>;
|
|
2938
2941
|
private readPageMetaFromFile;
|
|
2939
2942
|
}
|
|
2940
2943
|
declare function parseSFC(code: string, options?: SFCParseOptions): SFCDescriptor;
|
|
@@ -2984,6 +2987,7 @@ declare class PageContext {
|
|
|
2984
2987
|
setHomePage(result: PageMetaDatum[]): PageMetaDatum[];
|
|
2985
2988
|
mergePageMetaData(): Promise<void>;
|
|
2986
2989
|
mergeSubPageMetaData(): Promise<void>;
|
|
2990
|
+
private getTabBarMerged;
|
|
2987
2991
|
updatePagesJSON(filepath?: string): Promise<boolean>;
|
|
2988
2992
|
virtualModule(): string;
|
|
2989
2993
|
resolveRoutes(): string;
|
|
@@ -3144,12 +3148,27 @@ interface SubPageMetaDatum {
|
|
|
3144
3148
|
root: string;
|
|
3145
3149
|
pages: PageMetaDatum[];
|
|
3146
3150
|
}
|
|
3151
|
+
interface UserTabBarItem extends Partial<TabBarItem> {
|
|
3152
|
+
/**
|
|
3153
|
+
* 配置页面路径
|
|
3154
|
+
* @deprecated 可选,将会根据文件路径自动生成
|
|
3155
|
+
*/
|
|
3156
|
+
pagePath?: string;
|
|
3157
|
+
/**
|
|
3158
|
+
* 排序,数字越小越靠前
|
|
3159
|
+
*/
|
|
3160
|
+
index?: number;
|
|
3161
|
+
}
|
|
3147
3162
|
interface UserPageMeta extends Partial<PageMetaDatum> {
|
|
3148
3163
|
/**
|
|
3149
3164
|
* 配置页面路径
|
|
3150
3165
|
* @deprecated 可选,将会根据文件路径自动生成
|
|
3151
3166
|
*/
|
|
3152
3167
|
path?: string;
|
|
3168
|
+
/**
|
|
3169
|
+
* 底部 tabBar 的子项 [tabBar](https://uniapp.dcloud.net.cn/collocation/pages#tabBar)
|
|
3170
|
+
*/
|
|
3171
|
+
tabBar?: UserTabBarItem;
|
|
3153
3172
|
}
|
|
3154
3173
|
type MaybePromise<T> = T | Promise<T>;
|
|
3155
3174
|
type MaybeCallable<T> = T | (() => T);
|
|
@@ -3183,4 +3202,4 @@ declare function resolvePageDirs(dir: string, root: string, exclude: string[]):
|
|
|
3183
3202
|
declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
|
|
3184
3203
|
|
|
3185
3204
|
export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, Page, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, babelGenerate, checkPagesJsonFile, debug, VitePluginUniPages as default, definePage, defineUniPages, execScript, extsToGlob, findImports, findMacro, getPageFiles, getRouteBlock, getRouteSfcBlock, invalidatePagesModule, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, readFileSync, resolveOptions, resolvePageDirs, tryPageMetaFromCustomBlock, tryPageMetaFromMacro, writeFileSync };
|
|
3186
|
-
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, debugType };
|
|
3205
|
+
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 };
|
package/dist/index.d.mts
CHANGED
|
@@ -2533,6 +2533,7 @@ interface SubPackage {
|
|
|
2533
2533
|
[pluginName: string]: {
|
|
2534
2534
|
version: string;
|
|
2535
2535
|
provider: string;
|
|
2536
|
+
export?: string;
|
|
2536
2537
|
[key: string]: any;
|
|
2537
2538
|
};
|
|
2538
2539
|
};
|
|
@@ -2926,15 +2927,17 @@ declare function defineUniPages(config: UserPagesConfig): UserPagesConfig;
|
|
|
2926
2927
|
declare class Page {
|
|
2927
2928
|
ctx: PageContext;
|
|
2928
2929
|
path: PagePath;
|
|
2929
|
-
|
|
2930
|
-
|
|
2930
|
+
uri: string;
|
|
2931
|
+
changed: boolean;
|
|
2932
|
+
private raw;
|
|
2933
|
+
private meta;
|
|
2931
2934
|
constructor(ctx: PageContext, path: PagePath);
|
|
2932
2935
|
getPageMeta(forceUpdate?: boolean): Promise<PageMetaDatum>;
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2936
|
+
getTabBar(forceUpdate?: boolean): Promise<TabBarItem & {
|
|
2937
|
+
index: number;
|
|
2938
|
+
} | undefined>;
|
|
2939
|
+
hasChanged(): boolean;
|
|
2940
|
+
read(): Promise<void>;
|
|
2938
2941
|
private readPageMetaFromFile;
|
|
2939
2942
|
}
|
|
2940
2943
|
declare function parseSFC(code: string, options?: SFCParseOptions): SFCDescriptor;
|
|
@@ -2984,6 +2987,7 @@ declare class PageContext {
|
|
|
2984
2987
|
setHomePage(result: PageMetaDatum[]): PageMetaDatum[];
|
|
2985
2988
|
mergePageMetaData(): Promise<void>;
|
|
2986
2989
|
mergeSubPageMetaData(): Promise<void>;
|
|
2990
|
+
private getTabBarMerged;
|
|
2987
2991
|
updatePagesJSON(filepath?: string): Promise<boolean>;
|
|
2988
2992
|
virtualModule(): string;
|
|
2989
2993
|
resolveRoutes(): string;
|
|
@@ -3144,12 +3148,27 @@ interface SubPageMetaDatum {
|
|
|
3144
3148
|
root: string;
|
|
3145
3149
|
pages: PageMetaDatum[];
|
|
3146
3150
|
}
|
|
3151
|
+
interface UserTabBarItem extends Partial<TabBarItem> {
|
|
3152
|
+
/**
|
|
3153
|
+
* 配置页面路径
|
|
3154
|
+
* @deprecated 可选,将会根据文件路径自动生成
|
|
3155
|
+
*/
|
|
3156
|
+
pagePath?: string;
|
|
3157
|
+
/**
|
|
3158
|
+
* 排序,数字越小越靠前
|
|
3159
|
+
*/
|
|
3160
|
+
index?: number;
|
|
3161
|
+
}
|
|
3147
3162
|
interface UserPageMeta extends Partial<PageMetaDatum> {
|
|
3148
3163
|
/**
|
|
3149
3164
|
* 配置页面路径
|
|
3150
3165
|
* @deprecated 可选,将会根据文件路径自动生成
|
|
3151
3166
|
*/
|
|
3152
3167
|
path?: string;
|
|
3168
|
+
/**
|
|
3169
|
+
* 底部 tabBar 的子项 [tabBar](https://uniapp.dcloud.net.cn/collocation/pages#tabBar)
|
|
3170
|
+
*/
|
|
3171
|
+
tabBar?: UserTabBarItem;
|
|
3153
3172
|
}
|
|
3154
3173
|
type MaybePromise<T> = T | Promise<T>;
|
|
3155
3174
|
type MaybeCallable<T> = T | (() => T);
|
|
@@ -3183,4 +3202,4 @@ declare function resolvePageDirs(dir: string, root: string, exclude: string[]):
|
|
|
3183
3202
|
declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
|
|
3184
3203
|
|
|
3185
3204
|
export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, Page, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, babelGenerate, checkPagesJsonFile, debug, VitePluginUniPages as default, definePage, defineUniPages, execScript, extsToGlob, findImports, findMacro, getPageFiles, getRouteBlock, getRouteSfcBlock, invalidatePagesModule, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, readFileSync, resolveOptions, resolvePageDirs, tryPageMetaFromCustomBlock, tryPageMetaFromMacro, writeFileSync };
|
|
3186
|
-
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, debugType };
|
|
3205
|
+
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -2533,6 +2533,7 @@ interface SubPackage {
|
|
|
2533
2533
|
[pluginName: string]: {
|
|
2534
2534
|
version: string;
|
|
2535
2535
|
provider: string;
|
|
2536
|
+
export?: string;
|
|
2536
2537
|
[key: string]: any;
|
|
2537
2538
|
};
|
|
2538
2539
|
};
|
|
@@ -2926,15 +2927,17 @@ declare function defineUniPages(config: UserPagesConfig): UserPagesConfig;
|
|
|
2926
2927
|
declare class Page {
|
|
2927
2928
|
ctx: PageContext;
|
|
2928
2929
|
path: PagePath;
|
|
2929
|
-
|
|
2930
|
-
|
|
2930
|
+
uri: string;
|
|
2931
|
+
changed: boolean;
|
|
2932
|
+
private raw;
|
|
2933
|
+
private meta;
|
|
2931
2934
|
constructor(ctx: PageContext, path: PagePath);
|
|
2932
2935
|
getPageMeta(forceUpdate?: boolean): Promise<PageMetaDatum>;
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2936
|
+
getTabBar(forceUpdate?: boolean): Promise<TabBarItem & {
|
|
2937
|
+
index: number;
|
|
2938
|
+
} | undefined>;
|
|
2939
|
+
hasChanged(): boolean;
|
|
2940
|
+
read(): Promise<void>;
|
|
2938
2941
|
private readPageMetaFromFile;
|
|
2939
2942
|
}
|
|
2940
2943
|
declare function parseSFC(code: string, options?: SFCParseOptions): SFCDescriptor;
|
|
@@ -2984,6 +2987,7 @@ declare class PageContext {
|
|
|
2984
2987
|
setHomePage(result: PageMetaDatum[]): PageMetaDatum[];
|
|
2985
2988
|
mergePageMetaData(): Promise<void>;
|
|
2986
2989
|
mergeSubPageMetaData(): Promise<void>;
|
|
2990
|
+
private getTabBarMerged;
|
|
2987
2991
|
updatePagesJSON(filepath?: string): Promise<boolean>;
|
|
2988
2992
|
virtualModule(): string;
|
|
2989
2993
|
resolveRoutes(): string;
|
|
@@ -3144,12 +3148,27 @@ interface SubPageMetaDatum {
|
|
|
3144
3148
|
root: string;
|
|
3145
3149
|
pages: PageMetaDatum[];
|
|
3146
3150
|
}
|
|
3151
|
+
interface UserTabBarItem extends Partial<TabBarItem> {
|
|
3152
|
+
/**
|
|
3153
|
+
* 配置页面路径
|
|
3154
|
+
* @deprecated 可选,将会根据文件路径自动生成
|
|
3155
|
+
*/
|
|
3156
|
+
pagePath?: string;
|
|
3157
|
+
/**
|
|
3158
|
+
* 排序,数字越小越靠前
|
|
3159
|
+
*/
|
|
3160
|
+
index?: number;
|
|
3161
|
+
}
|
|
3147
3162
|
interface UserPageMeta extends Partial<PageMetaDatum> {
|
|
3148
3163
|
/**
|
|
3149
3164
|
* 配置页面路径
|
|
3150
3165
|
* @deprecated 可选,将会根据文件路径自动生成
|
|
3151
3166
|
*/
|
|
3152
3167
|
path?: string;
|
|
3168
|
+
/**
|
|
3169
|
+
* 底部 tabBar 的子项 [tabBar](https://uniapp.dcloud.net.cn/collocation/pages#tabBar)
|
|
3170
|
+
*/
|
|
3171
|
+
tabBar?: UserTabBarItem;
|
|
3153
3172
|
}
|
|
3154
3173
|
type MaybePromise<T> = T | Promise<T>;
|
|
3155
3174
|
type MaybeCallable<T> = T | (() => T);
|
|
@@ -3183,4 +3202,4 @@ declare function resolvePageDirs(dir: string, root: string, exclude: string[]):
|
|
|
3183
3202
|
declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
|
|
3184
3203
|
|
|
3185
3204
|
export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, Page, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, babelGenerate, checkPagesJsonFile, debug, VitePluginUniPages as default, definePage, defineUniPages, execScript, extsToGlob, findImports, findMacro, getPageFiles, getRouteBlock, getRouteSfcBlock, invalidatePagesModule, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, readFileSync, resolveOptions, resolvePageDirs, tryPageMetaFromCustomBlock, tryPageMetaFromMacro, writeFileSync };
|
|
3186
|
-
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, debugType };
|
|
3205
|
+
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 };
|
package/dist/index.mjs
CHANGED
|
@@ -373,65 +373,81 @@ class Page {
|
|
|
373
373
|
constructor(ctx, path) {
|
|
374
374
|
__publicField$1(this, "ctx");
|
|
375
375
|
__publicField$1(this, "path");
|
|
376
|
-
__publicField$1(this, "
|
|
377
|
-
__publicField$1(this, "
|
|
376
|
+
__publicField$1(this, "uri");
|
|
377
|
+
__publicField$1(this, "changed", true);
|
|
378
|
+
__publicField$1(this, "raw", "");
|
|
379
|
+
__publicField$1(this, "meta");
|
|
378
380
|
this.ctx = ctx;
|
|
379
381
|
this.path = path;
|
|
382
|
+
this.uri = normalizePath(path.relativePath.replace(extname(path.relativePath), ""));
|
|
380
383
|
}
|
|
381
384
|
async getPageMeta(forceUpdate = false) {
|
|
382
|
-
if (forceUpdate || !this.
|
|
383
|
-
await this.
|
|
385
|
+
if (forceUpdate || !this.meta) {
|
|
386
|
+
await this.read();
|
|
384
387
|
}
|
|
385
|
-
|
|
388
|
+
const { path, tabBar: _, ...others } = this.meta;
|
|
389
|
+
return {
|
|
390
|
+
path: path ?? this.uri,
|
|
391
|
+
...others
|
|
392
|
+
};
|
|
386
393
|
}
|
|
387
|
-
async
|
|
388
|
-
|
|
389
|
-
|
|
394
|
+
async getTabBar(forceUpdate = false) {
|
|
395
|
+
if (forceUpdate || !this.meta) {
|
|
396
|
+
await this.read();
|
|
397
|
+
}
|
|
398
|
+
const { tabBar } = this.meta;
|
|
399
|
+
if (tabBar === void 0) {
|
|
400
|
+
return void 0;
|
|
401
|
+
}
|
|
402
|
+
return {
|
|
403
|
+
...tabBar,
|
|
404
|
+
pagePath: tabBar.pagePath || this.uri,
|
|
405
|
+
index: tabBar.index || 0
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
hasChanged() {
|
|
409
|
+
return this.changed;
|
|
390
410
|
}
|
|
391
|
-
async
|
|
411
|
+
async read() {
|
|
412
|
+
let meta;
|
|
392
413
|
try {
|
|
393
|
-
|
|
394
|
-
const { path, ...others } = await this.readPageMetaFromFile();
|
|
395
|
-
this.options = {
|
|
396
|
-
path: path ?? normalizePath(relativePath.replace(extname(relativePath), "")),
|
|
397
|
-
...others
|
|
398
|
-
};
|
|
399
|
-
const raw = this.options ? JSON.stringify(this.options) : "";
|
|
400
|
-
const hasChanged = this.rawOptions !== raw;
|
|
401
|
-
this.rawOptions = raw;
|
|
402
|
-
return {
|
|
403
|
-
options: this.options,
|
|
404
|
-
hasChanged
|
|
405
|
-
};
|
|
414
|
+
meta = await this.readPageMetaFromFile();
|
|
406
415
|
} catch (err) {
|
|
407
|
-
|
|
408
|
-
|
|
416
|
+
debug.error(err);
|
|
417
|
+
return;
|
|
409
418
|
}
|
|
419
|
+
let raw = "";
|
|
420
|
+
try {
|
|
421
|
+
raw = JSON.stringify(meta);
|
|
422
|
+
} catch {
|
|
423
|
+
}
|
|
424
|
+
this.changed = this.raw !== raw;
|
|
425
|
+
this.meta = meta;
|
|
426
|
+
this.raw = raw;
|
|
410
427
|
}
|
|
411
428
|
async readPageMetaFromFile() {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
429
|
+
try {
|
|
430
|
+
const content = readFileSync$1(this.path.absolutePath, "utf-8");
|
|
431
|
+
const sfc = parseSFC(content, { filename: this.path.absolutePath });
|
|
432
|
+
const meta = await tryPageMetaFromMacro(sfc);
|
|
433
|
+
if (meta) {
|
|
434
|
+
return meta;
|
|
435
|
+
}
|
|
436
|
+
return tryPageMetaFromCustomBlock(sfc, this.ctx.options.routeBlockLang);
|
|
437
|
+
} catch (err) {
|
|
438
|
+
throw new Error(`Read page meta fail in ${this.path.relativePath}
|
|
439
|
+
${err.message}`);
|
|
417
440
|
}
|
|
418
|
-
return tryPageMetaFromCustomBlock(sfc, this.ctx.options.routeBlockLang);
|
|
419
441
|
}
|
|
420
442
|
}
|
|
421
443
|
function parseSFC(code, options) {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
});
|
|
430
|
-
} catch (error) {
|
|
431
|
-
throw new Error(`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required.
|
|
432
|
-
Original error:
|
|
433
|
-
${error}`);
|
|
434
|
-
}
|
|
444
|
+
return parse$2(code, {
|
|
445
|
+
pad: "space",
|
|
446
|
+
...options
|
|
447
|
+
}).descriptor || parse$2({
|
|
448
|
+
source: code,
|
|
449
|
+
...options
|
|
450
|
+
});
|
|
435
451
|
}
|
|
436
452
|
async function tryPageMetaFromMacro(sfc) {
|
|
437
453
|
const sfcScript = sfc.scriptSetup || sfc.script;
|
|
@@ -443,7 +459,7 @@ async function tryPageMetaFromMacro(sfc) {
|
|
|
443
459
|
});
|
|
444
460
|
const macro = findMacro(ast.body, sfc.filename);
|
|
445
461
|
if (macro) {
|
|
446
|
-
const imports = findImports(ast.body).map((imp) => babelGenerate(imp).code);
|
|
462
|
+
const imports = findImports(ast.body).filter((imp) => !!imp.specifiers.length).map((imp) => babelGenerate(imp).code);
|
|
447
463
|
const [macroOption] = macro.arguments;
|
|
448
464
|
const code = babelGenerate(macroOption).code;
|
|
449
465
|
const result = await execScript({
|
|
@@ -465,7 +481,10 @@ async function tryPageMetaFromCustomBlock(sfc, routeBlockLang) {
|
|
|
465
481
|
type: routeBlock?.attr.type ?? "page"
|
|
466
482
|
};
|
|
467
483
|
if (routeBlock) {
|
|
468
|
-
|
|
484
|
+
try {
|
|
485
|
+
assign(pageMeta, routeBlock.content);
|
|
486
|
+
} catch {
|
|
487
|
+
}
|
|
469
488
|
}
|
|
470
489
|
return pageMeta;
|
|
471
490
|
}
|
|
@@ -490,7 +509,13 @@ function findMacro(stmts, filename) {
|
|
|
490
509
|
return macro;
|
|
491
510
|
}
|
|
492
511
|
function findImports(stmts) {
|
|
493
|
-
|
|
512
|
+
const imports = [];
|
|
513
|
+
for (const stmt of stmts) {
|
|
514
|
+
if (t.isImportDeclaration(stmt)) {
|
|
515
|
+
imports.push(stmt);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return imports;
|
|
494
519
|
}
|
|
495
520
|
|
|
496
521
|
var __defProp = Object.defineProperty;
|
|
@@ -701,6 +726,31 @@ class PageContext {
|
|
|
701
726
|
this.subPageMetaData = subPageMetaData;
|
|
702
727
|
debug.subPages(this.subPageMetaData);
|
|
703
728
|
}
|
|
729
|
+
async getTabBarMerged() {
|
|
730
|
+
const tabBar = {
|
|
731
|
+
...this.pagesGlobConfig?.tabBar,
|
|
732
|
+
list: this.pagesGlobConfig?.tabBar?.list || []
|
|
733
|
+
};
|
|
734
|
+
const pagePaths = /* @__PURE__ */ new Map();
|
|
735
|
+
for (const item of tabBar.list) {
|
|
736
|
+
pagePaths.set(item.pagePath, true);
|
|
737
|
+
}
|
|
738
|
+
const tabBarItems = [];
|
|
739
|
+
for (const [_, page] of this.pages) {
|
|
740
|
+
const tabbar = await page.getTabBar();
|
|
741
|
+
if (tabbar) {
|
|
742
|
+
tabBarItems.push(tabbar);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
tabBarItems.sort((a, b) => a.index - b.index);
|
|
746
|
+
for (const item of tabBarItems) {
|
|
747
|
+
if (!pagePaths.has(item.pagePath)) {
|
|
748
|
+
const { index: _, ...tabbar } = item;
|
|
749
|
+
tabBar.list.push(tabbar);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
return tabBar;
|
|
753
|
+
}
|
|
704
754
|
async updatePagesJSON(filepath) {
|
|
705
755
|
if (filepath) {
|
|
706
756
|
let page = this.pages.get(filepath);
|
|
@@ -714,9 +764,12 @@ class PageContext {
|
|
|
714
764
|
}
|
|
715
765
|
page = subPage;
|
|
716
766
|
}
|
|
717
|
-
if (page
|
|
718
|
-
|
|
719
|
-
|
|
767
|
+
if (page) {
|
|
768
|
+
await page.read();
|
|
769
|
+
if (!page.hasChanged()) {
|
|
770
|
+
debug.cache(`The route block on page ${filepath} did not send any changes, skipping`);
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
720
773
|
}
|
|
721
774
|
}
|
|
722
775
|
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
@@ -744,7 +797,8 @@ class PageContext {
|
|
|
744
797
|
const data = {
|
|
745
798
|
...this.pagesGlobConfig,
|
|
746
799
|
pages: this.pageMetaData,
|
|
747
|
-
subPackages: this.subPageMetaData
|
|
800
|
+
subPackages: this.subPageMetaData,
|
|
801
|
+
tabBar: await this.getTabBarMerged()
|
|
748
802
|
};
|
|
749
803
|
const pagesJson = stringify(
|
|
750
804
|
data,
|