@edgeone/nuxt-pages
Version:
A professional deployment package that seamlessly deploys your Nuxt 3/4 applications to Tencent Cloud EdgeOne platform with optimized performance and intelligent caching.
227 lines (222 loc) • 7.11 kB
JavaScript
var require = await (async () => {
var { createRequire } = await import("node:module");
return createRequire(import.meta.url);
})();
import {
getHandlersArrayWithAST
} from "./chunk-5VJRCUAW.js";
// src/build/routes.ts
import * as fs from "fs";
import * as path from "path";
var convertNuxtRoutePattern = (path2) => {
if (!path2.includes("[") && !path2.includes("_")) {
return path2;
}
let convertedPath = path2;
const catchAllMatch = path2.match(/\[\.\.\.([^\]]+)\]/);
if (catchAllMatch) {
const paramName = catchAllMatch[1];
convertedPath = convertedPath.replace(/\[\.\.\.([^\]]+)\]/g, `:${paramName}*`);
}
const dynamicMatch = path2.match(/\[([^\]]+)\]/);
if (dynamicMatch) {
const paramName = dynamicMatch[1];
convertedPath = convertedPath.replace(/\[([^\]]+)\]/g, `:${paramName}`);
}
const underscoreMatch = path2.match(/_([^\/\.]+)/);
if (underscoreMatch) {
const paramName = underscoreMatch[1];
convertedPath = convertedPath.replace(/_([^\/\.]+)/g, `:${paramName}`);
}
return convertedPath;
};
var createNuxtPagesRouteMeta = async (ctx) => {
const routeMap = {};
const prerenderRoutes = await ctx.getPrerenderRoutes();
if (prerenderRoutes && Array.isArray(prerenderRoutes)) {
for (const route of prerenderRoutes) {
routeMap[route] = {
isStatic: true
};
}
}
const staticPages = await ctx.getStaticPages();
if (staticPages && Array.isArray(staticPages)) {
for (const page of staticPages) {
if (!routeMap[page]) {
routeMap[page] = {};
}
routeMap[page].isStatic = true;
}
}
const routesManifest = await ctx.getRoutesManifest();
if (routesManifest) {
if (routesManifest.routes) {
for (let [route, routeInfo] of Object.entries(routesManifest.routes)) {
route = routeInfo.path;
if (!routeMap[route]) {
routeMap[route] = {};
}
if (routeInfo.prerender !== void 0) {
routeMap[route].isStatic = routeInfo.prerender;
}
if (routeInfo.swr !== void 0 && routeInfo.swr) {
routeMap[route].isr = routeInfo.swr;
}
if (routeInfo.isr !== void 0 && routeInfo.isr) {
routeMap[route].isr = routeInfo.isr;
}
}
}
}
const convertedRouteMap = {};
const pathsToDelete = [];
for (const [routePath, routeConfig] of Object.entries(routeMap)) {
const convertedPath = convertNuxtRoutePattern(routePath);
if (convertedPath !== routePath) {
pathsToDelete.push(routePath);
convertedRouteMap[convertedPath] = routeConfig;
}
}
for (const pathToDelete of pathsToDelete) {
delete routeMap[pathToDelete];
}
Object.assign(routeMap, convertedRouteMap);
const routesArray = Object.entries(routeMap).map(([path2, config]) => ({
path: path2,
...config
}));
const edgeOneDir = path.join(process.cwd(), ".edgeone");
if (!fs.existsSync(edgeOneDir)) {
fs.mkdirSync(edgeOneDir, { recursive: true });
}
const metaFilePath = path.join(edgeOneDir, "server-handler", "meta.json");
let existingMetaData = {};
if (fs.existsSync(metaFilePath)) {
try {
const existingContent = fs.readFileSync(metaFilePath, "utf-8");
existingMetaData = JSON.parse(existingContent);
} catch (error2) {
console.warn("Failed to parse existing meta.json:", error2);
}
}
const mergedMetaData = {
...existingMetaData,
routes: routesArray
};
await fs.writeFileSync(
metaFilePath,
JSON.stringify(mergedMetaData, null, 2),
"utf-8"
);
};
var createNuxtApiRoutesMeta = async (ctx) => {
const edgeOneDir = path.join(process.cwd(), ".edgeone");
if (!fs.existsSync(edgeOneDir)) {
console.error("Failed to create .edgeone directory:", error);
return;
}
const metaFilePath = path.join(edgeOneDir, "server-handler", "meta.json");
if (!fs.existsSync(metaFilePath)) {
console.error("Failed to create meta.json file:", error);
return;
}
const existingContent = await fs.readFileSync(metaFilePath, "utf-8");
const existingMetaData = JSON.parse(existingContent);
const nitroPath = path.join(edgeOneDir, "server-handler", "chunks", "nitro", "nitro.mjs");
let apiRoutes = [];
if (fs.existsSync(nitroPath)) {
try {
const nitroContent = fs.readFileSync(nitroPath, "utf-8");
const handlersArray = getHandlersArrayWithAST(nitroContent);
if (Array.isArray(handlersArray)) {
for (const handler of handlersArray) {
if (handler && typeof handler === "object") {
let routePath = null;
if (handler.route && typeof handler.route === "string") {
routePath = handler.route;
if (existingMetaData.routes.some((route) => route.path === routePath)) {
continue;
}
apiRoutes.push({
path: routePath,
handler
});
}
}
}
}
} catch (error2) {
console.error("Error parsing nitro.mjs for API routes:", error2);
}
}
const mergedMetaData = {
frameworkRoutes: existingMetaData.routes.concat(apiRoutes.map((route) => ({ path: route.path })))
};
const excludeRoutes = ["/**"];
let processedRoutes = mergedMetaData.frameworkRoutes.filter((route) => !excludeRoutes.includes(route.path)).map((route) => {
if (!route.path || route.path === "/**") {
return {
...route,
path: "/"
};
}
if (route.path && route.path.includes("**")) {
let regexPath = route.path.replace(/\*\*/g, ".*").replace(/\*/g, "[^/]*").replace(/\//g, "\\/");
regexPath = `^${regexPath}$`;
if (route.path.startsWith("/_ipx")) {
regexPath = `/_ipx/:path*`;
}
return {
...route,
path: regexPath
// Keep original path
};
} else if (route.path && route.path.includes("(.*)*")) {
let regexPath = route.path.replace(/\(\.\*\)\*/g, "*");
regexPath = `${regexPath}`;
return {
...route,
path: regexPath
// Keep original path
};
}
return route;
});
processedRoutes.push({
path: ".*-ssr-functions/:path*"
});
processedRoutes = processedRoutes.map((route) => {
if (route.hasOwnProperty("ssr")) {
Reflect.deleteProperty(route, "ssr");
}
route.path = route.path.replace(/\(/g, "").replace(/\)/g, "");
return route;
});
processedRoutes = processedRoutes.map((route) => {
if (route.path.startsWith("/:")) {
if (route.hasOwnProperty("isStatic")) {
route.isStatic = true;
} else {
Reflect.defineProperty(route, "isStatic", {
value: true,
enumerable: true
});
}
return route;
}
return route;
});
const finalMetaData = {
frameworkRoutes: processedRoutes,
conf: {
ssr404: true
}
};
await fs.writeFileSync(metaFilePath, JSON.stringify(finalMetaData, null, 2));
};
export {
convertNuxtRoutePattern,
createNuxtPagesRouteMeta,
createNuxtApiRoutesMeta
};