@uni-helper/vite-plugin-uni-pages 0.1.7 → 0.1.9
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 +115 -37
- package/dist/index.d.ts +54 -24
- package/dist/index.mjs +113 -34
- package/package.json +5 -6
package/dist/index.cjs
CHANGED
|
@@ -2,28 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const path
|
|
6
|
-
const
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const node_child_process = require('node: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');
|
|
11
10
|
const unconfig = require('unconfig');
|
|
12
11
|
const uniEnv = require('@uni-helper/uni-env');
|
|
13
12
|
const Debug = require('debug');
|
|
14
13
|
const lodashUnified = require('lodash-unified');
|
|
15
14
|
const fg = require('fast-glob');
|
|
16
|
-
const fs = require('fs');
|
|
15
|
+
const fs = require('node:fs');
|
|
17
16
|
const JSON5 = require('json5');
|
|
18
17
|
const yaml = require('yaml');
|
|
19
18
|
const compilerSfc = require('@vue/compiler-sfc');
|
|
20
19
|
|
|
21
20
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
22
21
|
|
|
23
|
-
const path__default
|
|
22
|
+
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
24
23
|
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
25
24
|
const chokidar__default = /*#__PURE__*/_interopDefaultCompat(chokidar);
|
|
26
|
-
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
27
25
|
const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
|
|
28
26
|
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
29
27
|
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
@@ -53,6 +51,7 @@ const debug = {
|
|
|
53
51
|
routeBlock: Debug__default("vite-plugin-uni-pages:routeBlock"),
|
|
54
52
|
options: Debug__default("vite-plugin-uni-pages:options"),
|
|
55
53
|
pages: Debug__default("vite-plugin-uni-pages:pages"),
|
|
54
|
+
subPages: Debug__default("vite-plugin-uni-pages:subPages"),
|
|
56
55
|
error: Debug__default("vite-plugin-uni-pages:error")
|
|
57
56
|
};
|
|
58
57
|
function extsToGlob(extensions) {
|
|
@@ -79,22 +78,25 @@ function mergePageMetaDataArray(pageMetaData) {
|
|
|
79
78
|
}
|
|
80
79
|
return result;
|
|
81
80
|
}
|
|
82
|
-
|
|
81
|
+
async function getPagesConfigSourcePaths() {
|
|
83
82
|
return await fg__default("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
|
|
84
83
|
deep: 0,
|
|
85
84
|
onlyFiles: true,
|
|
86
85
|
absolute: true
|
|
87
86
|
});
|
|
88
|
-
}
|
|
87
|
+
}
|
|
89
88
|
|
|
90
89
|
function resolveOptions(userOptions, viteRoot) {
|
|
91
90
|
const {
|
|
91
|
+
homePage = ["pages/index", "pages/index/index"],
|
|
92
92
|
mergePages = true,
|
|
93
93
|
dir = "src/pages",
|
|
94
|
+
subPackages = [],
|
|
94
95
|
outDir = "src",
|
|
95
96
|
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
96
97
|
routeBlockLang = "json5",
|
|
97
98
|
minify = false,
|
|
99
|
+
debug = false,
|
|
98
100
|
onBeforeLoadUserConfig = () => {
|
|
99
101
|
},
|
|
100
102
|
onAfterLoadUserConfig = () => {
|
|
@@ -114,14 +116,19 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
114
116
|
} = userOptions;
|
|
115
117
|
const root = viteRoot || slash(process.cwd());
|
|
116
118
|
const resolvedDirs = resolvePageDirs(dir, root, exclude);
|
|
119
|
+
const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
|
|
120
|
+
const ResolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
|
|
117
121
|
const resolvedOptions = {
|
|
122
|
+
homePage: ResolvedHomePage,
|
|
118
123
|
mergePages,
|
|
119
124
|
dirs: resolvedDirs,
|
|
125
|
+
subPackages: resolvedSubDirs,
|
|
120
126
|
outDir,
|
|
121
127
|
exclude,
|
|
122
128
|
routeBlockLang,
|
|
123
129
|
root,
|
|
124
130
|
minify,
|
|
131
|
+
debug,
|
|
125
132
|
onBeforeLoadUserConfig,
|
|
126
133
|
onAfterLoadUserConfig,
|
|
127
134
|
onBeforeScanPages,
|
|
@@ -186,7 +193,7 @@ function parseCustomBlock(block, filePath, options) {
|
|
|
186
193
|
};
|
|
187
194
|
let content;
|
|
188
195
|
debug.routeBlock(`use ${lang} parser`);
|
|
189
|
-
if (lang === "json5") {
|
|
196
|
+
if (lang === "json5" || lang === "jsonc") {
|
|
190
197
|
try {
|
|
191
198
|
content = JSON5__default.parse(block.content);
|
|
192
199
|
} catch (err) {
|
|
@@ -234,12 +241,20 @@ async function getRouteBlock(path, options) {
|
|
|
234
241
|
class PageContext {
|
|
235
242
|
constructor(userOptions, viteRoot = process.cwd()) {
|
|
236
243
|
this.pagesPath = [];
|
|
244
|
+
this.subPagesPath = {};
|
|
237
245
|
this.pageMetaData = [];
|
|
246
|
+
this.subPageMetaData = [];
|
|
238
247
|
this.resolvedPagesJSONPath = "";
|
|
239
248
|
this.rawOptions = userOptions;
|
|
240
249
|
this.root = slash(viteRoot);
|
|
241
250
|
debug.options("root", this.root);
|
|
242
251
|
this.options = resolveOptions(userOptions, this.root);
|
|
252
|
+
const debugOption = this.options.debug;
|
|
253
|
+
if (debugOption) {
|
|
254
|
+
const prefix = "vite-plugin-uni-pages:";
|
|
255
|
+
const suffix = typeof debugOption === "boolean" ? "*" : debugOption;
|
|
256
|
+
Debug__default.enable(`${prefix}${suffix}`);
|
|
257
|
+
}
|
|
243
258
|
this.resolvedPagesJSONPath = path__default.join(this.root, this.options.outDir, OUTPUT_NAME);
|
|
244
259
|
debug.options(this.options);
|
|
245
260
|
}
|
|
@@ -257,19 +272,20 @@ class PageContext {
|
|
|
257
272
|
}
|
|
258
273
|
async scanPages() {
|
|
259
274
|
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 };
|
|
275
|
+
return { dir, files: getPagePaths(dir, this.options) };
|
|
269
276
|
});
|
|
270
277
|
this.pagesPath = pageDirFiles.map((page) => page.files).flat();
|
|
271
278
|
debug.pages(this.pagesPath);
|
|
272
279
|
}
|
|
280
|
+
async scanSubPages() {
|
|
281
|
+
const subPagesPath = {};
|
|
282
|
+
for (const dir of this.options.subPackages) {
|
|
283
|
+
const pagePaths = getPagePaths(dir, this.options);
|
|
284
|
+
subPagesPath[dir] = pagePaths;
|
|
285
|
+
}
|
|
286
|
+
this.subPagesPath = subPagesPath;
|
|
287
|
+
debug.subPages(this.subPagesPath);
|
|
288
|
+
}
|
|
273
289
|
setupViteServer(server) {
|
|
274
290
|
if (this._server === server)
|
|
275
291
|
return;
|
|
@@ -319,26 +335,70 @@ class PageContext {
|
|
|
319
335
|
const { relativePath, absolutePath } = page;
|
|
320
336
|
const routeBlock = await getRouteBlock(absolutePath, this.options);
|
|
321
337
|
const relativePathWithFileName = relativePath.replace(path__default.extname(relativePath), "");
|
|
322
|
-
const pageMetaDatum = {
|
|
338
|
+
const pageMetaDatum = {
|
|
339
|
+
path: vite.normalizePath(relativePathWithFileName),
|
|
340
|
+
type: routeBlock?.attr.type ?? "page"
|
|
341
|
+
};
|
|
323
342
|
if (routeBlock)
|
|
324
343
|
Object.assign(pageMetaDatum, routeBlock.content);
|
|
325
344
|
return pageMetaDatum;
|
|
326
345
|
}
|
|
327
|
-
async
|
|
328
|
-
const generatedPageMetaData = await Promise.all(
|
|
329
|
-
|
|
330
|
-
);
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
346
|
+
async parsePages(pages, overrides) {
|
|
347
|
+
const generatedPageMetaData = await Promise.all(pages.map(async (page) => await this.parsePage(page)));
|
|
348
|
+
const customPageMetaData = overrides || [];
|
|
349
|
+
const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
|
|
350
|
+
this.setHomePage(result);
|
|
351
|
+
result.sort((page) => page.type === "home" ? -1 : 0);
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
setHomePage(result) {
|
|
355
|
+
const hasHome = result.some((page) => {
|
|
356
|
+
if (page.type === "home")
|
|
357
|
+
return true;
|
|
358
|
+
const base = page.path.split("/")[0];
|
|
359
|
+
if (this.options.subPackages.includes(`src/${base}`))
|
|
360
|
+
return true;
|
|
361
|
+
return false;
|
|
362
|
+
});
|
|
363
|
+
if (hasHome)
|
|
364
|
+
return true;
|
|
365
|
+
const isFoundHome = result.some((item) => {
|
|
366
|
+
if (this.options.homePage.includes(item.path)) {
|
|
367
|
+
item.type = "home";
|
|
368
|
+
return true;
|
|
369
|
+
} else {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
if (isFoundHome) {
|
|
374
|
+
return true;
|
|
334
375
|
} else {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
this.pageMetaData = generatedPageMetaData;
|
|
376
|
+
this.logger?.warn('No home page found, check the configuration of pages.config.ts, or add the `homePage` option to UniPages in vite.config.js, or add `type="home"` to the routeBlock of your vue page.', {
|
|
377
|
+
timestamp: true
|
|
378
|
+
});
|
|
339
379
|
}
|
|
340
|
-
|
|
341
|
-
|
|
380
|
+
}
|
|
381
|
+
async mergePageMetaData() {
|
|
382
|
+
const pageMetaData = await this.parsePages(this.pagesPath, this.pagesGlobConfig?.pages);
|
|
383
|
+
this.pageMetaData = pageMetaData;
|
|
384
|
+
debug.pages(this.pageMetaData);
|
|
385
|
+
}
|
|
386
|
+
async mergeSubPageMetaData() {
|
|
387
|
+
const subPageMaps = {};
|
|
388
|
+
const subPackages = this.pagesGlobConfig?.subPackages || [];
|
|
389
|
+
for (const [dir, pages] of Object.entries(this.subPagesPath)) {
|
|
390
|
+
const root = path__default.basename(dir);
|
|
391
|
+
const globPackage = subPackages?.find((v) => v.root === root);
|
|
392
|
+
subPageMaps[root] = await this.parsePages(pages, globPackage?.pages);
|
|
393
|
+
subPageMaps[root] = subPageMaps[root].map((page) => ({ ...page, path: slash(path__default.relative(root, page.path)) }));
|
|
394
|
+
}
|
|
395
|
+
for (const { root, pages } of subPackages) {
|
|
396
|
+
if (root && !subPageMaps[root])
|
|
397
|
+
subPageMaps[root] = pages || [];
|
|
398
|
+
}
|
|
399
|
+
const subPageMetaData = Object.keys(subPageMaps).map((root) => ({ root, pages: subPageMaps[root] }));
|
|
400
|
+
this.subPageMetaData = subPageMetaData;
|
|
401
|
+
debug.subPages(this.subPageMetaData);
|
|
342
402
|
}
|
|
343
403
|
async updatePagesJSON() {
|
|
344
404
|
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
@@ -348,13 +408,20 @@ class PageContext {
|
|
|
348
408
|
if (this.options.mergePages) {
|
|
349
409
|
this.options.onBeforeScanPages(this);
|
|
350
410
|
await this.scanPages();
|
|
411
|
+
await this.scanSubPages();
|
|
351
412
|
this.options.onAfterScanPages(this);
|
|
352
413
|
}
|
|
353
414
|
this.options.onBeforeMergePageMetaData(this);
|
|
354
415
|
await this.mergePageMetaData();
|
|
416
|
+
await this.mergeSubPageMetaData();
|
|
355
417
|
this.options.onAfterMergePageMetaData(this);
|
|
356
418
|
this.options.onBeforeWriteFile(this);
|
|
357
|
-
const
|
|
419
|
+
const data = {
|
|
420
|
+
...this.pagesGlobConfig,
|
|
421
|
+
pages: this.pageMetaData,
|
|
422
|
+
subPackages: this.subPageMetaData
|
|
423
|
+
};
|
|
424
|
+
const pagesJson = JSON.stringify(data, null, this.options.minify ? void 0 : 2);
|
|
358
425
|
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
359
426
|
this.options.onAfterWriteFile(this);
|
|
360
427
|
}
|
|
@@ -365,6 +432,17 @@ class PageContext {
|
|
|
365
432
|
return JSON.stringify(this.pageMetaData, null, 2);
|
|
366
433
|
}
|
|
367
434
|
}
|
|
435
|
+
function getPagePaths(dir, options) {
|
|
436
|
+
const pagesDirPath = slash(path__default.resolve(options.root, dir));
|
|
437
|
+
const basePath = slash(path__default.join(options.root, options.outDir));
|
|
438
|
+
const files = getPageFiles(pagesDirPath, options);
|
|
439
|
+
debug.pages(dir, files);
|
|
440
|
+
const pagePaths = files.map((file) => slash(file)).map((file) => ({
|
|
441
|
+
relativePath: path__default.relative(basePath, slash(path__default.resolve(pagesDirPath, file))),
|
|
442
|
+
absolutePath: slash(path__default.resolve(pagesDirPath, file))
|
|
443
|
+
}));
|
|
444
|
+
return pagePaths;
|
|
445
|
+
}
|
|
368
446
|
|
|
369
447
|
function defineUniPages(config) {
|
|
370
448
|
return config;
|
|
@@ -372,7 +450,7 @@ function defineUniPages(config) {
|
|
|
372
450
|
|
|
373
451
|
async function restart() {
|
|
374
452
|
return new Promise((resolve) => {
|
|
375
|
-
const build =
|
|
453
|
+
const build = node_child_process.spawn(process.argv.shift(), process.argv, {
|
|
376
454
|
cwd: process.cwd(),
|
|
377
455
|
detached: true,
|
|
378
456
|
env: process.env
|
|
@@ -384,9 +462,9 @@ async function restart() {
|
|
|
384
462
|
});
|
|
385
463
|
});
|
|
386
464
|
}
|
|
387
|
-
|
|
465
|
+
function VitePluginUniPages(userOptions = {}) {
|
|
388
466
|
let ctx;
|
|
389
|
-
const resolvedPagesJSONPath = path__default
|
|
467
|
+
const resolvedPagesJSONPath = path__default.join(
|
|
390
468
|
process.cwd(),
|
|
391
469
|
userOptions.outDir ?? "src",
|
|
392
470
|
OUTPUT_NAME
|
|
@@ -440,7 +518,7 @@ const VitePluginUniPages = (userOptions = {}) => {
|
|
|
440
518
|
return ctx.virtualModule();
|
|
441
519
|
}
|
|
442
520
|
};
|
|
443
|
-
}
|
|
521
|
+
}
|
|
444
522
|
|
|
445
523
|
exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
|
|
446
524
|
exports.MODULE_ID_VIRTUAL = MODULE_ID_VIRTUAL;
|
package/dist/index.d.ts
CHANGED
|
@@ -22,10 +22,10 @@ interface AppPlus {
|
|
|
22
22
|
*/
|
|
23
23
|
background?: HEXColor;
|
|
24
24
|
/**
|
|
25
|
-
* 导航栏
|
|
25
|
+
* 导航栏 ,详见?:导航栏; 设置为 false 不显示默认导航栏
|
|
26
26
|
* @desc App、H5
|
|
27
27
|
*/
|
|
28
|
-
titleNView?: object;
|
|
28
|
+
titleNView?: object | false;
|
|
29
29
|
/**
|
|
30
30
|
* 原生子窗体,详见?:原生子窗体
|
|
31
31
|
* @desc App 1.9.10+
|
|
@@ -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,21 +651,51 @@ 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[]>;
|
|
660
|
+
setHomePage(result: PageMetaDatum[]): true | undefined;
|
|
656
661
|
mergePageMetaData(): Promise<void>;
|
|
662
|
+
mergeSubPageMetaData(): Promise<void>;
|
|
657
663
|
updatePagesJSON(): Promise<void>;
|
|
658
664
|
virtualModule(): Promise<string>;
|
|
659
665
|
resolveRoutes(): string;
|
|
660
666
|
}
|
|
661
667
|
|
|
668
|
+
declare function invalidatePagesModule(server: ViteDevServer): void;
|
|
669
|
+
declare const debug: {
|
|
670
|
+
hmr: Debug.Debugger;
|
|
671
|
+
routeBlock: Debug.Debugger;
|
|
672
|
+
options: Debug.Debugger;
|
|
673
|
+
pages: Debug.Debugger;
|
|
674
|
+
subPages: Debug.Debugger;
|
|
675
|
+
error: Debug.Debugger;
|
|
676
|
+
};
|
|
677
|
+
declare function extsToGlob(extensions: string[]): string;
|
|
678
|
+
declare function isTargetFile(path: string): boolean;
|
|
679
|
+
declare function isConfigFile(path: string): boolean;
|
|
680
|
+
/**
|
|
681
|
+
* merge page meta data array by path and assign style
|
|
682
|
+
* @param pageMetaData page meta data array
|
|
683
|
+
* TODO: support merge middleware
|
|
684
|
+
*/
|
|
685
|
+
declare function mergePageMetaDataArray(pageMetaData: PageMetaDatum[]): PageMetaDatum[];
|
|
686
|
+
declare function getPagesConfigSourcePaths(): Promise<string[]>;
|
|
687
|
+
|
|
662
688
|
interface CustomBlock {
|
|
663
689
|
attr: Record<string, any>;
|
|
664
690
|
content: Record<string, any>;
|
|
665
691
|
}
|
|
692
|
+
type debugType = keyof typeof debug;
|
|
666
693
|
interface Options {
|
|
694
|
+
/**
|
|
695
|
+
* The default application entry page is the home page
|
|
696
|
+
* @default 'pages/index' or 'pages/index/index'
|
|
697
|
+
*/
|
|
698
|
+
homePage: string;
|
|
667
699
|
/**
|
|
668
700
|
* Whether to merge pages in pages.json
|
|
669
701
|
* @default true
|
|
@@ -674,6 +706,11 @@ interface Options {
|
|
|
674
706
|
* @default 'src/pages'
|
|
675
707
|
*/
|
|
676
708
|
dir: string;
|
|
709
|
+
/**
|
|
710
|
+
* all root directories loaded by subPackages
|
|
711
|
+
* @default []
|
|
712
|
+
*/
|
|
713
|
+
subPackages: string[];
|
|
677
714
|
/**
|
|
678
715
|
* pages.json dir
|
|
679
716
|
* @default "src"
|
|
@@ -694,6 +731,10 @@ interface Options {
|
|
|
694
731
|
* @default false
|
|
695
732
|
*/
|
|
696
733
|
minify: boolean;
|
|
734
|
+
/**
|
|
735
|
+
* enable debug log
|
|
736
|
+
*/
|
|
737
|
+
debug: boolean | debugType;
|
|
697
738
|
onBeforeLoadUserConfig: (ctx: PageContext) => void;
|
|
698
739
|
onAfterLoadUserConfig: (ctx: PageContext) => void;
|
|
699
740
|
onBeforeScanPages: (ctx: PageContext) => void;
|
|
@@ -704,7 +745,7 @@ interface Options {
|
|
|
704
745
|
onAfterWriteFile: (ctx: PageContext) => void;
|
|
705
746
|
}
|
|
706
747
|
type UserOptions = Partial<Options>;
|
|
707
|
-
interface ResolvedOptions extends Omit<Options, 'dir'> {
|
|
748
|
+
interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage'> {
|
|
708
749
|
/**
|
|
709
750
|
* Resolves to the `root` value from Vite config.
|
|
710
751
|
* @default config.root
|
|
@@ -714,6 +755,10 @@ interface ResolvedOptions extends Omit<Options, 'dir'> {
|
|
|
714
755
|
* Resolved page dirs
|
|
715
756
|
*/
|
|
716
757
|
dirs: string[];
|
|
758
|
+
/**
|
|
759
|
+
* Resolved entry page
|
|
760
|
+
*/
|
|
761
|
+
homePage: string[];
|
|
717
762
|
}
|
|
718
763
|
interface PagePath {
|
|
719
764
|
relativePath: string;
|
|
@@ -735,31 +780,16 @@ interface PageMetaDatum {
|
|
|
735
780
|
needLogin?: boolean;
|
|
736
781
|
[x: string]: any;
|
|
737
782
|
}
|
|
783
|
+
interface SubPageMetaDatum {
|
|
784
|
+
root: string;
|
|
785
|
+
pages: PageMetaDatum[];
|
|
786
|
+
}
|
|
738
787
|
|
|
739
788
|
declare const MODULE_ID_VIRTUAL = "virtual:uni-pages";
|
|
740
789
|
declare const RESOLVED_MODULE_ID_VIRTUAL: string;
|
|
741
790
|
declare const OUTPUT_NAME = "pages.json";
|
|
742
791
|
declare const FILE_EXTENSIONS: string[];
|
|
743
792
|
|
|
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
793
|
/**
|
|
764
794
|
* Resolves the files that are valid pages for the given context.
|
|
765
795
|
*/
|
|
@@ -777,6 +807,6 @@ declare function parseSFC(code: string): Promise<SFCDescriptor>;
|
|
|
777
807
|
declare function parseCustomBlock(block: SFCBlock, filePath: string, options: ResolvedOptions): CustomBlock | undefined;
|
|
778
808
|
declare function getRouteBlock(path: string, options: ResolvedOptions): Promise<CustomBlock | undefined>;
|
|
779
809
|
|
|
780
|
-
declare
|
|
810
|
+
declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
|
|
781
811
|
|
|
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 };
|
|
812
|
+
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,15 +1,14 @@
|
|
|
1
|
-
import path
|
|
2
|
-
import { spawn } from 'child_process';
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { spawn } from 'node: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';
|
|
7
6
|
import { loadConfig } from 'unconfig';
|
|
8
7
|
import { isH5 } from '@uni-helper/uni-env';
|
|
9
8
|
import Debug from 'debug';
|
|
10
9
|
import { groupBy } from 'lodash-unified';
|
|
11
10
|
import fg from 'fast-glob';
|
|
12
|
-
import fs from 'fs';
|
|
11
|
+
import fs from 'node:fs';
|
|
13
12
|
import JSON5 from 'json5';
|
|
14
13
|
import { parse as parse$1 } from 'yaml';
|
|
15
14
|
import { parse } from '@vue/compiler-sfc';
|
|
@@ -38,6 +37,7 @@ const debug = {
|
|
|
38
37
|
routeBlock: Debug("vite-plugin-uni-pages:routeBlock"),
|
|
39
38
|
options: Debug("vite-plugin-uni-pages:options"),
|
|
40
39
|
pages: Debug("vite-plugin-uni-pages:pages"),
|
|
40
|
+
subPages: Debug("vite-plugin-uni-pages:subPages"),
|
|
41
41
|
error: Debug("vite-plugin-uni-pages:error")
|
|
42
42
|
};
|
|
43
43
|
function extsToGlob(extensions) {
|
|
@@ -64,22 +64,25 @@ function mergePageMetaDataArray(pageMetaData) {
|
|
|
64
64
|
}
|
|
65
65
|
return result;
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
async function getPagesConfigSourcePaths() {
|
|
68
68
|
return await fg("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
|
|
69
69
|
deep: 0,
|
|
70
70
|
onlyFiles: true,
|
|
71
71
|
absolute: true
|
|
72
72
|
});
|
|
73
|
-
}
|
|
73
|
+
}
|
|
74
74
|
|
|
75
75
|
function resolveOptions(userOptions, viteRoot) {
|
|
76
76
|
const {
|
|
77
|
+
homePage = ["pages/index", "pages/index/index"],
|
|
77
78
|
mergePages = true,
|
|
78
79
|
dir = "src/pages",
|
|
80
|
+
subPackages = [],
|
|
79
81
|
outDir = "src",
|
|
80
82
|
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
81
83
|
routeBlockLang = "json5",
|
|
82
84
|
minify = false,
|
|
85
|
+
debug = false,
|
|
83
86
|
onBeforeLoadUserConfig = () => {
|
|
84
87
|
},
|
|
85
88
|
onAfterLoadUserConfig = () => {
|
|
@@ -99,14 +102,19 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
99
102
|
} = userOptions;
|
|
100
103
|
const root = viteRoot || slash(process.cwd());
|
|
101
104
|
const resolvedDirs = resolvePageDirs(dir, root, exclude);
|
|
105
|
+
const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
|
|
106
|
+
const ResolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
|
|
102
107
|
const resolvedOptions = {
|
|
108
|
+
homePage: ResolvedHomePage,
|
|
103
109
|
mergePages,
|
|
104
110
|
dirs: resolvedDirs,
|
|
111
|
+
subPackages: resolvedSubDirs,
|
|
105
112
|
outDir,
|
|
106
113
|
exclude,
|
|
107
114
|
routeBlockLang,
|
|
108
115
|
root,
|
|
109
116
|
minify,
|
|
117
|
+
debug,
|
|
110
118
|
onBeforeLoadUserConfig,
|
|
111
119
|
onAfterLoadUserConfig,
|
|
112
120
|
onBeforeScanPages,
|
|
@@ -171,7 +179,7 @@ function parseCustomBlock(block, filePath, options) {
|
|
|
171
179
|
};
|
|
172
180
|
let content;
|
|
173
181
|
debug.routeBlock(`use ${lang} parser`);
|
|
174
|
-
if (lang === "json5") {
|
|
182
|
+
if (lang === "json5" || lang === "jsonc") {
|
|
175
183
|
try {
|
|
176
184
|
content = JSON5.parse(block.content);
|
|
177
185
|
} catch (err) {
|
|
@@ -219,12 +227,20 @@ async function getRouteBlock(path, options) {
|
|
|
219
227
|
class PageContext {
|
|
220
228
|
constructor(userOptions, viteRoot = process.cwd()) {
|
|
221
229
|
this.pagesPath = [];
|
|
230
|
+
this.subPagesPath = {};
|
|
222
231
|
this.pageMetaData = [];
|
|
232
|
+
this.subPageMetaData = [];
|
|
223
233
|
this.resolvedPagesJSONPath = "";
|
|
224
234
|
this.rawOptions = userOptions;
|
|
225
235
|
this.root = slash(viteRoot);
|
|
226
236
|
debug.options("root", this.root);
|
|
227
237
|
this.options = resolveOptions(userOptions, this.root);
|
|
238
|
+
const debugOption = this.options.debug;
|
|
239
|
+
if (debugOption) {
|
|
240
|
+
const prefix = "vite-plugin-uni-pages:";
|
|
241
|
+
const suffix = typeof debugOption === "boolean" ? "*" : debugOption;
|
|
242
|
+
Debug.enable(`${prefix}${suffix}`);
|
|
243
|
+
}
|
|
228
244
|
this.resolvedPagesJSONPath = path.join(this.root, this.options.outDir, OUTPUT_NAME);
|
|
229
245
|
debug.options(this.options);
|
|
230
246
|
}
|
|
@@ -242,19 +258,20 @@ class PageContext {
|
|
|
242
258
|
}
|
|
243
259
|
async scanPages() {
|
|
244
260
|
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 };
|
|
261
|
+
return { dir, files: getPagePaths(dir, this.options) };
|
|
254
262
|
});
|
|
255
263
|
this.pagesPath = pageDirFiles.map((page) => page.files).flat();
|
|
256
264
|
debug.pages(this.pagesPath);
|
|
257
265
|
}
|
|
266
|
+
async scanSubPages() {
|
|
267
|
+
const subPagesPath = {};
|
|
268
|
+
for (const dir of this.options.subPackages) {
|
|
269
|
+
const pagePaths = getPagePaths(dir, this.options);
|
|
270
|
+
subPagesPath[dir] = pagePaths;
|
|
271
|
+
}
|
|
272
|
+
this.subPagesPath = subPagesPath;
|
|
273
|
+
debug.subPages(this.subPagesPath);
|
|
274
|
+
}
|
|
258
275
|
setupViteServer(server) {
|
|
259
276
|
if (this._server === server)
|
|
260
277
|
return;
|
|
@@ -304,26 +321,70 @@ class PageContext {
|
|
|
304
321
|
const { relativePath, absolutePath } = page;
|
|
305
322
|
const routeBlock = await getRouteBlock(absolutePath, this.options);
|
|
306
323
|
const relativePathWithFileName = relativePath.replace(path.extname(relativePath), "");
|
|
307
|
-
const pageMetaDatum = {
|
|
324
|
+
const pageMetaDatum = {
|
|
325
|
+
path: normalizePath(relativePathWithFileName),
|
|
326
|
+
type: routeBlock?.attr.type ?? "page"
|
|
327
|
+
};
|
|
308
328
|
if (routeBlock)
|
|
309
329
|
Object.assign(pageMetaDatum, routeBlock.content);
|
|
310
330
|
return pageMetaDatum;
|
|
311
331
|
}
|
|
312
|
-
async
|
|
313
|
-
const generatedPageMetaData = await Promise.all(
|
|
314
|
-
|
|
315
|
-
);
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
332
|
+
async parsePages(pages, overrides) {
|
|
333
|
+
const generatedPageMetaData = await Promise.all(pages.map(async (page) => await this.parsePage(page)));
|
|
334
|
+
const customPageMetaData = overrides || [];
|
|
335
|
+
const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
|
|
336
|
+
this.setHomePage(result);
|
|
337
|
+
result.sort((page) => page.type === "home" ? -1 : 0);
|
|
338
|
+
return result;
|
|
339
|
+
}
|
|
340
|
+
setHomePage(result) {
|
|
341
|
+
const hasHome = result.some((page) => {
|
|
342
|
+
if (page.type === "home")
|
|
343
|
+
return true;
|
|
344
|
+
const base = page.path.split("/")[0];
|
|
345
|
+
if (this.options.subPackages.includes(`src/${base}`))
|
|
346
|
+
return true;
|
|
347
|
+
return false;
|
|
348
|
+
});
|
|
349
|
+
if (hasHome)
|
|
350
|
+
return true;
|
|
351
|
+
const isFoundHome = result.some((item) => {
|
|
352
|
+
if (this.options.homePage.includes(item.path)) {
|
|
353
|
+
item.type = "home";
|
|
354
|
+
return true;
|
|
355
|
+
} else {
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
if (isFoundHome) {
|
|
360
|
+
return true;
|
|
319
361
|
} else {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
362
|
+
this.logger?.warn('No home page found, check the configuration of pages.config.ts, or add the `homePage` option to UniPages in vite.config.js, or add `type="home"` to the routeBlock of your vue page.', {
|
|
363
|
+
timestamp: true
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
async mergePageMetaData() {
|
|
368
|
+
const pageMetaData = await this.parsePages(this.pagesPath, this.pagesGlobConfig?.pages);
|
|
369
|
+
this.pageMetaData = pageMetaData;
|
|
370
|
+
debug.pages(this.pageMetaData);
|
|
371
|
+
}
|
|
372
|
+
async mergeSubPageMetaData() {
|
|
373
|
+
const subPageMaps = {};
|
|
374
|
+
const subPackages = this.pagesGlobConfig?.subPackages || [];
|
|
375
|
+
for (const [dir, pages] of Object.entries(this.subPagesPath)) {
|
|
376
|
+
const root = path.basename(dir);
|
|
377
|
+
const globPackage = subPackages?.find((v) => v.root === root);
|
|
378
|
+
subPageMaps[root] = await this.parsePages(pages, globPackage?.pages);
|
|
379
|
+
subPageMaps[root] = subPageMaps[root].map((page) => ({ ...page, path: slash(path.relative(root, page.path)) }));
|
|
380
|
+
}
|
|
381
|
+
for (const { root, pages } of subPackages) {
|
|
382
|
+
if (root && !subPageMaps[root])
|
|
383
|
+
subPageMaps[root] = pages || [];
|
|
324
384
|
}
|
|
325
|
-
|
|
326
|
-
|
|
385
|
+
const subPageMetaData = Object.keys(subPageMaps).map((root) => ({ root, pages: subPageMaps[root] }));
|
|
386
|
+
this.subPageMetaData = subPageMetaData;
|
|
387
|
+
debug.subPages(this.subPageMetaData);
|
|
327
388
|
}
|
|
328
389
|
async updatePagesJSON() {
|
|
329
390
|
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
@@ -333,13 +394,20 @@ class PageContext {
|
|
|
333
394
|
if (this.options.mergePages) {
|
|
334
395
|
this.options.onBeforeScanPages(this);
|
|
335
396
|
await this.scanPages();
|
|
397
|
+
await this.scanSubPages();
|
|
336
398
|
this.options.onAfterScanPages(this);
|
|
337
399
|
}
|
|
338
400
|
this.options.onBeforeMergePageMetaData(this);
|
|
339
401
|
await this.mergePageMetaData();
|
|
402
|
+
await this.mergeSubPageMetaData();
|
|
340
403
|
this.options.onAfterMergePageMetaData(this);
|
|
341
404
|
this.options.onBeforeWriteFile(this);
|
|
342
|
-
const
|
|
405
|
+
const data = {
|
|
406
|
+
...this.pagesGlobConfig,
|
|
407
|
+
pages: this.pageMetaData,
|
|
408
|
+
subPackages: this.subPageMetaData
|
|
409
|
+
};
|
|
410
|
+
const pagesJson = JSON.stringify(data, null, this.options.minify ? void 0 : 2);
|
|
343
411
|
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
344
412
|
this.options.onAfterWriteFile(this);
|
|
345
413
|
}
|
|
@@ -350,6 +418,17 @@ class PageContext {
|
|
|
350
418
|
return JSON.stringify(this.pageMetaData, null, 2);
|
|
351
419
|
}
|
|
352
420
|
}
|
|
421
|
+
function getPagePaths(dir, options) {
|
|
422
|
+
const pagesDirPath = slash(path.resolve(options.root, dir));
|
|
423
|
+
const basePath = slash(path.join(options.root, options.outDir));
|
|
424
|
+
const files = getPageFiles(pagesDirPath, options);
|
|
425
|
+
debug.pages(dir, files);
|
|
426
|
+
const pagePaths = files.map((file) => slash(file)).map((file) => ({
|
|
427
|
+
relativePath: path.relative(basePath, slash(path.resolve(pagesDirPath, file))),
|
|
428
|
+
absolutePath: slash(path.resolve(pagesDirPath, file))
|
|
429
|
+
}));
|
|
430
|
+
return pagePaths;
|
|
431
|
+
}
|
|
353
432
|
|
|
354
433
|
function defineUniPages(config) {
|
|
355
434
|
return config;
|
|
@@ -369,9 +448,9 @@ async function restart() {
|
|
|
369
448
|
});
|
|
370
449
|
});
|
|
371
450
|
}
|
|
372
|
-
|
|
451
|
+
function VitePluginUniPages(userOptions = {}) {
|
|
373
452
|
let ctx;
|
|
374
|
-
const resolvedPagesJSONPath = path
|
|
453
|
+
const resolvedPagesJSONPath = path.join(
|
|
375
454
|
process.cwd(),
|
|
376
455
|
userOptions.outDir ?? "src",
|
|
377
456
|
OUTPUT_NAME
|
|
@@ -425,6 +504,6 @@ const VitePluginUniPages = (userOptions = {}) => {
|
|
|
425
504
|
return ctx.virtualModule();
|
|
426
505
|
}
|
|
427
506
|
};
|
|
428
|
-
}
|
|
507
|
+
}
|
|
429
508
|
|
|
430
509
|
export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, checkPagesJsonFile, debug, VitePluginUniPages as default, defineUniPages, extsToGlob, getPageFiles, getPagesConfigSourcePaths, getRouteBlock, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, writeFileSync };
|
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.1.
|
|
4
|
+
"version": "0.1.9",
|
|
5
5
|
"description": "File system based routing for uni-app applications using Vite",
|
|
6
6
|
"author": "KeJun",
|
|
7
7
|
"license": "MIT",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"client.d.ts"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@uni-helper/uni-env": "^0.0.
|
|
35
|
-
"@vue/compiler-sfc": "^3.
|
|
34
|
+
"@uni-helper/uni-env": "^0.0.3",
|
|
35
|
+
"@vue/compiler-sfc": "^3.3.1",
|
|
36
36
|
"chokidar": "^3.5.3",
|
|
37
37
|
"debug": "^4.3.4",
|
|
38
38
|
"fast-glob": "^3.2.12",
|
|
@@ -40,13 +40,12 @@
|
|
|
40
40
|
"lodash-unified": "^1.0.3",
|
|
41
41
|
"magic-string": "^0.30.0",
|
|
42
42
|
"unconfig": "^0.3.7",
|
|
43
|
-
"yaml": "^2.2.
|
|
43
|
+
"yaml": "^2.2.2"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@antfu/utils": "^0.7.2",
|
|
47
47
|
"@types/debug": "^4.1.7",
|
|
48
|
-
"@types/node": "^18.
|
|
49
|
-
"vite": "^4.2.1"
|
|
48
|
+
"@types/node": "^18.16.7"
|
|
50
49
|
},
|
|
51
50
|
"scripts": {
|
|
52
51
|
"build": "unbuild",
|