@webnumseoagent/next 0.1.4 → 0.1.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/bin/wrap.mjs +24 -3
- package/package.json +1 -1
package/bin/wrap.mjs
CHANGED
|
@@ -103,16 +103,37 @@ export async function wrapFile(code, routeExpr, importSpec) {
|
|
|
103
103
|
// ── Путь 1: функция generateMetadata — оборачиваем её последний object-return ──
|
|
104
104
|
// locale опционально (мультиязычные — есть, одноязычные — нет).
|
|
105
105
|
const hasLocale = /\blocale\b/.test(code);
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
|
|
107
|
+
// База доступа к params: `generateMetadata({ params })` → "params";
|
|
108
|
+
// `generateMetadata(props)` → "props.params".
|
|
109
|
+
let paramsBase = "params";
|
|
110
|
+
const p0 = fn.params && fn.params[0];
|
|
111
|
+
if (p0) {
|
|
112
|
+
if (p0.type === "ObjectPattern") {
|
|
113
|
+
const pp = p0.properties.find((x) => x.key && x.key.name === "params");
|
|
114
|
+
if (pp && pp.value && pp.value.name) paramsBase = pp.value.name;
|
|
115
|
+
} else if (p0.type === "Identifier") {
|
|
116
|
+
paramsBase = p0.name + ".params";
|
|
117
|
+
}
|
|
108
118
|
}
|
|
119
|
+
|
|
120
|
+
// Динамические параметры маршрута: если параметр деструктурирован (есть как
|
|
121
|
+
// отдельная переменная) — используем его напрямую (${slug}); иначе — через
|
|
122
|
+
// объект params (${params.slug}). Это чинит страницы, где обращаются как params.X.
|
|
123
|
+
let effRoute = routeExpr;
|
|
124
|
+
for (const m of [...routeExpr.matchAll(/\$\{(\w+)\}/g)]) {
|
|
125
|
+
const name = m[1];
|
|
126
|
+
const standalone = new RegExp("(?<!\\.)\\b" + name + "\\b").test(code);
|
|
127
|
+
if (!standalone) effRoute = effRoute.replace("${" + name + "}", "${" + paramsBase + "." + name + "}");
|
|
128
|
+
}
|
|
129
|
+
|
|
109
130
|
const returns = collectReturns(fn.body, fn);
|
|
110
131
|
if (!returns.length) return { ok: false, reason: "no-object-return" };
|
|
111
132
|
const target = returns[returns.length - 1];
|
|
112
133
|
let call;
|
|
113
134
|
try {
|
|
114
135
|
const localePart = hasLocale ? "locale, " : "";
|
|
115
|
-
call = babelParser.parseExpression(`seoMeta({ route: ${
|
|
136
|
+
call = babelParser.parseExpression(`seoMeta({ route: ${effRoute}, ${localePart}fallback: null })`, { plugins });
|
|
116
137
|
} catch { return { ok: false, reason: "route-expr" }; }
|
|
117
138
|
call.arguments[0].properties.find((p) => p.key && p.key.name === "fallback").value = target.argument;
|
|
118
139
|
target.argument = call;
|
package/package.json
CHANGED