@unhead/addons 2.0.0-alpha.21 → 2.0.0-alpha.23
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.
|
@@ -78,6 +78,12 @@ const TreeshakeServerComposables = createUnplugin((options = {}) => {
|
|
|
78
78
|
|
|
79
79
|
const UseSeoMetaTransform = createUnplugin((options = {}) => {
|
|
80
80
|
options.imports = options.imports || true;
|
|
81
|
+
function isValidPackage(s) {
|
|
82
|
+
if (s === "unhead" || s.startsWith("@unhead")) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
return [...options.importPaths || []].includes(s);
|
|
86
|
+
}
|
|
81
87
|
return {
|
|
82
88
|
name: "unhead:use-seo-meta-transform",
|
|
83
89
|
enforce: "post",
|
|
@@ -99,8 +105,7 @@ const UseSeoMetaTransform = createUnplugin((options = {}) => {
|
|
|
99
105
|
async transform(code, id) {
|
|
100
106
|
if (!code.includes("useSeoMeta") && !code.includes("useServerSeoMeta"))
|
|
101
107
|
return;
|
|
102
|
-
const
|
|
103
|
-
const statements = findStaticImports(code).filter((i) => packages.includes(i.specifier));
|
|
108
|
+
const statements = findStaticImports(code).filter((i) => isValidPackage(i.specifier));
|
|
104
109
|
const importNames = {};
|
|
105
110
|
for (const i of statements.flatMap((i2) => parseStaticImport(i2))) {
|
|
106
111
|
if (i.namedImports) {
|
|
@@ -112,26 +117,36 @@ const UseSeoMetaTransform = createUnplugin((options = {}) => {
|
|
|
112
117
|
}
|
|
113
118
|
const ast = this.parse(code);
|
|
114
119
|
const s = new MagicString(code);
|
|
115
|
-
|
|
120
|
+
let replacementPayload;
|
|
121
|
+
let replaceCount = 0;
|
|
122
|
+
let totalCount = 0;
|
|
116
123
|
walk(ast, {
|
|
117
124
|
enter(_node) {
|
|
118
|
-
if (options.imports && _node.type === "ImportDeclaration" &&
|
|
125
|
+
if (options.imports && _node.type === "ImportDeclaration" && isValidPackage(_node.source.value)) {
|
|
119
126
|
const node = _node;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
)
|
|
127
|
+
const hasSeoMeta = node.specifiers.some(
|
|
128
|
+
(s2) => s2.type === "ImportSpecifier" && ["useSeoMeta", "useServerSeoMeta"].includes(s2.imported.name)
|
|
129
|
+
);
|
|
130
|
+
if (!hasSeoMeta) {
|
|
124
131
|
return;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
132
|
+
}
|
|
133
|
+
const toImport = /* @__PURE__ */ new Set();
|
|
134
|
+
node.specifiers.forEach((spec) => {
|
|
135
|
+
if (spec.type === "ImportSpecifier" && ["useSeoMeta", "useServerSeoMeta"].includes(spec.imported.name)) {
|
|
136
|
+
toImport.add(spec.imported.name.includes("Server") ? "useServerHead" : "useHead");
|
|
137
|
+
} else {
|
|
138
|
+
toImport.add(spec.imported.name);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
if (toImport.size) {
|
|
142
|
+
replacementPayload = (useSeoMeta = false) => [node.specifiers[0].start, node.specifiers[node.specifiers.length - 1].end, [...toImport, useSeoMeta ? "useSeoMeta" : false].filter(Boolean).join(", ")];
|
|
143
|
+
}
|
|
130
144
|
} else if (_node.type === "CallExpression" && _node.callee.type === "Identifier" && Object.keys({
|
|
131
145
|
useSeoMeta: "useSeoMeta",
|
|
132
146
|
useServerSeoMeta: "useServerSeoMeta",
|
|
133
147
|
...importNames
|
|
134
148
|
}).includes(_node.callee.name)) {
|
|
149
|
+
replaceCount++;
|
|
135
150
|
const node = _node;
|
|
136
151
|
const calleeName = importNames[node.callee.name] || node.callee.name;
|
|
137
152
|
const properties = node.arguments[0].properties;
|
|
@@ -178,7 +193,21 @@ const UseSeoMetaTransform = createUnplugin((options = {}) => {
|
|
|
178
193
|
}
|
|
179
194
|
let value = code.substring(property.value.start, property.value.end);
|
|
180
195
|
if (property.value.type === "ArrayExpression") {
|
|
181
|
-
output
|
|
196
|
+
if (output === false)
|
|
197
|
+
return;
|
|
198
|
+
const elements = property.value.elements;
|
|
199
|
+
if (!elements.length)
|
|
200
|
+
return;
|
|
201
|
+
const metaTags = elements.map((element) => {
|
|
202
|
+
if (element.type !== "ObjectExpression")
|
|
203
|
+
return ` { ${key}: '${keyValue}', ${valueKey}: ${code.substring(element.start, element.end)} },`;
|
|
204
|
+
return element.properties.map((p) => {
|
|
205
|
+
const propKey = p.key.name;
|
|
206
|
+
const propValue = code.substring(p.value.start, p.value.end);
|
|
207
|
+
return ` { ${key}: '${keyValue}:${propKey}', ${valueKey}: ${propValue} },`;
|
|
208
|
+
}).join("\n");
|
|
209
|
+
});
|
|
210
|
+
output.push(metaTags.join("\n"));
|
|
182
211
|
return;
|
|
183
212
|
} else if (property.value.type === "ObjectExpression") {
|
|
184
213
|
const isStatic = property.value.properties.every((p) => p.value.type === "Literal" && typeof p.value.value === "string");
|
|
@@ -209,14 +238,15 @@ const UseSeoMetaTransform = createUnplugin((options = {}) => {
|
|
|
209
238
|
output.push("})");
|
|
210
239
|
s.overwrite(node.start, node.end, output.join("\n"));
|
|
211
240
|
}
|
|
241
|
+
} else if (_node.type === "Identifier" && ["useSeoMeta", "useServerSeoMeta"].includes(_node.name)) {
|
|
242
|
+
totalCount++;
|
|
212
243
|
}
|
|
213
244
|
}
|
|
214
245
|
});
|
|
215
246
|
if (s.hasChanged()) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
`);
|
|
247
|
+
if (replacementPayload) {
|
|
248
|
+
s.overwrite(...replacementPayload(replaceCount + 3 === totalCount));
|
|
249
|
+
}
|
|
220
250
|
return {
|
|
221
251
|
code: s.toString(),
|
|
222
252
|
map: s.generateMap({ includeContent: true, source: id })
|
package/dist/vite.mjs
CHANGED
package/dist/webpack.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unhead/addons",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-alpha.
|
|
4
|
+
"version": "2.0.0-alpha.23",
|
|
5
5
|
"author": "Harlan Wilton <harlan@harlanzw.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": "https://github.com/sponsors/harlan-zw",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"dist"
|
|
51
51
|
],
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"unhead": "2.0.0-alpha.
|
|
53
|
+
"unhead": "2.0.0-alpha.23"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@rollup/pluginutils": "^5.1.4",
|