@uni-helper/vite-plugin-uni-pages 0.1.8 → 0.2.0
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/README.md +1 -0
- package/dist/index.cjs +43 -17
- package/dist/index.d.ts +14 -4
- package/dist/index.mjs +41 -13
- package/package.json +5 -6
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -2,33 +2,29 @@
|
|
|
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');
|
|
17
|
-
const fs$1 = require('node:fs');
|
|
15
|
+
const fs = require('node:fs');
|
|
18
16
|
const JSON5 = require('json5');
|
|
19
17
|
const yaml = require('yaml');
|
|
20
18
|
const compilerSfc = require('@vue/compiler-sfc');
|
|
21
19
|
|
|
22
20
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
23
21
|
|
|
24
|
-
const path__default
|
|
22
|
+
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
25
23
|
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
26
24
|
const chokidar__default = /*#__PURE__*/_interopDefaultCompat(chokidar);
|
|
27
|
-
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
28
25
|
const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
|
|
29
26
|
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
30
27
|
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
31
|
-
const fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
|
|
32
28
|
const JSON5__default = /*#__PURE__*/_interopDefaultCompat(JSON5);
|
|
33
29
|
|
|
34
30
|
function slash(str) {
|
|
@@ -92,6 +88,7 @@ async function getPagesConfigSourcePaths() {
|
|
|
92
88
|
|
|
93
89
|
function resolveOptions(userOptions, viteRoot) {
|
|
94
90
|
const {
|
|
91
|
+
homePage = ["pages/index", "pages/index/index"],
|
|
95
92
|
mergePages = true,
|
|
96
93
|
dir = "src/pages",
|
|
97
94
|
subPackages = [],
|
|
@@ -120,7 +117,9 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
120
117
|
const root = viteRoot || slash(process.cwd());
|
|
121
118
|
const resolvedDirs = resolvePageDirs(dir, root, exclude);
|
|
122
119
|
const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
|
|
120
|
+
const ResolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
|
|
123
121
|
const resolvedOptions = {
|
|
122
|
+
homePage: ResolvedHomePage,
|
|
124
123
|
mergePages,
|
|
125
124
|
dirs: resolvedDirs,
|
|
126
125
|
subPackages: resolvedSubDirs,
|
|
@@ -230,7 +229,7 @@ ${err.message}`
|
|
|
230
229
|
}
|
|
231
230
|
}
|
|
232
231
|
async function getRouteBlock(path, options) {
|
|
233
|
-
const content = fs__default
|
|
232
|
+
const content = fs__default.readFileSync(path, "utf8");
|
|
234
233
|
const parsedSFC = await parseSFC(content);
|
|
235
234
|
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
|
|
236
235
|
if (!blockStr)
|
|
@@ -336,20 +335,47 @@ class PageContext {
|
|
|
336
335
|
const { relativePath, absolutePath } = page;
|
|
337
336
|
const routeBlock = await getRouteBlock(absolutePath, this.options);
|
|
338
337
|
const relativePathWithFileName = relativePath.replace(path__default.extname(relativePath), "");
|
|
339
|
-
const pageMetaDatum = {
|
|
338
|
+
const pageMetaDatum = {
|
|
339
|
+
path: vite.normalizePath(relativePathWithFileName),
|
|
340
|
+
type: routeBlock?.attr.type ?? "page"
|
|
341
|
+
};
|
|
340
342
|
if (routeBlock)
|
|
341
343
|
Object.assign(pageMetaDatum, routeBlock.content);
|
|
342
344
|
return pageMetaDatum;
|
|
343
345
|
}
|
|
344
346
|
async parsePages(pages, overrides) {
|
|
345
|
-
const generatedPageMetaData = await Promise.all(
|
|
346
|
-
pages.map(async (page) => await this.parsePage(page))
|
|
347
|
-
);
|
|
347
|
+
const generatedPageMetaData = await Promise.all(pages.map(async (page) => await this.parsePage(page)));
|
|
348
348
|
const customPageMetaData = overrides || [];
|
|
349
349
|
const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
|
|
350
|
+
this.setHomePage(result);
|
|
350
351
|
result.sort((page) => page.type === "home" ? -1 : 0);
|
|
351
352
|
return result;
|
|
352
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;
|
|
375
|
+
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.', {
|
|
376
|
+
timestamp: true
|
|
377
|
+
});
|
|
378
|
+
}
|
|
353
379
|
async mergePageMetaData() {
|
|
354
380
|
const pageMetaData = await this.parsePages(this.pagesPath, this.pagesGlobConfig?.pages);
|
|
355
381
|
this.pageMetaData = pageMetaData;
|
|
@@ -422,7 +448,7 @@ function defineUniPages(config) {
|
|
|
422
448
|
|
|
423
449
|
async function restart() {
|
|
424
450
|
return new Promise((resolve) => {
|
|
425
|
-
const build =
|
|
451
|
+
const build = node_child_process.spawn(process.argv.shift(), process.argv, {
|
|
426
452
|
cwd: process.cwd(),
|
|
427
453
|
detached: true,
|
|
428
454
|
env: process.env
|
|
@@ -434,9 +460,9 @@ async function restart() {
|
|
|
434
460
|
});
|
|
435
461
|
});
|
|
436
462
|
}
|
|
437
|
-
|
|
463
|
+
function VitePluginUniPages(userOptions = {}) {
|
|
438
464
|
let ctx;
|
|
439
|
-
const resolvedPagesJSONPath = path__default
|
|
465
|
+
const resolvedPagesJSONPath = path__default.join(
|
|
440
466
|
process.cwd(),
|
|
441
467
|
userOptions.outDir ?? "src",
|
|
442
468
|
OUTPUT_NAME
|
|
@@ -490,7 +516,7 @@ const VitePluginUniPages = (userOptions = {}) => {
|
|
|
490
516
|
return ctx.virtualModule();
|
|
491
517
|
}
|
|
492
518
|
};
|
|
493
|
-
}
|
|
519
|
+
}
|
|
494
520
|
|
|
495
521
|
exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
|
|
496
522
|
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+
|
|
@@ -657,6 +657,7 @@ declare class PageContext {
|
|
|
657
657
|
onUpdate(): void;
|
|
658
658
|
parsePage(page: PagePath): Promise<PageMetaDatum>;
|
|
659
659
|
parsePages(pages: PagePath[], overrides?: PageMetaDatum[]): Promise<PageMetaDatum[]>;
|
|
660
|
+
setHomePage(result: PageMetaDatum[]): true | undefined;
|
|
660
661
|
mergePageMetaData(): Promise<void>;
|
|
661
662
|
mergeSubPageMetaData(): Promise<void>;
|
|
662
663
|
updatePagesJSON(): Promise<void>;
|
|
@@ -690,6 +691,11 @@ interface CustomBlock {
|
|
|
690
691
|
}
|
|
691
692
|
type debugType = keyof typeof debug;
|
|
692
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;
|
|
693
699
|
/**
|
|
694
700
|
* Whether to merge pages in pages.json
|
|
695
701
|
* @default true
|
|
@@ -739,7 +745,7 @@ interface Options {
|
|
|
739
745
|
onAfterWriteFile: (ctx: PageContext) => void;
|
|
740
746
|
}
|
|
741
747
|
type UserOptions = Partial<Options>;
|
|
742
|
-
interface ResolvedOptions extends Omit<Options, 'dir'> {
|
|
748
|
+
interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage'> {
|
|
743
749
|
/**
|
|
744
750
|
* Resolves to the `root` value from Vite config.
|
|
745
751
|
* @default config.root
|
|
@@ -749,6 +755,10 @@ interface ResolvedOptions extends Omit<Options, 'dir'> {
|
|
|
749
755
|
* Resolved page dirs
|
|
750
756
|
*/
|
|
751
757
|
dirs: string[];
|
|
758
|
+
/**
|
|
759
|
+
* Resolved entry page
|
|
760
|
+
*/
|
|
761
|
+
homePage: string[];
|
|
752
762
|
}
|
|
753
763
|
interface PagePath {
|
|
754
764
|
relativePath: string;
|
|
@@ -797,6 +807,6 @@ declare function parseSFC(code: string): Promise<SFCDescriptor>;
|
|
|
797
807
|
declare function parseCustomBlock(block: SFCBlock, filePath: string, options: ResolvedOptions): CustomBlock | undefined;
|
|
798
808
|
declare function getRouteBlock(path: string, options: ResolvedOptions): Promise<CustomBlock | undefined>;
|
|
799
809
|
|
|
800
|
-
declare
|
|
810
|
+
declare function VitePluginUniPages(userOptions?: UserOptions): Plugin;
|
|
801
811
|
|
|
802
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,16 +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';
|
|
13
|
-
import fs$1 from 'node:fs';
|
|
11
|
+
import fs from 'node:fs';
|
|
14
12
|
import JSON5 from 'json5';
|
|
15
13
|
import { parse as parse$1 } from 'yaml';
|
|
16
14
|
import { parse } from '@vue/compiler-sfc';
|
|
@@ -76,6 +74,7 @@ async function getPagesConfigSourcePaths() {
|
|
|
76
74
|
|
|
77
75
|
function resolveOptions(userOptions, viteRoot) {
|
|
78
76
|
const {
|
|
77
|
+
homePage = ["pages/index", "pages/index/index"],
|
|
79
78
|
mergePages = true,
|
|
80
79
|
dir = "src/pages",
|
|
81
80
|
subPackages = [],
|
|
@@ -104,7 +103,9 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
104
103
|
const root = viteRoot || slash(process.cwd());
|
|
105
104
|
const resolvedDirs = resolvePageDirs(dir, root, exclude);
|
|
106
105
|
const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
|
|
106
|
+
const ResolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
|
|
107
107
|
const resolvedOptions = {
|
|
108
|
+
homePage: ResolvedHomePage,
|
|
108
109
|
mergePages,
|
|
109
110
|
dirs: resolvedDirs,
|
|
110
111
|
subPackages: resolvedSubDirs,
|
|
@@ -214,7 +215,7 @@ ${err.message}`
|
|
|
214
215
|
}
|
|
215
216
|
}
|
|
216
217
|
async function getRouteBlock(path, options) {
|
|
217
|
-
const content = fs
|
|
218
|
+
const content = fs.readFileSync(path, "utf8");
|
|
218
219
|
const parsedSFC = await parseSFC(content);
|
|
219
220
|
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
|
|
220
221
|
if (!blockStr)
|
|
@@ -320,20 +321,47 @@ class PageContext {
|
|
|
320
321
|
const { relativePath, absolutePath } = page;
|
|
321
322
|
const routeBlock = await getRouteBlock(absolutePath, this.options);
|
|
322
323
|
const relativePathWithFileName = relativePath.replace(path.extname(relativePath), "");
|
|
323
|
-
const pageMetaDatum = {
|
|
324
|
+
const pageMetaDatum = {
|
|
325
|
+
path: normalizePath(relativePathWithFileName),
|
|
326
|
+
type: routeBlock?.attr.type ?? "page"
|
|
327
|
+
};
|
|
324
328
|
if (routeBlock)
|
|
325
329
|
Object.assign(pageMetaDatum, routeBlock.content);
|
|
326
330
|
return pageMetaDatum;
|
|
327
331
|
}
|
|
328
332
|
async parsePages(pages, overrides) {
|
|
329
|
-
const generatedPageMetaData = await Promise.all(
|
|
330
|
-
pages.map(async (page) => await this.parsePage(page))
|
|
331
|
-
);
|
|
333
|
+
const generatedPageMetaData = await Promise.all(pages.map(async (page) => await this.parsePage(page)));
|
|
332
334
|
const customPageMetaData = overrides || [];
|
|
333
335
|
const result = customPageMetaData.length ? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData)) : generatedPageMetaData;
|
|
336
|
+
this.setHomePage(result);
|
|
334
337
|
result.sort((page) => page.type === "home" ? -1 : 0);
|
|
335
338
|
return result;
|
|
336
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;
|
|
361
|
+
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.', {
|
|
362
|
+
timestamp: true
|
|
363
|
+
});
|
|
364
|
+
}
|
|
337
365
|
async mergePageMetaData() {
|
|
338
366
|
const pageMetaData = await this.parsePages(this.pagesPath, this.pagesGlobConfig?.pages);
|
|
339
367
|
this.pageMetaData = pageMetaData;
|
|
@@ -418,9 +446,9 @@ async function restart() {
|
|
|
418
446
|
});
|
|
419
447
|
});
|
|
420
448
|
}
|
|
421
|
-
|
|
449
|
+
function VitePluginUniPages(userOptions = {}) {
|
|
422
450
|
let ctx;
|
|
423
|
-
const resolvedPagesJSONPath = path
|
|
451
|
+
const resolvedPagesJSONPath = path.join(
|
|
424
452
|
process.cwd(),
|
|
425
453
|
userOptions.outDir ?? "src",
|
|
426
454
|
OUTPUT_NAME
|
|
@@ -474,6 +502,6 @@ const VitePluginUniPages = (userOptions = {}) => {
|
|
|
474
502
|
return ctx.virtualModule();
|
|
475
503
|
}
|
|
476
504
|
};
|
|
477
|
-
}
|
|
505
|
+
}
|
|
478
506
|
|
|
479
507
|
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.
|
|
4
|
+
"version": "0.2.0",
|
|
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.8"
|
|
50
49
|
},
|
|
51
50
|
"scripts": {
|
|
52
51
|
"build": "unbuild",
|