@edgeone/nuxt-pages 1.1.0-beta.3 → 1.1.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/dist/build/content/{static.js → module-inject.js} +8 -7
- package/dist/build/content/server.js +1 -2
- package/dist/build/functions/server.js +2 -3
- package/dist/build/plugin-context.js +3 -2
- package/dist/build/routes.js +7 -4
- package/dist/esm-chunks/chunk-3HA3DZ3G.js +261 -0
- package/dist/esm-chunks/chunk-AZLY3JO2.js +299 -0
- package/dist/esm-chunks/{chunk-NJ4SUJNF.js → chunk-K5VT7O2H.js} +844 -19
- package/dist/esm-chunks/{chunk-RNEZUAPL.js → chunk-PJWA566S.js} +4 -4
- package/dist/esm-chunks/{chunk-MONI3XWQ.js → chunk-VCJ5U4PV.js} +24 -541
- package/dist/esm-chunks/{chunk-BZQMVWYQ.js → chunk-VDBASNGL.js} +3 -3
- package/dist/esm-chunks/chunk-ZR3DYEBK.js +180 -0
- package/dist/index.js +28 -25
- package/dist/utils.js +2 -5
- package/package.json +3 -2
- package/dist/esm-chunks/chunk-7X4RPD4I.js +0 -131
- package/dist/esm-chunks/chunk-J25U56II.js +0 -213
- package/dist/esm-chunks/chunk-V2LFVP3C.js +0 -838
|
@@ -5,13 +5,14 @@
|
|
|
5
5
|
})();
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import "../../esm-chunks/chunk-
|
|
8
|
+
cleanupEdgeoneModule,
|
|
9
|
+
injectEdgeoneModule,
|
|
10
|
+
removeTempModule
|
|
11
|
+
} from "../../esm-chunks/chunk-ZR3DYEBK.js";
|
|
12
|
+
import "../../esm-chunks/chunk-VCJ5U4PV.js";
|
|
13
13
|
import "../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
14
14
|
export {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
cleanupEdgeoneModule,
|
|
16
|
+
injectEdgeoneModule,
|
|
17
|
+
removeTempModule
|
|
17
18
|
};
|
|
@@ -8,8 +8,7 @@ import {
|
|
|
8
8
|
RUN_CONFIG_FILE,
|
|
9
9
|
copyNuxtServerCode,
|
|
10
10
|
verifyNuxtHandlerDirStructure
|
|
11
|
-
} from "../../esm-chunks/chunk-
|
|
12
|
-
import "../../esm-chunks/chunk-V2LFVP3C.js";
|
|
11
|
+
} from "../../esm-chunks/chunk-K5VT7O2H.js";
|
|
13
12
|
import "../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
14
13
|
export {
|
|
15
14
|
RUN_CONFIG_FILE,
|
|
@@ -7,9 +7,8 @@
|
|
|
7
7
|
import {
|
|
8
8
|
createServerHandler,
|
|
9
9
|
patchNitroHandler
|
|
10
|
-
} from "../../esm-chunks/chunk-
|
|
11
|
-
import "../../esm-chunks/chunk-
|
|
12
|
-
import "../../esm-chunks/chunk-V2LFVP3C.js";
|
|
10
|
+
} from "../../esm-chunks/chunk-PJWA566S.js";
|
|
11
|
+
import "../../esm-chunks/chunk-K5VT7O2H.js";
|
|
13
12
|
import "../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
14
13
|
export {
|
|
15
14
|
createServerHandler,
|
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
import {
|
|
8
8
|
PluginContext,
|
|
9
9
|
SERVER_HANDLER_NAME
|
|
10
|
-
} from "../esm-chunks/chunk-
|
|
11
|
-
import "../esm-chunks/chunk-
|
|
10
|
+
} from "../esm-chunks/chunk-VDBASNGL.js";
|
|
11
|
+
import "../esm-chunks/chunk-AZLY3JO2.js";
|
|
12
|
+
import "../esm-chunks/chunk-VCJ5U4PV.js";
|
|
12
13
|
import "../esm-chunks/chunk-6BT4RYQJ.js";
|
|
13
14
|
export {
|
|
14
15
|
PluginContext,
|
package/dist/build/routes.js
CHANGED
|
@@ -6,11 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
8
|
createNuxtApiRoutesMeta,
|
|
9
|
-
createNuxtPagesRouteMeta
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
createNuxtPagesRouteMeta,
|
|
10
|
+
createNuxtRoutesMeta
|
|
11
|
+
} from "../esm-chunks/chunk-3HA3DZ3G.js";
|
|
12
|
+
import "../esm-chunks/chunk-AZLY3JO2.js";
|
|
13
|
+
import "../esm-chunks/chunk-VCJ5U4PV.js";
|
|
12
14
|
import "../esm-chunks/chunk-6BT4RYQJ.js";
|
|
13
15
|
export {
|
|
14
16
|
createNuxtApiRoutesMeta,
|
|
15
|
-
createNuxtPagesRouteMeta
|
|
17
|
+
createNuxtPagesRouteMeta,
|
|
18
|
+
createNuxtRoutesMeta
|
|
16
19
|
};
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
|
|
2
|
+
var require = await (async () => {
|
|
3
|
+
var { createRequire } = await import("node:module");
|
|
4
|
+
return createRequire(import.meta.url);
|
|
5
|
+
})();
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
getHandlersArrayWithAST,
|
|
9
|
+
getRouteRulesWithAST
|
|
10
|
+
} from "./chunk-AZLY3JO2.js";
|
|
11
|
+
|
|
12
|
+
// src/build/routes.ts
|
|
13
|
+
import * as fs from "fs";
|
|
14
|
+
import * as path from "path";
|
|
15
|
+
var CONFIG_VERSION = 3;
|
|
16
|
+
var CONFIG_FILE_NAME = "config.json";
|
|
17
|
+
function escapeRegExp(str) {
|
|
18
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
19
|
+
}
|
|
20
|
+
function normalizeRoutePath(p) {
|
|
21
|
+
if (!p) return "/";
|
|
22
|
+
const clean = p.split("?")[0];
|
|
23
|
+
if (clean === "" || clean === "/") return "/";
|
|
24
|
+
return clean.startsWith("/") ? clean.replace(/\/+$/, "") : `/${clean.replace(/\/+$/, "")}`;
|
|
25
|
+
}
|
|
26
|
+
function ensureServerHandlerDir() {
|
|
27
|
+
const edgeOneDir = path.join(process.cwd(), ".edgeone");
|
|
28
|
+
const serverHandlerDir = path.join(edgeOneDir, "cloud-functions", "ssr-node");
|
|
29
|
+
if (!fs.existsSync(edgeOneDir)) fs.mkdirSync(edgeOneDir, { recursive: true });
|
|
30
|
+
if (!fs.existsSync(serverHandlerDir)) fs.mkdirSync(serverHandlerDir, { recursive: true });
|
|
31
|
+
return serverHandlerDir;
|
|
32
|
+
}
|
|
33
|
+
function getConfigPath() {
|
|
34
|
+
return path.join(ensureServerHandlerDir(), CONFIG_FILE_NAME);
|
|
35
|
+
}
|
|
36
|
+
function writeConfig(config) {
|
|
37
|
+
const configPath = getConfigPath();
|
|
38
|
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
|
|
39
|
+
}
|
|
40
|
+
function normalizeHeaders(headers) {
|
|
41
|
+
if (!headers || typeof headers !== "object") return null;
|
|
42
|
+
const out = {};
|
|
43
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
44
|
+
if (typeof k !== "string" || !k) continue;
|
|
45
|
+
if (typeof v === "string") out[k] = v;
|
|
46
|
+
else if (typeof v === "number" || typeof v === "boolean") out[k] = String(v);
|
|
47
|
+
}
|
|
48
|
+
return Object.keys(out).length ? out : null;
|
|
49
|
+
}
|
|
50
|
+
function extractInlineRuntimeConfigFromNitroMjs(code) {
|
|
51
|
+
if (!code || typeof code !== "string") return null;
|
|
52
|
+
const idx = code.search(/\b_inlineRuntimeConfig\b\s*=\s*\{/);
|
|
53
|
+
if (idx === -1) return null;
|
|
54
|
+
const braceStart = code.indexOf("{", idx);
|
|
55
|
+
if (braceStart === -1) return null;
|
|
56
|
+
let depth = 0;
|
|
57
|
+
let inStr = false;
|
|
58
|
+
let quote = "";
|
|
59
|
+
let escaped = false;
|
|
60
|
+
let end = -1;
|
|
61
|
+
for (let i = braceStart; i < code.length; i++) {
|
|
62
|
+
const ch = code[i];
|
|
63
|
+
if (inStr) {
|
|
64
|
+
if (escaped) {
|
|
65
|
+
escaped = false;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (ch === "\\") {
|
|
69
|
+
escaped = true;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (ch === quote) {
|
|
73
|
+
inStr = false;
|
|
74
|
+
quote = "";
|
|
75
|
+
}
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (ch === '"' || ch === "'") {
|
|
79
|
+
inStr = true;
|
|
80
|
+
quote = ch;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (ch === "{") {
|
|
84
|
+
depth++;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (ch === "}") {
|
|
88
|
+
depth--;
|
|
89
|
+
if (depth === 0) {
|
|
90
|
+
end = i;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (end === -1) return null;
|
|
97
|
+
const objStr = code.slice(braceStart, end + 1);
|
|
98
|
+
try {
|
|
99
|
+
return JSON.parse(objStr);
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function getRouteRulesFromNitroInlineRuntimeConfig(ctx) {
|
|
105
|
+
const nitroMjsPath = resolveNitroMjsPath();
|
|
106
|
+
if (!nitroMjsPath) return null;
|
|
107
|
+
try {
|
|
108
|
+
const nitroContent = fs.readFileSync(nitroMjsPath, "utf-8");
|
|
109
|
+
const inlineCfg = extractInlineRuntimeConfigFromNitroMjs(nitroContent);
|
|
110
|
+
const rr = inlineCfg?.nitro?.routeRules;
|
|
111
|
+
if (rr && typeof rr === "object") return rr;
|
|
112
|
+
} catch {
|
|
113
|
+
}
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
async function getRouteRulesFromBuildOutput(ctx) {
|
|
117
|
+
const fromNitroMjs = await getRouteRulesFromNitroInlineRuntimeConfig(ctx);
|
|
118
|
+
if (fromNitroMjs) return fromNitroMjs;
|
|
119
|
+
try {
|
|
120
|
+
if (typeof ctx?.getNuxtConfig === "function") {
|
|
121
|
+
const cfg = await ctx.getNuxtConfig();
|
|
122
|
+
const parsed = getRouteRulesWithAST(cfg || "");
|
|
123
|
+
if (parsed && typeof parsed === "object") return parsed;
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
async function buildHeadersRoutes(ctx) {
|
|
130
|
+
const routeRules = await getRouteRulesFromBuildOutput(ctx);
|
|
131
|
+
if (!routeRules || typeof routeRules !== "object") return [];
|
|
132
|
+
const out = [];
|
|
133
|
+
for (const [srcRaw, rule] of Object.entries(routeRules)) {
|
|
134
|
+
const headers = normalizeHeaders(rule?.headers);
|
|
135
|
+
if (!headers) continue;
|
|
136
|
+
const src = typeof srcRaw === "string" && srcRaw.startsWith("^") ? srcRaw : normalizeRoutePath(String(srcRaw)).replace(/\*\*/g, "(.*)");
|
|
137
|
+
out.push({ src, headers });
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
async function buildStage1Routes(ctx) {
|
|
142
|
+
const routes = [];
|
|
143
|
+
routes.push({ src: "^/([^.]+[^/.])$", dest: "/$1/", continue: true });
|
|
144
|
+
routes.push(...await buildHeadersRoutes(ctx));
|
|
145
|
+
routes.push({ handle: "filesystem" });
|
|
146
|
+
return routes;
|
|
147
|
+
}
|
|
148
|
+
function resolveNitroMjsPath() {
|
|
149
|
+
const edgeOneDir = path.join(process.cwd(), ".edgeone");
|
|
150
|
+
const defaultNitroPath = path.join(edgeOneDir, "cloud-functions", "ssr-node", "chunks", "nitro", "nitro.mjs");
|
|
151
|
+
const fallbackNitroPath = path.join(edgeOneDir, "cloud-functions", "ssr-node", "chunks", "_", "nitro.mjs");
|
|
152
|
+
const nitroPath = fs.existsSync(defaultNitroPath) ? defaultNitroPath : fallbackNitroPath;
|
|
153
|
+
return fs.existsSync(nitroPath) ? nitroPath : null;
|
|
154
|
+
}
|
|
155
|
+
async function buildExplicitApiRoutes(_ctx) {
|
|
156
|
+
const nitroPath = resolveNitroMjsPath();
|
|
157
|
+
if (!nitroPath) {
|
|
158
|
+
console.error("Nitro entry(nitro.mjs) not found");
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const nitroContent = fs.readFileSync(nitroPath, "utf-8");
|
|
163
|
+
const handlersArray = getHandlersArrayWithAST(nitroContent);
|
|
164
|
+
if (!Array.isArray(handlersArray)) return [];
|
|
165
|
+
const seen = /* @__PURE__ */ new Set();
|
|
166
|
+
const out = [];
|
|
167
|
+
for (const h of handlersArray) {
|
|
168
|
+
const route = typeof h?.route === "string" ? String(h.route) : "";
|
|
169
|
+
if (!route) continue;
|
|
170
|
+
const normalized = normalizeRoutePath(route);
|
|
171
|
+
if (!normalized.startsWith("/api")) continue;
|
|
172
|
+
const rule = route.startsWith("^") ? { src: route } : nuxtRoutePathToRouteRule(normalized);
|
|
173
|
+
const src = rule?.src;
|
|
174
|
+
if (!src || seen.has(src)) continue;
|
|
175
|
+
seen.add(src);
|
|
176
|
+
out.push(rule);
|
|
177
|
+
}
|
|
178
|
+
out.sort((a, b) => String(b.src || "").length - String(a.src || "").length);
|
|
179
|
+
return out;
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.error("Error parsing nitro.mjs for API routes:", error);
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function isCatchAllLikeRoutePath(p) {
|
|
186
|
+
const clean = normalizeRoutePath(p);
|
|
187
|
+
return clean.includes("(.*)") || clean.includes(".*") || clean.includes(":all");
|
|
188
|
+
}
|
|
189
|
+
function nuxtRoutePathToRouteRule(routePath) {
|
|
190
|
+
const p = normalizeRoutePath(routePath);
|
|
191
|
+
if (!p.startsWith("/")) return null;
|
|
192
|
+
if (p === "/") return { src: "^/$" };
|
|
193
|
+
const parts = p.split("/").filter(Boolean);
|
|
194
|
+
const srcParts = [];
|
|
195
|
+
for (const seg of parts) {
|
|
196
|
+
if (seg === "*" || seg === "**") {
|
|
197
|
+
srcParts.push("(.*)");
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (seg.startsWith(":")) {
|
|
201
|
+
const m = seg.match(/^:([A-Za-z_$][\w$]*)(?:\((.*)\))?(\*)?$/);
|
|
202
|
+
const inner = m ? m[2] : null;
|
|
203
|
+
const star = m ? m[3] : null;
|
|
204
|
+
if (star || inner === ".*") {
|
|
205
|
+
srcParts.push("(.*)");
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (!inner || inner.trim() === "") {
|
|
209
|
+
srcParts.push("([^/]+)");
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
srcParts.push(`((?:${inner}))`);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
srcParts.push(escapeRegExp(seg));
|
|
216
|
+
}
|
|
217
|
+
const srcBody = srcParts.join("/");
|
|
218
|
+
return { src: `^/${srcBody}$` };
|
|
219
|
+
}
|
|
220
|
+
async function buildExplicitSsrAndSwrRoutes(ctx) {
|
|
221
|
+
const routesManifest = await ctx.getRoutesManifest();
|
|
222
|
+
const manifestRoutes = Array.isArray(routesManifest?.routes) ? routesManifest.routes : [];
|
|
223
|
+
const seen = /* @__PURE__ */ new Set();
|
|
224
|
+
const tmp = [];
|
|
225
|
+
for (const r of manifestRoutes) {
|
|
226
|
+
const p = typeof r?.path === "string" ? r.path : "";
|
|
227
|
+
if (!p) continue;
|
|
228
|
+
const normalized = normalizeRoutePath(p);
|
|
229
|
+
if (normalized.startsWith("/api")) continue;
|
|
230
|
+
const isSsr = r?.ssr === true;
|
|
231
|
+
const isSwr = r?.swr !== false && r?.swr !== void 0 && r?.swr !== null;
|
|
232
|
+
if (!isSsr && !isSwr) continue;
|
|
233
|
+
const rule = nuxtRoutePathToRouteRule(normalized);
|
|
234
|
+
const src = rule?.src;
|
|
235
|
+
if (!src || seen.has(src)) continue;
|
|
236
|
+
seen.add(src);
|
|
237
|
+
const score = (isCatchAllLikeRoutePath(normalized) ? -1e4 : 0) + normalized.split("/").length;
|
|
238
|
+
tmp.push({ score, rule });
|
|
239
|
+
}
|
|
240
|
+
tmp.sort((a, b) => b.score - a.score);
|
|
241
|
+
return tmp.map((x) => x.rule);
|
|
242
|
+
}
|
|
243
|
+
var createNuxtRoutesMeta = async (ctx) => {
|
|
244
|
+
const routes = await buildStage1Routes(ctx);
|
|
245
|
+
routes.push(...await buildExplicitApiRoutes(ctx));
|
|
246
|
+
routes.push(...await buildExplicitSsrAndSwrRoutes(ctx));
|
|
247
|
+
routes.push({ src: "^/(.*)$" });
|
|
248
|
+
writeConfig({ version: CONFIG_VERSION, routes });
|
|
249
|
+
};
|
|
250
|
+
var createNuxtPagesRouteMeta = async (ctx) => {
|
|
251
|
+
await createNuxtRoutesMeta(ctx);
|
|
252
|
+
};
|
|
253
|
+
var createNuxtApiRoutesMeta = async (ctx) => {
|
|
254
|
+
await createNuxtRoutesMeta(ctx);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export {
|
|
258
|
+
createNuxtRoutesMeta,
|
|
259
|
+
createNuxtPagesRouteMeta,
|
|
260
|
+
createNuxtApiRoutesMeta
|
|
261
|
+
};
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
|
|
2
|
+
var require = await (async () => {
|
|
3
|
+
var { createRequire } = await import("node:module");
|
|
4
|
+
return createRequire(import.meta.url);
|
|
5
|
+
})();
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
require_lib
|
|
9
|
+
} from "./chunk-VCJ5U4PV.js";
|
|
10
|
+
import {
|
|
11
|
+
__toESM
|
|
12
|
+
} from "./chunk-6BT4RYQJ.js";
|
|
13
|
+
|
|
14
|
+
// src/utils.ts
|
|
15
|
+
var parser = __toESM(require_lib(), 1);
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
import * as fs from "fs";
|
|
18
|
+
import * as path from "path";
|
|
19
|
+
var require2 = createRequire(import.meta.url);
|
|
20
|
+
var traverse = require2("@babel/traverse").default;
|
|
21
|
+
function useStaticBuild(cwd) {
|
|
22
|
+
const edgeoneJsonPath = path.join(cwd, "edgeone.json");
|
|
23
|
+
let originalEdgeOneConfig = null;
|
|
24
|
+
let config = {};
|
|
25
|
+
if (!fs.existsSync(edgeoneJsonPath)) {
|
|
26
|
+
fs.writeFileSync(edgeoneJsonPath, "{}", "utf-8");
|
|
27
|
+
config = {};
|
|
28
|
+
} else {
|
|
29
|
+
try {
|
|
30
|
+
const fileContent = fs.readFileSync(edgeoneJsonPath, "utf-8");
|
|
31
|
+
originalEdgeOneConfig = fileContent;
|
|
32
|
+
config = JSON.parse(fileContent);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error("\u8BFB\u53D6 edgeone.json \u6587\u4EF6\u5931\u8D25:", error);
|
|
35
|
+
config = {};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
config.buildCommand = "npm run generate";
|
|
39
|
+
try {
|
|
40
|
+
fs.writeFileSync(edgeoneJsonPath, JSON.stringify(config, null, 2), "utf-8");
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error("\u5199\u5165 edgeone.json \u6587\u4EF6\u5931\u8D25:", error);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
return originalEdgeOneConfig;
|
|
46
|
+
}
|
|
47
|
+
function resetEdgeOneConfig(cwd, originalEdgeOneConfig) {
|
|
48
|
+
const edgeoneJsonPath = path.join(cwd, "edgeone.json");
|
|
49
|
+
try {
|
|
50
|
+
if (originalEdgeOneConfig == null) {
|
|
51
|
+
fs.rmSync(edgeoneJsonPath, { recursive: true, force: true });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
fs.writeFileSync(edgeoneJsonPath, originalEdgeOneConfig, "utf-8");
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error("\u5199\u5165 edgeone.json \u6587\u4EF6\u5931\u8D25:", error);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function astNodeToObject(node) {
|
|
61
|
+
if (node.type === "ObjectExpression") {
|
|
62
|
+
const obj = {};
|
|
63
|
+
for (const prop of node.properties) {
|
|
64
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier") {
|
|
65
|
+
obj[prop.key.name] = astNodeToObject(prop.value);
|
|
66
|
+
} else if (prop.type === "ObjectProperty" && prop.key.type === "StringLiteral") {
|
|
67
|
+
obj[prop.key.value] = astNodeToObject(prop.value);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return obj;
|
|
71
|
+
} else if (node.type === "ArrayExpression") {
|
|
72
|
+
return node.elements.map((element) => element ? astNodeToObject(element) : null);
|
|
73
|
+
} else if (node.type === "StringLiteral" || node.type === "NumericLiteral" || node.type === "BooleanLiteral") {
|
|
74
|
+
return node.value;
|
|
75
|
+
} else if (node.type === "NullLiteral") {
|
|
76
|
+
return null;
|
|
77
|
+
} else {
|
|
78
|
+
console.warn(`Unsupported node type in astNodeToObject: ${node.type}`);
|
|
79
|
+
return void 0;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function getRouteRulesWithAST(code) {
|
|
83
|
+
try {
|
|
84
|
+
const ast = parser.parse(code, {
|
|
85
|
+
sourceType: "module",
|
|
86
|
+
plugins: ["typescript"]
|
|
87
|
+
});
|
|
88
|
+
let routeRulesObject = null;
|
|
89
|
+
traverse(ast, {
|
|
90
|
+
// We're looking for a function call
|
|
91
|
+
CallExpression(path2) {
|
|
92
|
+
const { node } = path2;
|
|
93
|
+
if (node.callee.type === "Identifier" && node.callee.name === "defineNuxtConfig") {
|
|
94
|
+
if (node.arguments.length > 0 && node.arguments[0].type === "ObjectExpression") {
|
|
95
|
+
const configObjectNode = node.arguments[0];
|
|
96
|
+
for (const prop of configObjectNode.properties) {
|
|
97
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "routeRules") {
|
|
98
|
+
routeRulesObject = astNodeToObject(prop.value);
|
|
99
|
+
path2.stop();
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
return routeRulesObject;
|
|
108
|
+
} catch (e) {
|
|
109
|
+
console.error("AST parsing or traversal failed:", e);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function getPrerenderRoutesWithAST(code) {
|
|
114
|
+
try {
|
|
115
|
+
const ast = parser.parse(code, {
|
|
116
|
+
sourceType: "module",
|
|
117
|
+
plugins: ["typescript"]
|
|
118
|
+
});
|
|
119
|
+
let prerenderRoutes = [];
|
|
120
|
+
traverse(ast, {
|
|
121
|
+
// We're looking for a function call
|
|
122
|
+
CallExpression(path2) {
|
|
123
|
+
const { node } = path2;
|
|
124
|
+
if (node.callee.type === "Identifier" && node.callee.name === "defineNuxtConfig") {
|
|
125
|
+
if (node.arguments.length > 0 && node.arguments[0].type === "ObjectExpression") {
|
|
126
|
+
const configObjectNode = node.arguments[0];
|
|
127
|
+
for (const prop of configObjectNode.properties) {
|
|
128
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "nitro" && prop.value.type === "ObjectExpression") {
|
|
129
|
+
for (const nitroProp of prop.value.properties) {
|
|
130
|
+
if (nitroProp.type === "ObjectProperty" && nitroProp.key.type === "Identifier" && nitroProp.key.name === "prerender" && nitroProp.value.type === "ObjectExpression") {
|
|
131
|
+
for (const prerenderProp of nitroProp.value.properties) {
|
|
132
|
+
if (prerenderProp.type === "ObjectProperty" && prerenderProp.key.type === "Identifier" && prerenderProp.key.name === "routes" && prerenderProp.value.type === "ArrayExpression") {
|
|
133
|
+
const routesArray = astNodeToObject(prerenderProp.value);
|
|
134
|
+
if (Array.isArray(routesArray)) {
|
|
135
|
+
prerenderRoutes = routesArray.filter((route) => typeof route === "string");
|
|
136
|
+
}
|
|
137
|
+
path2.stop();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return prerenderRoutes;
|
|
150
|
+
} catch (e) {
|
|
151
|
+
console.error("AST parsing or traversal failed:", e);
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function getRoutesArrayWithAST(code) {
|
|
156
|
+
try {
|
|
157
|
+
const ast = parser.parse(code, {
|
|
158
|
+
sourceType: "module",
|
|
159
|
+
plugins: ["typescript"]
|
|
160
|
+
});
|
|
161
|
+
let routesArray = null;
|
|
162
|
+
traverse(ast, {
|
|
163
|
+
// We're looking for a variable declaration
|
|
164
|
+
VariableDeclaration(path2) {
|
|
165
|
+
const { node } = path2;
|
|
166
|
+
for (const declarator of node.declarations) {
|
|
167
|
+
if (declarator.type === "VariableDeclarator" && declarator.id.type === "Identifier" && declarator.id.name === "_routes") {
|
|
168
|
+
if (declarator.init && declarator.init.type === "ArrayExpression") {
|
|
169
|
+
routesArray = astNodeToObjectForRoutes(declarator.init);
|
|
170
|
+
path2.stop();
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
return routesArray;
|
|
178
|
+
} catch (e) {
|
|
179
|
+
console.error("AST parsing or traversal failed:", e);
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function getHandlersArrayWithAST(code) {
|
|
184
|
+
try {
|
|
185
|
+
const ast = parser.parse(code, {
|
|
186
|
+
sourceType: "module",
|
|
187
|
+
plugins: ["typescript"]
|
|
188
|
+
});
|
|
189
|
+
let handlersArray = [];
|
|
190
|
+
traverse(ast, {
|
|
191
|
+
// We're looking for a variable declaration
|
|
192
|
+
VariableDeclaration(path2) {
|
|
193
|
+
const { node } = path2;
|
|
194
|
+
for (const declarator of node.declarations) {
|
|
195
|
+
if (declarator.type === "VariableDeclarator" && declarator.id.type === "Identifier" && declarator.id.name === "handlers") {
|
|
196
|
+
if (declarator.init && declarator.init.type === "ArrayExpression") {
|
|
197
|
+
handlersArray = astNodeToObjectForRoutes(declarator.init);
|
|
198
|
+
path2.stop();
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
return handlersArray;
|
|
206
|
+
} catch (e) {
|
|
207
|
+
console.error("AST parsing or traversal failed:", e);
|
|
208
|
+
return [];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function astNodeToObjectForRoutes(node) {
|
|
212
|
+
if (node.type === "ObjectExpression") {
|
|
213
|
+
const obj = {};
|
|
214
|
+
for (const prop of node.properties) {
|
|
215
|
+
if (prop.type === "ObjectProperty") {
|
|
216
|
+
let key;
|
|
217
|
+
if (prop.key.type === "Identifier") {
|
|
218
|
+
key = prop.key.name;
|
|
219
|
+
} else if (prop.key.type === "StringLiteral") {
|
|
220
|
+
key = prop.key.value;
|
|
221
|
+
} else {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (prop.value.type === "ArrowFunctionExpression") {
|
|
225
|
+
if (prop.value.body.type === "CallExpression" && prop.value.body.callee.type === "Import" && prop.value.body.arguments.length > 0 && prop.value.body.arguments[0].type === "StringLiteral") {
|
|
226
|
+
const importPath = prop.value.body.arguments[0].value;
|
|
227
|
+
obj[key] = `() => import('${importPath}')`;
|
|
228
|
+
} else {
|
|
229
|
+
obj[key] = "[Function]";
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
obj[key] = astNodeToObjectForRoutes(prop.value);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return obj;
|
|
237
|
+
} else if (node.type === "ArrayExpression") {
|
|
238
|
+
return node.elements.map((element) => element ? astNodeToObjectForRoutes(element) : null);
|
|
239
|
+
} else if (node.type === "StringLiteral" || node.type === "NumericLiteral" || node.type === "BooleanLiteral") {
|
|
240
|
+
return node.value;
|
|
241
|
+
} else if (node.type === "NullLiteral") {
|
|
242
|
+
return null;
|
|
243
|
+
} else {
|
|
244
|
+
return `[${node.type}]`;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function getModulesWithAST(code) {
|
|
248
|
+
try {
|
|
249
|
+
const ast = parser.parse(code, {
|
|
250
|
+
sourceType: "module",
|
|
251
|
+
plugins: ["typescript"]
|
|
252
|
+
});
|
|
253
|
+
let modulesArray = [];
|
|
254
|
+
traverse(ast, {
|
|
255
|
+
CallExpression(path2) {
|
|
256
|
+
const { node } = path2;
|
|
257
|
+
if (node.callee.type === "Identifier" && node.callee.name === "defineNuxtConfig") {
|
|
258
|
+
if (node.arguments.length > 0 && node.arguments[0].type === "ObjectExpression") {
|
|
259
|
+
const configObject = node.arguments[0];
|
|
260
|
+
for (const prop of configObject.properties) {
|
|
261
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "modules") {
|
|
262
|
+
if (prop.value.type === "ArrayExpression") {
|
|
263
|
+
for (const element of prop.value.elements) {
|
|
264
|
+
if (element) {
|
|
265
|
+
if (element.type === "StringLiteral") {
|
|
266
|
+
modulesArray.push(element.value);
|
|
267
|
+
} else if (element.type === "ArrayExpression" && element.elements.length > 0) {
|
|
268
|
+
const firstElement = element.elements[0];
|
|
269
|
+
if (firstElement && firstElement.type === "StringLiteral") {
|
|
270
|
+
modulesArray.push(firstElement.value);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
path2.stop();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
return modulesArray;
|
|
285
|
+
} catch (e) {
|
|
286
|
+
console.error("AST parsing or traversal failed:", e);
|
|
287
|
+
return [];
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export {
|
|
292
|
+
useStaticBuild,
|
|
293
|
+
resetEdgeOneConfig,
|
|
294
|
+
getRouteRulesWithAST,
|
|
295
|
+
getPrerenderRoutesWithAST,
|
|
296
|
+
getRoutesArrayWithAST,
|
|
297
|
+
getHandlersArrayWithAST,
|
|
298
|
+
getModulesWithAST
|
|
299
|
+
};
|