vite-plugin-pages2 0.34.10 → 0.34.12
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 +63 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -6
- package/dist/index.d.ts +2 -6
- package/dist/index.js +63 -29
- package/dist/index.js.map +1 -1
- package/package.json +118 -118
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { Logger, ViteDevServer, Plugin } from 'vite';
|
|
2
2
|
import * as debug from 'debug';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Promise, or maybe not
|
|
6
|
-
*/
|
|
7
|
-
type Awaitable<T> = T | PromiseLike<T>;
|
|
8
|
-
|
|
9
4
|
interface PageRoute {
|
|
10
5
|
path: string;
|
|
11
6
|
route: string;
|
|
@@ -78,6 +73,7 @@ interface VueRoute extends Omit<Optional<VueRouteBase, 'rawRoute' | 'name'>, 'ch
|
|
|
78
73
|
}
|
|
79
74
|
declare function vueResolver(): PageResolver;
|
|
80
75
|
|
|
76
|
+
type Awaitable<T> = T | Promise<T>;
|
|
81
77
|
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
|
82
78
|
type ImportMode = 'sync' | 'async';
|
|
83
79
|
type ImportModeResolver = (filepath: string, pluginOptions: ResolvedOptions) => ImportMode;
|
|
@@ -223,4 +219,4 @@ declare const syncIndexResolver: ImportModeResolver;
|
|
|
223
219
|
|
|
224
220
|
declare function pagesPlugin(userOptions?: UserOptions): Plugin;
|
|
225
221
|
|
|
226
|
-
export { type CustomBlock, type ImportMode, type ImportModeResolver, type InternalPageResolvers, type Optional, PageContext, type PageOptions, type PageResolver, type ParsedJSX, type ReactRoute, type ResolvedOptions, type SolidRoute, type UserOptions, type VueRoute, pagesPlugin as default, reactResolver, solidResolver, syncIndexResolver, vueResolver };
|
|
222
|
+
export { type Awaitable, type CustomBlock, type ImportMode, type ImportModeResolver, type InternalPageResolvers, type Optional, PageContext, type PageOptions, type PageResolver, type ParsedJSX, type ReactRoute, type ResolvedOptions, type SolidRoute, type UserOptions, type VueRoute, pagesPlugin as default, reactResolver, solidResolver, syncIndexResolver, vueResolver };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { Logger, ViteDevServer, Plugin } from 'vite';
|
|
2
2
|
import * as debug from 'debug';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Promise, or maybe not
|
|
6
|
-
*/
|
|
7
|
-
type Awaitable<T> = T | PromiseLike<T>;
|
|
8
|
-
|
|
9
4
|
interface PageRoute {
|
|
10
5
|
path: string;
|
|
11
6
|
route: string;
|
|
@@ -78,6 +73,7 @@ interface VueRoute extends Omit<Optional<VueRouteBase, 'rawRoute' | 'name'>, 'ch
|
|
|
78
73
|
}
|
|
79
74
|
declare function vueResolver(): PageResolver;
|
|
80
75
|
|
|
76
|
+
type Awaitable<T> = T | Promise<T>;
|
|
81
77
|
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
|
82
78
|
type ImportMode = 'sync' | 'async';
|
|
83
79
|
type ImportModeResolver = (filepath: string, pluginOptions: ResolvedOptions) => ImportMode;
|
|
@@ -223,4 +219,4 @@ declare const syncIndexResolver: ImportModeResolver;
|
|
|
223
219
|
|
|
224
220
|
declare function pagesPlugin(userOptions?: UserOptions): Plugin;
|
|
225
221
|
|
|
226
|
-
export { type CustomBlock, type ImportMode, type ImportModeResolver, type InternalPageResolvers, type Optional, PageContext, type PageOptions, type PageResolver, type ParsedJSX, type ReactRoute, type ResolvedOptions, type SolidRoute, type UserOptions, type VueRoute, pagesPlugin as default, reactResolver, solidResolver, syncIndexResolver, vueResolver };
|
|
222
|
+
export { type Awaitable, type CustomBlock, type ImportMode, type ImportModeResolver, type InternalPageResolvers, type Optional, PageContext, type PageOptions, type PageResolver, type ParsedJSX, type ReactRoute, type ResolvedOptions, type SolidRoute, type UserOptions, type VueRoute, pagesPlugin as default, reactResolver, solidResolver, syncIndexResolver, vueResolver };
|
package/dist/index.js
CHANGED
|
@@ -36,23 +36,12 @@ var cacheAllRouteRE = /^\[\.{3}(.*)\]$/;
|
|
|
36
36
|
var replaceDynamicRouteRE = /^\[(?:\.{3})?(.*)\]$/;
|
|
37
37
|
var nuxtDynamicRouteRE = /^_(.*)$/;
|
|
38
38
|
var nuxtCacheAllRouteRE = /^_$/;
|
|
39
|
-
var countSlashRE = /\//g;
|
|
40
39
|
var replaceIndexRE = /\/?index$/;
|
|
41
40
|
|
|
42
41
|
// src/context.ts
|
|
43
42
|
import { join as join2, resolve as resolve3 } from "path";
|
|
44
43
|
import process2 from "process";
|
|
45
44
|
|
|
46
|
-
// node_modules/.pnpm/@antfu+utils@9.3.0/node_modules/@antfu/utils/dist/index.mjs
|
|
47
|
-
function toArray(array) {
|
|
48
|
-
array = array != null ? array : [];
|
|
49
|
-
return Array.isArray(array) ? array : [array];
|
|
50
|
-
}
|
|
51
|
-
function slash(str) {
|
|
52
|
-
return str.replace(/\\/g, "/");
|
|
53
|
-
}
|
|
54
|
-
var VOID = Symbol("p-void");
|
|
55
|
-
|
|
56
45
|
// src/files.ts
|
|
57
46
|
import { join } from "path";
|
|
58
47
|
import { globSync } from "tinyglobby";
|
|
@@ -62,6 +51,15 @@ import { resolve, win32 } from "path";
|
|
|
62
51
|
import { URLSearchParams } from "url";
|
|
63
52
|
import Debug from "debug";
|
|
64
53
|
import micromatch from "micromatch";
|
|
54
|
+
var slashRE = /\\/g;
|
|
55
|
+
function slash(str) {
|
|
56
|
+
return str.replace(slashRE, "/");
|
|
57
|
+
}
|
|
58
|
+
function toArray(value) {
|
|
59
|
+
if (value == null)
|
|
60
|
+
return [];
|
|
61
|
+
return Array.isArray(value) ? value : [value];
|
|
62
|
+
}
|
|
65
63
|
var debug = {
|
|
66
64
|
hmr: Debug("vite-plugin-pages:hmr"),
|
|
67
65
|
routeBlock: Debug("vite-plugin-pages:routeBlock"),
|
|
@@ -76,7 +74,12 @@ function extsToGlob(extensions) {
|
|
|
76
74
|
return extensions.length > 1 ? `{${extensions.join(",")}}` : extensions[0] || "";
|
|
77
75
|
}
|
|
78
76
|
function countSlash(value) {
|
|
79
|
-
|
|
77
|
+
let count = 0;
|
|
78
|
+
for (let i = 0; i < value.length; i++) {
|
|
79
|
+
if (value[i] === "/")
|
|
80
|
+
count++;
|
|
81
|
+
}
|
|
82
|
+
return count;
|
|
80
83
|
}
|
|
81
84
|
function isPagesDir(path, options) {
|
|
82
85
|
for (const page of options.dirs) {
|
|
@@ -202,6 +205,7 @@ function parsePageRequest(id) {
|
|
|
202
205
|
}
|
|
203
206
|
|
|
204
207
|
// src/files.ts
|
|
208
|
+
var VUE_MD_EXT_RE = /\/$/;
|
|
205
209
|
function getPageDirs(PageOptions, root, exclude) {
|
|
206
210
|
const dirs = globSync(slash(PageOptions.dir), {
|
|
207
211
|
ignore: exclude,
|
|
@@ -211,7 +215,7 @@ function getPageDirs(PageOptions, root, exclude) {
|
|
|
211
215
|
cwd: root
|
|
212
216
|
});
|
|
213
217
|
const pageDirs = dirs.map((dir) => __spreadProps(__spreadValues({}, PageOptions), {
|
|
214
|
-
dir: dir.replace(
|
|
218
|
+
dir: dir.replace(VUE_MD_EXT_RE, "")
|
|
215
219
|
}));
|
|
216
220
|
return pageDirs;
|
|
217
221
|
}
|
|
@@ -240,9 +244,10 @@ var componentRE = /"(?:component|element)":("(.*?)")/g;
|
|
|
240
244
|
var hasFunctionRE = /"(?:props|beforeEnter)":("(.*?)")/g;
|
|
241
245
|
var multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//g;
|
|
242
246
|
var singlelineCommentsRE = /\/\/.*/g;
|
|
247
|
+
var VUE_MD_EXT_RE2 = /(\s)/g;
|
|
243
248
|
function replaceFunction(_, value) {
|
|
244
|
-
if (typeof value === "function"
|
|
245
|
-
const fnBody = value.toString().replace(multilineCommentsRE, "").replace(singlelineCommentsRE, "").replace(
|
|
249
|
+
if (typeof value === "function") {
|
|
250
|
+
const fnBody = value.toString().replace(multilineCommentsRE, "").replace(singlelineCommentsRE, "").replace(VUE_MD_EXT_RE2, "");
|
|
246
251
|
if (fnBody.length < 8 || fnBody.substring(0, 8) !== "function")
|
|
247
252
|
return `_NuFrRa_${fnBody}`;
|
|
248
253
|
return fnBody;
|
|
@@ -291,11 +296,33 @@ export default routes;`;
|
|
|
291
296
|
}
|
|
292
297
|
|
|
293
298
|
// src/resolvers/react.ts
|
|
299
|
+
var VUE_MD_EXT_RE3 = /^\//;
|
|
300
|
+
function sortReactRoutes(routes) {
|
|
301
|
+
routes.sort((a, b) => {
|
|
302
|
+
const weight = (r) => {
|
|
303
|
+
var _a;
|
|
304
|
+
const p = (_a = r.path) != null ? _a : "";
|
|
305
|
+
if (p === "/")
|
|
306
|
+
return 0;
|
|
307
|
+
if (p === "*")
|
|
308
|
+
return 3;
|
|
309
|
+
if (p.startsWith(":"))
|
|
310
|
+
return 2;
|
|
311
|
+
return 1;
|
|
312
|
+
};
|
|
313
|
+
return weight(a) - weight(b);
|
|
314
|
+
});
|
|
315
|
+
for (const route of routes) {
|
|
316
|
+
if (route.children)
|
|
317
|
+
sortReactRoutes(route.children);
|
|
318
|
+
}
|
|
319
|
+
return routes;
|
|
320
|
+
}
|
|
294
321
|
function prepareRoutes(routes, options, parent) {
|
|
295
322
|
var _a, _b;
|
|
296
323
|
for (const route of routes) {
|
|
297
324
|
if (parent)
|
|
298
|
-
route.path = (_a = route.path) == null ? void 0 : _a.replace(
|
|
325
|
+
route.path = (_a = route.path) == null ? void 0 : _a.replace(VUE_MD_EXT_RE3, "");
|
|
299
326
|
if (route.children)
|
|
300
327
|
route.children = prepareRoutes(route.children, options, route);
|
|
301
328
|
delete route.rawRoute;
|
|
@@ -345,6 +372,7 @@ async function computeReactRoutes(ctx) {
|
|
|
345
372
|
parentRoutes.push(route);
|
|
346
373
|
}
|
|
347
374
|
});
|
|
375
|
+
sortReactRoutes(routes);
|
|
348
376
|
let finalRoutes = prepareRoutes(routes, ctx.options);
|
|
349
377
|
finalRoutes = await ((_b = (_a = ctx.options).onRoutesGenerated) == null ? void 0 : _b.call(_a, finalRoutes)) || finalRoutes;
|
|
350
378
|
return finalRoutes;
|
|
@@ -380,11 +408,12 @@ ${code}`
|
|
|
380
408
|
}
|
|
381
409
|
|
|
382
410
|
// src/resolvers/solid.ts
|
|
411
|
+
var VUE_MD_EXT_RE4 = /^\//;
|
|
383
412
|
function prepareRoutes2(options, routes, parent) {
|
|
384
413
|
var _a, _b;
|
|
385
414
|
for (const route of routes) {
|
|
386
415
|
if (parent)
|
|
387
|
-
route.path = (_a = route.path) == null ? void 0 : _a.replace(
|
|
416
|
+
route.path = (_a = route.path) == null ? void 0 : _a.replace(VUE_MD_EXT_RE4, "");
|
|
388
417
|
if (route.children)
|
|
389
418
|
route.children = prepareRoutes2(options, route.children, route);
|
|
390
419
|
delete route.rawRoute;
|
|
@@ -475,12 +504,13 @@ import { dequal } from "dequal";
|
|
|
475
504
|
import colors from "picocolors";
|
|
476
505
|
|
|
477
506
|
// src/customBlock.ts
|
|
478
|
-
import
|
|
507
|
+
import { readFile } from "fs/promises";
|
|
479
508
|
import extractComments from "extract-comments";
|
|
480
509
|
import JSON5 from "json5";
|
|
481
510
|
import { importModule } from "local-pkg";
|
|
482
511
|
import { parse as YAMLParser } from "yaml";
|
|
483
512
|
var routeJSXReg = /^\s+(route)\s+/gm;
|
|
513
|
+
var _compilerSfc;
|
|
484
514
|
function parseJSX(code) {
|
|
485
515
|
return extractComments(code).slice(0, 1).filter((comment) => routeJSXReg.test(comment.value) && comment.value.includes(":") && comment.loc.start.line === 1);
|
|
486
516
|
}
|
|
@@ -500,7 +530,9 @@ ${err.message}`);
|
|
|
500
530
|
}
|
|
501
531
|
async function parseSFC(code) {
|
|
502
532
|
try {
|
|
503
|
-
|
|
533
|
+
if (!_compilerSfc)
|
|
534
|
+
_compilerSfc = await importModule("@vue/compiler-sfc");
|
|
535
|
+
const { parse } = _compilerSfc;
|
|
504
536
|
return parse(code, {
|
|
505
537
|
pad: "space"
|
|
506
538
|
}).descriptor || parse({
|
|
@@ -538,7 +570,7 @@ ${err.message}`);
|
|
|
538
570
|
}
|
|
539
571
|
}
|
|
540
572
|
async function getRouteBlock(path, options) {
|
|
541
|
-
const content =
|
|
573
|
+
const content = await readFile(path, "utf8");
|
|
542
574
|
const parsedSFC = await parseSFC(content);
|
|
543
575
|
const blockStr = parsedSFC == null ? void 0 : parsedSFC.customBlocks.find((b) => b.type === "route");
|
|
544
576
|
const parsedJSX = parseJSX(content);
|
|
@@ -553,13 +585,15 @@ async function getRouteBlock(path, options) {
|
|
|
553
585
|
}
|
|
554
586
|
|
|
555
587
|
// src/resolvers/vue.ts
|
|
588
|
+
var VUE_MD_EXT_RE5 = /^\//;
|
|
556
589
|
function prepareRoutes3(ctx, routes, parent) {
|
|
557
590
|
var _a, _b, _c, _d;
|
|
591
|
+
const indexSuffixRE = new RegExp(`${ctx.options.routeNameSeparator}index$`);
|
|
558
592
|
for (const route of routes) {
|
|
559
593
|
if (route.name)
|
|
560
|
-
route.name = route.name.replace(
|
|
594
|
+
route.name = route.name.replace(indexSuffixRE, "");
|
|
561
595
|
if (parent)
|
|
562
|
-
route.path = (_a = route.path) == null ? void 0 : _a.replace(
|
|
596
|
+
route.path = (_a = route.path) == null ? void 0 : _a.replace(VUE_MD_EXT_RE5, "");
|
|
563
597
|
if (route.children)
|
|
564
598
|
route.children = prepareRoutes3(ctx, route.children, route);
|
|
565
599
|
if ((_b = route.children) == null ? void 0 : _b.find((c) => c.name === route.name))
|
|
@@ -579,6 +613,7 @@ async function computeVueRoutes(ctx, customBlockMap) {
|
|
|
579
613
|
const { routeStyle, caseSensitive, importPath, routeNameSeparator } = ctx.options;
|
|
580
614
|
const pageRoutes = [...ctx.pageRouteMap.values()].sort((a, b) => countSlash(a.route) - countSlash(b.route));
|
|
581
615
|
const routes = [];
|
|
616
|
+
const routePaths = new Set(pageRoutes.map((p) => p.route));
|
|
582
617
|
pageRoutes.forEach((page) => {
|
|
583
618
|
const pathNodes = page.route.split("/");
|
|
584
619
|
const component = importPath === "relative" ? page.path.replace(ctx.root, "") : page.path;
|
|
@@ -621,10 +656,7 @@ async function computeVueRoutes(ctx, customBlockMap) {
|
|
|
621
656
|
else
|
|
622
657
|
route.path += "(.*)";
|
|
623
658
|
} else if (nuxtStyle && i === pathNodes.length - 1) {
|
|
624
|
-
|
|
625
|
-
return route2 === page.route.replace(pathNodes[i], "index");
|
|
626
|
-
});
|
|
627
|
-
if (!isIndexFound)
|
|
659
|
+
if (!routePaths.has(page.route.replace(pathNodes[i], "index")))
|
|
628
660
|
route.path += "?";
|
|
629
661
|
}
|
|
630
662
|
} else {
|
|
@@ -698,12 +730,14 @@ function vueResolver() {
|
|
|
698
730
|
}
|
|
699
731
|
|
|
700
732
|
// src/options.ts
|
|
733
|
+
var VUE_MD_EXT_RE6 = /^\//;
|
|
734
|
+
var VUE_MD_EXT_RE1 = /\/$/;
|
|
701
735
|
function resolvePageDirs(dirs, root, exclude) {
|
|
702
736
|
dirs = toArray(dirs);
|
|
703
737
|
return dirs.flatMap((dir) => {
|
|
704
738
|
const option = typeof dir === "string" ? { dir, baseRoute: "" } : dir;
|
|
705
739
|
option.dir = slash(resolve2(root, option.dir)).replace(`${root}/`, "");
|
|
706
|
-
option.baseRoute = option.baseRoute.replace(
|
|
740
|
+
option.baseRoute = option.baseRoute.replace(VUE_MD_EXT_RE6, "").replace(VUE_MD_EXT_RE1, "");
|
|
707
741
|
return getPageDirs(option, root, exclude);
|
|
708
742
|
});
|
|
709
743
|
}
|
|
@@ -882,10 +916,10 @@ function pagesPlugin(userOptions = {}) {
|
|
|
882
916
|
name: "vite-plugin-pages",
|
|
883
917
|
enforce: "pre",
|
|
884
918
|
async configResolved(config) {
|
|
885
|
-
if (!userOptions.resolver && config.plugins.
|
|
919
|
+
if (!userOptions.resolver && config.plugins.some((i) => i.name.includes("vite:react"))) {
|
|
886
920
|
userOptions.resolver = "react";
|
|
887
921
|
}
|
|
888
|
-
if (!userOptions.resolver && config.plugins.
|
|
922
|
+
if (!userOptions.resolver && config.plugins.some((i) => i.name.includes("solid"))) {
|
|
889
923
|
userOptions.resolver = "solid";
|
|
890
924
|
}
|
|
891
925
|
ctx = new PageContext(userOptions, config.root);
|