vite-plugin-pages2 0.32.5
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/LICENSE +21 -0
- package/README.md +660 -0
- package/client-react.d.ts +13 -0
- package/client-solid.d.ts +6 -0
- package/client.d.ts +20 -0
- package/dist/index.cjs +974 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +252 -0
- package/dist/index.d.ts +252 -0
- package/dist/index.js +936 -0
- package/dist/index.js.map +1 -0
- package/package.json +124 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,936 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/constants.ts
|
|
22
|
+
var MODULE_IDS = [
|
|
23
|
+
"~pages",
|
|
24
|
+
"~react-pages",
|
|
25
|
+
"~solid-pages",
|
|
26
|
+
"pages-generated",
|
|
27
|
+
"virtual:generated-pages",
|
|
28
|
+
"virtual:generated-pages-react"
|
|
29
|
+
];
|
|
30
|
+
var MODULE_ID_VIRTUAL = "virtual:vite-plugin-pages/generated-pages";
|
|
31
|
+
var ROUTE_BLOCK_ID_VIRTUAL = "virtual:vite-plugin-pages/route-block";
|
|
32
|
+
var ROUTE_IMPORT_NAME = "__pages_import_$1__";
|
|
33
|
+
var routeBlockQueryRE = /\?vue&type=route/;
|
|
34
|
+
var dynamicRouteRE = /^\[(.+)\]$/;
|
|
35
|
+
var cacheAllRouteRE = /^\[\.{3}(.*)\]$/;
|
|
36
|
+
var replaceDynamicRouteRE = /^\[(?:\.{3})?(.*)\]$/;
|
|
37
|
+
var nuxtDynamicRouteRE = /^_(.*)$/;
|
|
38
|
+
var nuxtCacheAllRouteRE = /^_$/;
|
|
39
|
+
var countSlashRE = /\//g;
|
|
40
|
+
var replaceIndexRE = /\/?index$/;
|
|
41
|
+
|
|
42
|
+
// src/context.ts
|
|
43
|
+
import { join as join2, resolve as resolve3 } from "node:path";
|
|
44
|
+
import process2 from "node:process";
|
|
45
|
+
|
|
46
|
+
// node_modules/.pnpm/@antfu+utils@8.1.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
|
+
var VOID = Symbol("p-void");
|
|
52
|
+
function slash(str) {
|
|
53
|
+
return str.replace(/\\/g, "/");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/files.ts
|
|
57
|
+
import { join } from "node:path";
|
|
58
|
+
import fg from "fast-glob";
|
|
59
|
+
|
|
60
|
+
// src/utils.ts
|
|
61
|
+
import { resolve, win32 } from "node:path";
|
|
62
|
+
import { URLSearchParams } from "node:url";
|
|
63
|
+
import Debug from "debug";
|
|
64
|
+
var debug = {
|
|
65
|
+
hmr: Debug("vite-plugin-pages:hmr"),
|
|
66
|
+
routeBlock: Debug("vite-plugin-pages:routeBlock"),
|
|
67
|
+
options: Debug("vite-plugin-pages:options"),
|
|
68
|
+
pages: Debug("vite-plugin-pages:pages"),
|
|
69
|
+
search: Debug("vite-plugin-pages:search"),
|
|
70
|
+
env: Debug("vite-plugin-pages:env"),
|
|
71
|
+
cache: Debug("vite-plugin-pages:cache"),
|
|
72
|
+
resolver: Debug("vite-plugin-pages:resolver")
|
|
73
|
+
};
|
|
74
|
+
function extsToGlob(extensions) {
|
|
75
|
+
return extensions.length > 1 ? `{${extensions.join(",")}}` : extensions[0] || "";
|
|
76
|
+
}
|
|
77
|
+
function countSlash(value) {
|
|
78
|
+
return (value.match(countSlashRE) || []).length;
|
|
79
|
+
}
|
|
80
|
+
function isPagesDir(path, options) {
|
|
81
|
+
for (const page of options.dirs) {
|
|
82
|
+
const dirPath = slash(resolve(options.root, page.dir));
|
|
83
|
+
if (path.startsWith(dirPath))
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
function isTarget(path, options) {
|
|
89
|
+
return isPagesDir(path, options) && options.extensionsRE.test(path);
|
|
90
|
+
}
|
|
91
|
+
function isDynamicRoute(routePath, nuxtStyle = false) {
|
|
92
|
+
return nuxtStyle ? nuxtDynamicRouteRE.test(routePath) : dynamicRouteRE.test(routePath);
|
|
93
|
+
}
|
|
94
|
+
function isCatchAllRoute(routePath, nuxtStyle = false) {
|
|
95
|
+
return nuxtStyle ? nuxtCacheAllRouteRE.test(routePath) : cacheAllRouteRE.test(routePath);
|
|
96
|
+
}
|
|
97
|
+
function resolveImportMode(filepath, options) {
|
|
98
|
+
const mode = options.importMode;
|
|
99
|
+
if (typeof mode === "function")
|
|
100
|
+
return mode(filepath, options);
|
|
101
|
+
return mode;
|
|
102
|
+
}
|
|
103
|
+
function invalidatePagesModule(server) {
|
|
104
|
+
const { moduleGraph } = server;
|
|
105
|
+
const mods = moduleGraph.getModulesByFile(MODULE_ID_VIRTUAL);
|
|
106
|
+
if (mods) {
|
|
107
|
+
const seen = /* @__PURE__ */ new Set();
|
|
108
|
+
mods.forEach((mod) => {
|
|
109
|
+
moduleGraph.invalidateModule(mod, seen);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function normalizeCase(str, caseSensitive) {
|
|
114
|
+
if (!caseSensitive)
|
|
115
|
+
return str.toLocaleLowerCase();
|
|
116
|
+
return str;
|
|
117
|
+
}
|
|
118
|
+
function normalizeName(name, isDynamic, nuxtStyle = false) {
|
|
119
|
+
if (!isDynamic)
|
|
120
|
+
return name;
|
|
121
|
+
return nuxtStyle ? name.replace(nuxtDynamicRouteRE, "$1") || "all" : name.replace(replaceDynamicRouteRE, "$1");
|
|
122
|
+
}
|
|
123
|
+
function buildReactRoutePath(node, nuxtStyle = false) {
|
|
124
|
+
const isDynamic = isDynamicRoute(node, nuxtStyle);
|
|
125
|
+
const isCatchAll = isCatchAllRoute(node, nuxtStyle);
|
|
126
|
+
const normalizedName = normalizeName(node, isDynamic, nuxtStyle);
|
|
127
|
+
if (isDynamic) {
|
|
128
|
+
if (isCatchAll)
|
|
129
|
+
return "*";
|
|
130
|
+
return `:${normalizedName}`;
|
|
131
|
+
}
|
|
132
|
+
return `${normalizedName}`;
|
|
133
|
+
}
|
|
134
|
+
function buildReactRemixRoutePath(node) {
|
|
135
|
+
const escapeStart = "[";
|
|
136
|
+
const escapeEnd = "]";
|
|
137
|
+
let result = "";
|
|
138
|
+
let rawSegmentBuffer = "";
|
|
139
|
+
let inEscapeSequence = 0;
|
|
140
|
+
let skipSegment = false;
|
|
141
|
+
for (let i = 0; i < node.length; i++) {
|
|
142
|
+
let isNewEscapeSequence2 = function() {
|
|
143
|
+
return !inEscapeSequence && char === escapeStart && lastChar !== escapeStart;
|
|
144
|
+
}, isCloseEscapeSequence2 = function() {
|
|
145
|
+
return inEscapeSequence && char === escapeEnd && nextChar !== escapeEnd;
|
|
146
|
+
}, isStartOfLayoutSegment2 = function() {
|
|
147
|
+
return char === "_" && nextChar === "_" && !rawSegmentBuffer;
|
|
148
|
+
};
|
|
149
|
+
var isNewEscapeSequence = isNewEscapeSequence2, isCloseEscapeSequence = isCloseEscapeSequence2, isStartOfLayoutSegment = isStartOfLayoutSegment2;
|
|
150
|
+
const char = node.charAt(i);
|
|
151
|
+
const lastChar = i > 0 ? node.charAt(i - 1) : void 0;
|
|
152
|
+
const nextChar = i < node.length - 1 ? node.charAt(i + 1) : void 0;
|
|
153
|
+
if (skipSegment) {
|
|
154
|
+
if (char === "/" || char === "." || char === win32.sep)
|
|
155
|
+
skipSegment = false;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (isNewEscapeSequence2()) {
|
|
159
|
+
inEscapeSequence++;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (isCloseEscapeSequence2()) {
|
|
163
|
+
inEscapeSequence--;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (inEscapeSequence) {
|
|
167
|
+
result += char;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (char === "/" || char === win32.sep || char === ".") {
|
|
171
|
+
if (rawSegmentBuffer === "index" && result.endsWith("index"))
|
|
172
|
+
result = result.replace(replaceIndexRE, "");
|
|
173
|
+
else result += "/";
|
|
174
|
+
rawSegmentBuffer = "";
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (isStartOfLayoutSegment2()) {
|
|
178
|
+
skipSegment = true;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
rawSegmentBuffer += char;
|
|
182
|
+
if (char === "$") {
|
|
183
|
+
result += typeof nextChar === "undefined" ? "*" : ":";
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
result += char;
|
|
187
|
+
}
|
|
188
|
+
if (rawSegmentBuffer === "index" && result.endsWith("index"))
|
|
189
|
+
result = result.replace(replaceIndexRE, "");
|
|
190
|
+
return result || void 0;
|
|
191
|
+
}
|
|
192
|
+
function parsePageRequest(id) {
|
|
193
|
+
const [moduleId, rawQuery] = id.split("?", 2);
|
|
194
|
+
const query = new URLSearchParams(rawQuery);
|
|
195
|
+
const pageId = query.get("id");
|
|
196
|
+
return {
|
|
197
|
+
moduleId,
|
|
198
|
+
query,
|
|
199
|
+
pageId
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/files.ts
|
|
204
|
+
function getPageDirs(PageOptions, root, exclude) {
|
|
205
|
+
const dirs = fg.sync(slash(PageOptions.dir), {
|
|
206
|
+
ignore: exclude,
|
|
207
|
+
onlyDirectories: true,
|
|
208
|
+
dot: true,
|
|
209
|
+
unique: true,
|
|
210
|
+
cwd: root
|
|
211
|
+
});
|
|
212
|
+
const pageDirs = dirs.map((dir) => __spreadProps(__spreadValues({}, PageOptions), {
|
|
213
|
+
dir
|
|
214
|
+
}));
|
|
215
|
+
return pageDirs;
|
|
216
|
+
}
|
|
217
|
+
function getPageFiles(path, options, pageOptions) {
|
|
218
|
+
var _a;
|
|
219
|
+
const {
|
|
220
|
+
exclude,
|
|
221
|
+
extensions
|
|
222
|
+
} = options;
|
|
223
|
+
const ext = extsToGlob(extensions);
|
|
224
|
+
const pattern = (_a = (pageOptions == null ? void 0 : pageOptions.filePatern) || (pageOptions == null ? void 0 : pageOptions.filePattern)) != null ? _a : `**/*.${ext}`;
|
|
225
|
+
const files = fg.sync(pattern, {
|
|
226
|
+
ignore: exclude,
|
|
227
|
+
onlyFiles: true,
|
|
228
|
+
cwd: path
|
|
229
|
+
}).map((p) => slash(join(path, p)));
|
|
230
|
+
return files;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/options.ts
|
|
234
|
+
import { resolve as resolve2 } from "node:path";
|
|
235
|
+
import process from "node:process";
|
|
236
|
+
|
|
237
|
+
// src/stringify.ts
|
|
238
|
+
var componentRE = /"(?:component|element)":("(.*?)")/g;
|
|
239
|
+
var hasFunctionRE = /"(?:props|beforeEnter)":("(.*?)")/g;
|
|
240
|
+
var multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//g;
|
|
241
|
+
var singlelineCommentsRE = /\/\/.*/g;
|
|
242
|
+
function replaceFunction(_, value) {
|
|
243
|
+
if (value instanceof Function || typeof value === "function") {
|
|
244
|
+
const fnBody = value.toString().replace(multilineCommentsRE, "").replace(singlelineCommentsRE, "").replace(/(\s)/g, "");
|
|
245
|
+
if (fnBody.length < 8 || fnBody.substring(0, 8) !== "function")
|
|
246
|
+
return `_NuFrRa_${fnBody}`;
|
|
247
|
+
return fnBody;
|
|
248
|
+
}
|
|
249
|
+
return value;
|
|
250
|
+
}
|
|
251
|
+
function stringifyRoutes(preparedRoutes, options) {
|
|
252
|
+
const importsMap = /* @__PURE__ */ new Map();
|
|
253
|
+
function getImportString(path, importName) {
|
|
254
|
+
var _a, _b;
|
|
255
|
+
const mode = resolveImportMode(path, options);
|
|
256
|
+
return mode === "sync" ? `import ${importName} from "${path}"` : `const ${importName} = ${((_b = (_a = options.resolver.stringify) == null ? void 0 : _a.dynamicImport) == null ? void 0 : _b.call(_a, path)) || `() => import("${path}")`}`;
|
|
257
|
+
}
|
|
258
|
+
function componentReplacer(str, replaceStr, path) {
|
|
259
|
+
var _a, _b;
|
|
260
|
+
let importName = importsMap.get(path);
|
|
261
|
+
if (!importName)
|
|
262
|
+
importName = ROUTE_IMPORT_NAME.replace("$1", `${importsMap.size}`);
|
|
263
|
+
importsMap.set(path, importName);
|
|
264
|
+
importName = ((_b = (_a = options.resolver.stringify) == null ? void 0 : _a.component) == null ? void 0 : _b.call(_a, importName)) || importName;
|
|
265
|
+
return str.replace(replaceStr, importName);
|
|
266
|
+
}
|
|
267
|
+
function functionReplacer(str, replaceStr, content) {
|
|
268
|
+
if (content.startsWith("function"))
|
|
269
|
+
return str.replace(replaceStr, content);
|
|
270
|
+
if (content.startsWith("_NuFrRa_"))
|
|
271
|
+
return str.replace(replaceStr, content.slice(8));
|
|
272
|
+
return str;
|
|
273
|
+
}
|
|
274
|
+
const stringRoutes = JSON.stringify(preparedRoutes, replaceFunction).replace(componentRE, componentReplacer).replace(hasFunctionRE, functionReplacer);
|
|
275
|
+
const imports = Array.from(importsMap).map((args) => getImportString(...args));
|
|
276
|
+
return {
|
|
277
|
+
imports,
|
|
278
|
+
stringRoutes
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
function generateClientCode(routes, options) {
|
|
282
|
+
var _a, _b;
|
|
283
|
+
const { imports, stringRoutes } = stringifyRoutes(routes, options);
|
|
284
|
+
const code = `${imports.join(";\n")};
|
|
285
|
+
|
|
286
|
+
const routes = ${stringRoutes};
|
|
287
|
+
|
|
288
|
+
export default routes;`;
|
|
289
|
+
return ((_b = (_a = options.resolver.stringify) == null ? void 0 : _a.final) == null ? void 0 : _b.call(_a, code)) || code;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// src/resolvers/react.ts
|
|
293
|
+
function prepareRoutes(routes, options, parent) {
|
|
294
|
+
var _a, _b;
|
|
295
|
+
for (const route of routes) {
|
|
296
|
+
if (parent)
|
|
297
|
+
route.path = (_a = route.path) == null ? void 0 : _a.replace(/^\//, "");
|
|
298
|
+
if (route.children)
|
|
299
|
+
route.children = prepareRoutes(route.children, options, route);
|
|
300
|
+
delete route.rawRoute;
|
|
301
|
+
Object.assign(route, ((_b = options.extendRoute) == null ? void 0 : _b.call(options, route, parent)) || {});
|
|
302
|
+
}
|
|
303
|
+
return routes;
|
|
304
|
+
}
|
|
305
|
+
async function computeReactRoutes(ctx) {
|
|
306
|
+
var _a, _b;
|
|
307
|
+
const { routeStyle, caseSensitive, importPath } = ctx.options;
|
|
308
|
+
const nuxtStyle = routeStyle === "nuxt";
|
|
309
|
+
const pageRoutes = [...ctx.pageRouteMap.values()].sort((a, b) => countSlash(a.route) - countSlash(b.route));
|
|
310
|
+
const routes = [];
|
|
311
|
+
pageRoutes.forEach((page) => {
|
|
312
|
+
const pathNodes = page.route.split("/");
|
|
313
|
+
const element = importPath === "relative" ? page.path.replace(ctx.root, "") : page.path;
|
|
314
|
+
let parentRoutes = routes;
|
|
315
|
+
for (let i = 0; i < pathNodes.length; i++) {
|
|
316
|
+
const node = pathNodes[i];
|
|
317
|
+
const route = {
|
|
318
|
+
caseSensitive,
|
|
319
|
+
path: "",
|
|
320
|
+
rawRoute: pathNodes.slice(0, i + 1).join("/")
|
|
321
|
+
};
|
|
322
|
+
if (i === pathNodes.length - 1)
|
|
323
|
+
route.element = element;
|
|
324
|
+
const isIndexRoute = normalizeCase(node, caseSensitive).endsWith("index");
|
|
325
|
+
if (!route.path && isIndexRoute) {
|
|
326
|
+
route.path = "/";
|
|
327
|
+
} else if (!isIndexRoute) {
|
|
328
|
+
if (routeStyle === "remix")
|
|
329
|
+
route.path = buildReactRemixRoutePath(node);
|
|
330
|
+
else
|
|
331
|
+
route.path = buildReactRoutePath(node, nuxtStyle);
|
|
332
|
+
}
|
|
333
|
+
const parent = parentRoutes.find((parent2) => {
|
|
334
|
+
return pathNodes.slice(0, i).join("/") === parent2.rawRoute;
|
|
335
|
+
});
|
|
336
|
+
if (parent) {
|
|
337
|
+
parent.children = parent.children || [];
|
|
338
|
+
parentRoutes = parent.children;
|
|
339
|
+
}
|
|
340
|
+
const exits = parentRoutes.some((parent2) => {
|
|
341
|
+
return pathNodes.slice(0, i + 1).join("/") === parent2.rawRoute;
|
|
342
|
+
});
|
|
343
|
+
if (!exits)
|
|
344
|
+
parentRoutes.push(route);
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
let finalRoutes = prepareRoutes(routes, ctx.options);
|
|
348
|
+
finalRoutes = await ((_b = (_a = ctx.options).onRoutesGenerated) == null ? void 0 : _b.call(_a, finalRoutes)) || finalRoutes;
|
|
349
|
+
return finalRoutes;
|
|
350
|
+
}
|
|
351
|
+
async function resolveReactRoutes(ctx) {
|
|
352
|
+
var _a, _b;
|
|
353
|
+
const finalRoutes = await computeReactRoutes(ctx);
|
|
354
|
+
let client = generateClientCode(finalRoutes, ctx.options);
|
|
355
|
+
client = await ((_b = (_a = ctx.options).onClientGenerated) == null ? void 0 : _b.call(_a, client)) || client;
|
|
356
|
+
return client;
|
|
357
|
+
}
|
|
358
|
+
function reactResolver() {
|
|
359
|
+
return {
|
|
360
|
+
resolveModuleIds() {
|
|
361
|
+
return ["~react-pages", "virtual:generated-pages-react"];
|
|
362
|
+
},
|
|
363
|
+
resolveExtensions() {
|
|
364
|
+
return ["tsx", "jsx", "ts", "js"];
|
|
365
|
+
},
|
|
366
|
+
async resolveRoutes(ctx) {
|
|
367
|
+
return resolveReactRoutes(ctx);
|
|
368
|
+
},
|
|
369
|
+
async getComputedRoutes(ctx) {
|
|
370
|
+
return computeReactRoutes(ctx);
|
|
371
|
+
},
|
|
372
|
+
stringify: {
|
|
373
|
+
component: (path) => `React.createElement(${path})`,
|
|
374
|
+
dynamicImport: (path) => `React.lazy(() => import("${path}"))`,
|
|
375
|
+
final: (code) => `import React from "react";
|
|
376
|
+
${code}`
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/resolvers/solid.ts
|
|
382
|
+
function prepareRoutes2(options, routes, parent) {
|
|
383
|
+
var _a, _b;
|
|
384
|
+
for (const route of routes) {
|
|
385
|
+
if (parent)
|
|
386
|
+
route.path = (_a = route.path) == null ? void 0 : _a.replace(/^\//, "");
|
|
387
|
+
if (route.children)
|
|
388
|
+
route.children = prepareRoutes2(options, route.children, route);
|
|
389
|
+
delete route.rawRoute;
|
|
390
|
+
Object.assign(route, ((_b = options.extendRoute) == null ? void 0 : _b.call(options, route, parent)) || {});
|
|
391
|
+
}
|
|
392
|
+
return routes;
|
|
393
|
+
}
|
|
394
|
+
async function computeSolidRoutes(ctx) {
|
|
395
|
+
var _a, _b;
|
|
396
|
+
const { routeStyle, caseSensitive, importPath } = ctx.options;
|
|
397
|
+
const nuxtStyle = routeStyle === "nuxt";
|
|
398
|
+
const pageRoutes = [...ctx.pageRouteMap.values()].sort((a, b) => countSlash(a.route) - countSlash(b.route));
|
|
399
|
+
const routes = [];
|
|
400
|
+
pageRoutes.forEach((page) => {
|
|
401
|
+
const pathNodes = page.route.split("/");
|
|
402
|
+
const component = importPath === "relative" ? page.path.replace(ctx.root, "") : page.path;
|
|
403
|
+
const element = importPath === "relative" ? page.path.replace(ctx.root, "") : page.path;
|
|
404
|
+
let parentRoutes = routes;
|
|
405
|
+
for (let i = 0; i < pathNodes.length; i++) {
|
|
406
|
+
const node = pathNodes[i];
|
|
407
|
+
const normalizedPath = normalizeCase(node, caseSensitive);
|
|
408
|
+
const route = {
|
|
409
|
+
path: "",
|
|
410
|
+
rawRoute: pathNodes.slice(0, i + 1).join("/")
|
|
411
|
+
};
|
|
412
|
+
const parent = parentRoutes.find(
|
|
413
|
+
(parent2) => pathNodes.slice(0, i).join("/") === parent2.rawRoute
|
|
414
|
+
);
|
|
415
|
+
if (parent) {
|
|
416
|
+
parent.children = parent.children || [];
|
|
417
|
+
parentRoutes = parent.children;
|
|
418
|
+
}
|
|
419
|
+
if (i === pathNodes.length - 1) {
|
|
420
|
+
route.element = element;
|
|
421
|
+
route.component = component;
|
|
422
|
+
}
|
|
423
|
+
if (normalizedPath === "index") {
|
|
424
|
+
if (!route.path)
|
|
425
|
+
route.path = "/";
|
|
426
|
+
} else if (normalizedPath !== "index") {
|
|
427
|
+
if (routeStyle === "remix")
|
|
428
|
+
route.path = buildReactRemixRoutePath(node) || "";
|
|
429
|
+
else
|
|
430
|
+
route.path = buildReactRoutePath(node, nuxtStyle) || "";
|
|
431
|
+
}
|
|
432
|
+
const exist = parentRoutes.some((parent2) => {
|
|
433
|
+
return pathNodes.slice(0, i + 1).join("/") === parent2.rawRoute;
|
|
434
|
+
});
|
|
435
|
+
if (!exist)
|
|
436
|
+
parentRoutes.push(route);
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
let finalRoutes = prepareRoutes2(ctx.options, routes);
|
|
440
|
+
finalRoutes = await ((_b = (_a = ctx.options).onRoutesGenerated) == null ? void 0 : _b.call(_a, finalRoutes)) || finalRoutes;
|
|
441
|
+
return finalRoutes;
|
|
442
|
+
}
|
|
443
|
+
async function resolveSolidRoutes(ctx) {
|
|
444
|
+
var _a, _b;
|
|
445
|
+
const finalRoutes = await computeSolidRoutes(ctx);
|
|
446
|
+
let client = generateClientCode(finalRoutes, ctx.options);
|
|
447
|
+
client = await ((_b = (_a = ctx.options).onClientGenerated) == null ? void 0 : _b.call(_a, client)) || client;
|
|
448
|
+
return client;
|
|
449
|
+
}
|
|
450
|
+
function solidResolver() {
|
|
451
|
+
return {
|
|
452
|
+
resolveModuleIds() {
|
|
453
|
+
return ["~solid-pages"];
|
|
454
|
+
},
|
|
455
|
+
resolveExtensions() {
|
|
456
|
+
return ["tsx", "jsx", "ts", "js"];
|
|
457
|
+
},
|
|
458
|
+
async resolveRoutes(ctx) {
|
|
459
|
+
return resolveSolidRoutes(ctx);
|
|
460
|
+
},
|
|
461
|
+
async getComputedRoutes(ctx) {
|
|
462
|
+
return computeSolidRoutes(ctx);
|
|
463
|
+
},
|
|
464
|
+
stringify: {
|
|
465
|
+
dynamicImport: (path) => `Solid.lazy(() => import("${path}"))`,
|
|
466
|
+
final: (code) => `import * as Solid from "solid-js";
|
|
467
|
+
${code}`
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// src/resolvers/vue.ts
|
|
473
|
+
import { dequal } from "dequal";
|
|
474
|
+
import colors from "picocolors";
|
|
475
|
+
|
|
476
|
+
// src/customBlock.ts
|
|
477
|
+
import fs from "node:fs";
|
|
478
|
+
import extractComments from "extract-comments";
|
|
479
|
+
import JSON5 from "json5";
|
|
480
|
+
import { importModule } from "local-pkg";
|
|
481
|
+
import { parse as YAMLParser } from "yaml";
|
|
482
|
+
var routeJSXReg = /^\s+(route)\s+/gm;
|
|
483
|
+
function parseJSX(code) {
|
|
484
|
+
return extractComments(code).slice(0, 1).filter((comment) => routeJSXReg.test(comment.value) && comment.value.includes(":") && comment.loc.start.line === 1);
|
|
485
|
+
}
|
|
486
|
+
function parseYamlComment(code, path) {
|
|
487
|
+
return code.reduce((memo, item) => {
|
|
488
|
+
const { value } = item;
|
|
489
|
+
const v = value.replace(routeJSXReg, "");
|
|
490
|
+
debug.routeBlock(`use ${v} parser`);
|
|
491
|
+
try {
|
|
492
|
+
const yamlResult = YAMLParser(v);
|
|
493
|
+
return __spreadValues(__spreadValues({}, memo), yamlResult);
|
|
494
|
+
} catch (err) {
|
|
495
|
+
throw new Error(`Invalid YAML format of comment in ${path}
|
|
496
|
+
${err.message}`);
|
|
497
|
+
}
|
|
498
|
+
}, {});
|
|
499
|
+
}
|
|
500
|
+
async function parseSFC(code) {
|
|
501
|
+
try {
|
|
502
|
+
const { parse } = await importModule("@vue/compiler-sfc");
|
|
503
|
+
return parse(code, {
|
|
504
|
+
pad: "space"
|
|
505
|
+
}).descriptor || parse({
|
|
506
|
+
source: code
|
|
507
|
+
});
|
|
508
|
+
} catch (e) {
|
|
509
|
+
throw new Error(`[vite-plugin-pages] Vue3's "@vue/compiler-sfc" is required.`);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
function parseCustomBlock(block, filePath, options) {
|
|
513
|
+
var _a;
|
|
514
|
+
const lang = (_a = block.lang) != null ? _a : options.routeBlockLang;
|
|
515
|
+
debug.routeBlock(`use ${lang} parser`);
|
|
516
|
+
if (lang === "json5") {
|
|
517
|
+
try {
|
|
518
|
+
return JSON5.parse(block.content);
|
|
519
|
+
} catch (err) {
|
|
520
|
+
throw new Error(`Invalid JSON5 format of <${block.type}> content in ${filePath}
|
|
521
|
+
${err.message}`);
|
|
522
|
+
}
|
|
523
|
+
} else if (lang === "json") {
|
|
524
|
+
try {
|
|
525
|
+
return JSON.parse(block.content);
|
|
526
|
+
} catch (err) {
|
|
527
|
+
throw new Error(`Invalid JSON format of <${block.type}> content in ${filePath}
|
|
528
|
+
${err.message}`);
|
|
529
|
+
}
|
|
530
|
+
} else if (lang === "yaml" || lang === "yml") {
|
|
531
|
+
try {
|
|
532
|
+
return YAMLParser(block.content);
|
|
533
|
+
} catch (err) {
|
|
534
|
+
throw new Error(`Invalid YAML format of <${block.type}> content in ${filePath}
|
|
535
|
+
${err.message}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
async function getRouteBlock(path, options) {
|
|
540
|
+
const content = fs.readFileSync(path, "utf8");
|
|
541
|
+
const parsedSFC = await parseSFC(content);
|
|
542
|
+
const blockStr = parsedSFC == null ? void 0 : parsedSFC.customBlocks.find((b) => b.type === "route");
|
|
543
|
+
const parsedJSX = parseJSX(content);
|
|
544
|
+
if (!blockStr && parsedJSX.length === 0)
|
|
545
|
+
return;
|
|
546
|
+
let result;
|
|
547
|
+
if (blockStr)
|
|
548
|
+
result = parseCustomBlock(blockStr, path, options);
|
|
549
|
+
if (parsedJSX.length > 0)
|
|
550
|
+
result = parseYamlComment(parsedJSX, path);
|
|
551
|
+
return result;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// src/resolvers/vue.ts
|
|
555
|
+
function prepareRoutes3(ctx, routes, parent) {
|
|
556
|
+
var _a, _b, _c, _d;
|
|
557
|
+
for (const route of routes) {
|
|
558
|
+
if (route.name)
|
|
559
|
+
route.name = route.name.replace(new RegExp(`${ctx.options.routeNameSeparator}index$`), "");
|
|
560
|
+
if (parent)
|
|
561
|
+
route.path = (_a = route.path) == null ? void 0 : _a.replace(/^\//, "");
|
|
562
|
+
if (route.children)
|
|
563
|
+
route.children = prepareRoutes3(ctx, route.children, route);
|
|
564
|
+
if ((_b = route.children) == null ? void 0 : _b.find((c) => c.name === route.name))
|
|
565
|
+
delete route.name;
|
|
566
|
+
route.props = true;
|
|
567
|
+
delete route.rawRoute;
|
|
568
|
+
if (route.customBlock) {
|
|
569
|
+
Object.assign(route, route.customBlock || {});
|
|
570
|
+
delete route.customBlock;
|
|
571
|
+
}
|
|
572
|
+
Object.assign(route, ((_d = (_c = ctx.options).extendRoute) == null ? void 0 : _d.call(_c, route, parent)) || {});
|
|
573
|
+
}
|
|
574
|
+
return routes;
|
|
575
|
+
}
|
|
576
|
+
async function computeVueRoutes(ctx, customBlockMap) {
|
|
577
|
+
var _a, _b;
|
|
578
|
+
const { routeStyle, caseSensitive, importPath, routeNameSeparator } = ctx.options;
|
|
579
|
+
const pageRoutes = [...ctx.pageRouteMap.values()].sort((a, b) => countSlash(a.route) - countSlash(b.route));
|
|
580
|
+
const routes = [];
|
|
581
|
+
pageRoutes.forEach((page) => {
|
|
582
|
+
const pathNodes = page.route.split("/");
|
|
583
|
+
const component = importPath === "relative" ? page.path.replace(ctx.root, "") : page.path;
|
|
584
|
+
const customBlock = customBlockMap.get(page.path);
|
|
585
|
+
const route = {
|
|
586
|
+
name: "",
|
|
587
|
+
path: "",
|
|
588
|
+
component,
|
|
589
|
+
customBlock,
|
|
590
|
+
rawRoute: page.route
|
|
591
|
+
};
|
|
592
|
+
let parentRoutes = routes;
|
|
593
|
+
let dynamicRoute = false;
|
|
594
|
+
for (let i = 0; i < pathNodes.length; i++) {
|
|
595
|
+
const node = pathNodes[i];
|
|
596
|
+
const nuxtStyle = routeStyle === "nuxt";
|
|
597
|
+
const isDynamic = isDynamicRoute(node, nuxtStyle);
|
|
598
|
+
const isCatchAll = isCatchAllRoute(node, nuxtStyle);
|
|
599
|
+
const normalizedName = normalizeName(node, isDynamic, nuxtStyle);
|
|
600
|
+
const normalizedPath = normalizeCase(normalizedName, caseSensitive);
|
|
601
|
+
if (isDynamic)
|
|
602
|
+
dynamicRoute = true;
|
|
603
|
+
route.name += route.name ? `${routeNameSeparator}${normalizedName}` : normalizedName;
|
|
604
|
+
const parent = parentRoutes.find((parent2) => {
|
|
605
|
+
return pathNodes.slice(0, i + 1).join("/") === parent2.rawRoute;
|
|
606
|
+
});
|
|
607
|
+
if (parent) {
|
|
608
|
+
parent.children = parent.children || [];
|
|
609
|
+
parentRoutes = parent.children;
|
|
610
|
+
route.path = "";
|
|
611
|
+
} else if (normalizedPath === "index") {
|
|
612
|
+
if (!route.path)
|
|
613
|
+
route.path = "/";
|
|
614
|
+
} else if (normalizedPath !== "index") {
|
|
615
|
+
if (isDynamic) {
|
|
616
|
+
route.path += `/:${normalizedName}`;
|
|
617
|
+
if (isCatchAll) {
|
|
618
|
+
if (i === 0)
|
|
619
|
+
route.path += "(.*)*";
|
|
620
|
+
else
|
|
621
|
+
route.path += "(.*)";
|
|
622
|
+
} else if (nuxtStyle && i === pathNodes.length - 1) {
|
|
623
|
+
const isIndexFound = pageRoutes.find(({ route: route2 }) => {
|
|
624
|
+
return route2 === page.route.replace(pathNodes[i], "index");
|
|
625
|
+
});
|
|
626
|
+
if (!isIndexFound)
|
|
627
|
+
route.path += "?";
|
|
628
|
+
}
|
|
629
|
+
} else {
|
|
630
|
+
route.path += `/${normalizedPath}`;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (dynamicRoute)
|
|
635
|
+
parentRoutes.push(route);
|
|
636
|
+
else
|
|
637
|
+
parentRoutes.unshift(route);
|
|
638
|
+
});
|
|
639
|
+
let finalRoutes = prepareRoutes3(ctx, routes);
|
|
640
|
+
finalRoutes = await ((_b = (_a = ctx.options).onRoutesGenerated) == null ? void 0 : _b.call(_a, finalRoutes)) || finalRoutes;
|
|
641
|
+
return finalRoutes;
|
|
642
|
+
}
|
|
643
|
+
async function resolveVueRoutes(ctx, customBlockMap) {
|
|
644
|
+
var _a, _b;
|
|
645
|
+
const finalRoutes = await computeVueRoutes(ctx, customBlockMap);
|
|
646
|
+
let client = generateClientCode(finalRoutes, ctx.options);
|
|
647
|
+
client = await ((_b = (_a = ctx.options).onClientGenerated) == null ? void 0 : _b.call(_a, client)) || client;
|
|
648
|
+
return client;
|
|
649
|
+
}
|
|
650
|
+
function vueResolver() {
|
|
651
|
+
const customBlockMap = /* @__PURE__ */ new Map();
|
|
652
|
+
async function checkCustomBlockChange(ctx, path) {
|
|
653
|
+
var _a;
|
|
654
|
+
const exitsCustomBlock = customBlockMap.get(path);
|
|
655
|
+
let customBlock;
|
|
656
|
+
try {
|
|
657
|
+
customBlock = await getRouteBlock(path, ctx.options);
|
|
658
|
+
} catch (error) {
|
|
659
|
+
(_a = ctx.logger) == null ? void 0 : _a.error(colors.red(`[vite-plugin-pages] ${error.message}`));
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
if (!exitsCustomBlock && !customBlock)
|
|
663
|
+
return;
|
|
664
|
+
if (!customBlock) {
|
|
665
|
+
customBlockMap.delete(path);
|
|
666
|
+
ctx.debug.routeBlock("%s deleted", path);
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (!exitsCustomBlock || !dequal(exitsCustomBlock, customBlock)) {
|
|
670
|
+
ctx.debug.routeBlock("%s old: %O", path, exitsCustomBlock);
|
|
671
|
+
ctx.debug.routeBlock("%s new: %O", path, customBlock);
|
|
672
|
+
customBlockMap.set(path, customBlock);
|
|
673
|
+
ctx.onUpdate();
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
return {
|
|
677
|
+
resolveExtensions() {
|
|
678
|
+
return ["vue", "ts", "js"];
|
|
679
|
+
},
|
|
680
|
+
resolveModuleIds() {
|
|
681
|
+
return ["~pages", "pages-generated", "virtual:generated-pages"];
|
|
682
|
+
},
|
|
683
|
+
async resolveRoutes(ctx) {
|
|
684
|
+
return resolveVueRoutes(ctx, customBlockMap);
|
|
685
|
+
},
|
|
686
|
+
async getComputedRoutes(ctx) {
|
|
687
|
+
return computeVueRoutes(ctx, customBlockMap);
|
|
688
|
+
},
|
|
689
|
+
hmr: {
|
|
690
|
+
added: async (ctx, path) => checkCustomBlockChange(ctx, path),
|
|
691
|
+
changed: async (ctx, path) => checkCustomBlockChange(ctx, path),
|
|
692
|
+
removed: async (_ctx, path) => {
|
|
693
|
+
customBlockMap.delete(path);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// src/options.ts
|
|
700
|
+
function resolvePageDirs(dirs, root, exclude) {
|
|
701
|
+
dirs = toArray(dirs);
|
|
702
|
+
return dirs.flatMap((dir) => {
|
|
703
|
+
const option = typeof dir === "string" ? { dir, baseRoute: "" } : dir;
|
|
704
|
+
option.dir = slash(resolve2(root, option.dir)).replace(`${root}/`, "");
|
|
705
|
+
option.baseRoute = option.baseRoute.replace(/^\//, "").replace(/\/$/, "");
|
|
706
|
+
return getPageDirs(option, root, exclude);
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
var syncIndexResolver = (filepath, options) => {
|
|
710
|
+
for (const page of options.dirs) {
|
|
711
|
+
if (page.baseRoute === "" && filepath.startsWith(`/${page.dir}/index`))
|
|
712
|
+
return "sync";
|
|
713
|
+
}
|
|
714
|
+
return "async";
|
|
715
|
+
};
|
|
716
|
+
function getResolver(originalResolver) {
|
|
717
|
+
let resolver = originalResolver || "vue";
|
|
718
|
+
if (typeof resolver !== "string")
|
|
719
|
+
return resolver;
|
|
720
|
+
switch (resolver) {
|
|
721
|
+
case "vue":
|
|
722
|
+
resolver = vueResolver();
|
|
723
|
+
break;
|
|
724
|
+
case "react":
|
|
725
|
+
resolver = reactResolver();
|
|
726
|
+
break;
|
|
727
|
+
case "solid":
|
|
728
|
+
resolver = solidResolver();
|
|
729
|
+
break;
|
|
730
|
+
default:
|
|
731
|
+
throw new Error(`Unsupported resolver: ${resolver}`);
|
|
732
|
+
}
|
|
733
|
+
return resolver;
|
|
734
|
+
}
|
|
735
|
+
function resolveOptions(userOptions, viteRoot) {
|
|
736
|
+
var _a;
|
|
737
|
+
const {
|
|
738
|
+
dirs = userOptions.pagesDir || ["src/pages"],
|
|
739
|
+
routeBlockLang = "json5",
|
|
740
|
+
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
741
|
+
caseSensitive = false,
|
|
742
|
+
syncIndex = true,
|
|
743
|
+
routeNameSeparator = "-",
|
|
744
|
+
extendRoute,
|
|
745
|
+
onRoutesGenerated,
|
|
746
|
+
onClientGenerated
|
|
747
|
+
} = userOptions;
|
|
748
|
+
const root = viteRoot || slash(process.cwd());
|
|
749
|
+
const importMode = userOptions.importMode || (syncIndex ? syncIndexResolver : "async");
|
|
750
|
+
const importPath = userOptions.importPath || "relative";
|
|
751
|
+
const resolver = getResolver(userOptions.resolver);
|
|
752
|
+
const extensions = userOptions.extensions || resolver.resolveExtensions();
|
|
753
|
+
const extensionsRE = new RegExp(`\\.(${extensions.join("|")})$`);
|
|
754
|
+
const resolvedDirs = resolvePageDirs(dirs, root, exclude);
|
|
755
|
+
const routeStyle = userOptions.nuxtStyle ? "nuxt" : userOptions.routeStyle || "next";
|
|
756
|
+
const moduleIds = userOptions.moduleId ? [userOptions.moduleId] : ((_a = resolver.resolveModuleIds) == null ? void 0 : _a.call(resolver)) || MODULE_IDS;
|
|
757
|
+
const resolvedOptions = {
|
|
758
|
+
dirs: resolvedDirs,
|
|
759
|
+
routeStyle,
|
|
760
|
+
routeBlockLang,
|
|
761
|
+
moduleIds,
|
|
762
|
+
root,
|
|
763
|
+
extensions,
|
|
764
|
+
importMode,
|
|
765
|
+
importPath,
|
|
766
|
+
exclude,
|
|
767
|
+
caseSensitive,
|
|
768
|
+
resolver,
|
|
769
|
+
extensionsRE,
|
|
770
|
+
extendRoute,
|
|
771
|
+
onRoutesGenerated,
|
|
772
|
+
onClientGenerated,
|
|
773
|
+
routeNameSeparator
|
|
774
|
+
};
|
|
775
|
+
return resolvedOptions;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// src/context.ts
|
|
779
|
+
var PageContext = class {
|
|
780
|
+
constructor(userOptions, viteRoot = process2.cwd()) {
|
|
781
|
+
this._pageRouteMap = /* @__PURE__ */ new Map();
|
|
782
|
+
this.rawOptions = userOptions;
|
|
783
|
+
this.root = slash(viteRoot);
|
|
784
|
+
debug.env("root", this.root);
|
|
785
|
+
this.options = resolveOptions(userOptions, this.root);
|
|
786
|
+
debug.options(this.options);
|
|
787
|
+
}
|
|
788
|
+
setLogger(logger) {
|
|
789
|
+
this.logger = logger;
|
|
790
|
+
}
|
|
791
|
+
setupViteServer(server) {
|
|
792
|
+
if (this._server === server)
|
|
793
|
+
return;
|
|
794
|
+
this._server = server;
|
|
795
|
+
this.setupWatcher(server.watcher);
|
|
796
|
+
}
|
|
797
|
+
setupWatcher(watcher) {
|
|
798
|
+
watcher.on("unlink", async (path) => {
|
|
799
|
+
path = slash(path);
|
|
800
|
+
if (!isTarget(path, this.options))
|
|
801
|
+
return;
|
|
802
|
+
await this.removePage(path);
|
|
803
|
+
this.onUpdate();
|
|
804
|
+
});
|
|
805
|
+
watcher.on("add", async (path) => {
|
|
806
|
+
path = slash(path);
|
|
807
|
+
if (!isTarget(path, this.options))
|
|
808
|
+
return;
|
|
809
|
+
const page = this.options.dirs.find((i) => path.startsWith(slash(resolve3(this.root, i.dir))));
|
|
810
|
+
await this.addPage(path, page);
|
|
811
|
+
this.onUpdate();
|
|
812
|
+
});
|
|
813
|
+
watcher.on("change", async (path) => {
|
|
814
|
+
var _a, _b;
|
|
815
|
+
path = slash(path);
|
|
816
|
+
if (!isTarget(path, this.options))
|
|
817
|
+
return;
|
|
818
|
+
const page = this._pageRouteMap.get(path);
|
|
819
|
+
if (page)
|
|
820
|
+
await ((_b = (_a = this.options.resolver.hmr) == null ? void 0 : _a.changed) == null ? void 0 : _b.call(_a, this, path));
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
async addPage(path, pageDir) {
|
|
824
|
+
var _a, _b;
|
|
825
|
+
debug.pages("add", path);
|
|
826
|
+
for (const p of toArray(path)) {
|
|
827
|
+
const pageDirPath = slash(resolve3(this.root, pageDir.dir));
|
|
828
|
+
const extension = this.options.extensions.find((ext) => p.endsWith(`.${ext}`));
|
|
829
|
+
if (!extension)
|
|
830
|
+
continue;
|
|
831
|
+
const route = slash(join2(pageDir.baseRoute, p.replace(`${pageDirPath}/`, "").replace(`.${extension}`, "")));
|
|
832
|
+
this._pageRouteMap.set(p, {
|
|
833
|
+
path: p,
|
|
834
|
+
route
|
|
835
|
+
});
|
|
836
|
+
await ((_b = (_a = this.options.resolver.hmr) == null ? void 0 : _a.added) == null ? void 0 : _b.call(_a, this, p));
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
async removePage(path) {
|
|
840
|
+
var _a, _b;
|
|
841
|
+
debug.pages("remove", path);
|
|
842
|
+
this._pageRouteMap.delete(path);
|
|
843
|
+
await ((_b = (_a = this.options.resolver.hmr) == null ? void 0 : _a.removed) == null ? void 0 : _b.call(_a, this, path));
|
|
844
|
+
}
|
|
845
|
+
onUpdate() {
|
|
846
|
+
if (!this._server)
|
|
847
|
+
return;
|
|
848
|
+
invalidatePagesModule(this._server);
|
|
849
|
+
debug.hmr("Reload generated pages.");
|
|
850
|
+
this._server.ws.send({
|
|
851
|
+
type: "full-reload"
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
async resolveRoutes() {
|
|
855
|
+
return this.options.resolver.resolveRoutes(this);
|
|
856
|
+
}
|
|
857
|
+
async searchGlob() {
|
|
858
|
+
const pageDirFiles = this.options.dirs.map((page) => {
|
|
859
|
+
const pagesDirPath = slash(resolve3(this.options.root, page.dir));
|
|
860
|
+
const files = getPageFiles(pagesDirPath, this.options, page);
|
|
861
|
+
debug.search(page.dir, files);
|
|
862
|
+
return __spreadProps(__spreadValues({}, page), {
|
|
863
|
+
files: files.map((file) => slash(file))
|
|
864
|
+
});
|
|
865
|
+
});
|
|
866
|
+
for (const page of pageDirFiles)
|
|
867
|
+
await this.addPage(page.files, page);
|
|
868
|
+
debug.cache(this.pageRouteMap);
|
|
869
|
+
}
|
|
870
|
+
get debug() {
|
|
871
|
+
return debug;
|
|
872
|
+
}
|
|
873
|
+
get pageRouteMap() {
|
|
874
|
+
return this._pageRouteMap;
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
|
|
878
|
+
// src/index.ts
|
|
879
|
+
function pagesPlugin(userOptions = {}) {
|
|
880
|
+
let ctx;
|
|
881
|
+
return {
|
|
882
|
+
name: "vite-plugin-pages",
|
|
883
|
+
enforce: "pre",
|
|
884
|
+
async configResolved(config) {
|
|
885
|
+
if (!userOptions.resolver && config.plugins.find((i) => i.name.includes("vite:react"))) {
|
|
886
|
+
userOptions.resolver = "react";
|
|
887
|
+
}
|
|
888
|
+
if (!userOptions.resolver && config.plugins.find((i) => i.name.includes("solid"))) {
|
|
889
|
+
userOptions.resolver = "solid";
|
|
890
|
+
}
|
|
891
|
+
ctx = new PageContext(userOptions, config.root);
|
|
892
|
+
ctx.setLogger(config.logger);
|
|
893
|
+
await ctx.searchGlob();
|
|
894
|
+
},
|
|
895
|
+
api: {
|
|
896
|
+
getResolvedRoutes() {
|
|
897
|
+
return ctx.options.resolver.getComputedRoutes(ctx);
|
|
898
|
+
}
|
|
899
|
+
},
|
|
900
|
+
configureServer(server) {
|
|
901
|
+
ctx.setupViteServer(server);
|
|
902
|
+
},
|
|
903
|
+
resolveId(id) {
|
|
904
|
+
if (ctx.options.moduleIds.includes(id))
|
|
905
|
+
return `${MODULE_ID_VIRTUAL}?id=${id}`;
|
|
906
|
+
if (routeBlockQueryRE.test(id))
|
|
907
|
+
return ROUTE_BLOCK_ID_VIRTUAL;
|
|
908
|
+
return null;
|
|
909
|
+
},
|
|
910
|
+
async load(id) {
|
|
911
|
+
const {
|
|
912
|
+
moduleId,
|
|
913
|
+
pageId
|
|
914
|
+
} = parsePageRequest(id);
|
|
915
|
+
if (moduleId === MODULE_ID_VIRTUAL && pageId && ctx.options.moduleIds.includes(pageId))
|
|
916
|
+
return ctx.resolveRoutes();
|
|
917
|
+
if (id === ROUTE_BLOCK_ID_VIRTUAL) {
|
|
918
|
+
return {
|
|
919
|
+
code: "export default {};",
|
|
920
|
+
map: null
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
return null;
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
var index_default = pagesPlugin;
|
|
928
|
+
export {
|
|
929
|
+
PageContext,
|
|
930
|
+
index_default as default,
|
|
931
|
+
reactResolver,
|
|
932
|
+
solidResolver,
|
|
933
|
+
syncIndexResolver,
|
|
934
|
+
vueResolver
|
|
935
|
+
};
|
|
936
|
+
//# sourceMappingURL=index.js.map
|