@lark-apaas/fullstack-rspack-preset 1.0.20-alpha.app.1 → 1.0.20-alpha.app.3
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.
|
@@ -43,75 +43,60 @@ class OgMetaInjectionPlugin {
|
|
|
43
43
|
return;
|
|
44
44
|
}
|
|
45
45
|
const hooks = HtmlPlugin.getHooks(compilation);
|
|
46
|
-
// 使用
|
|
47
|
-
hooks.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
46
|
+
// 使用 beforeEmit 钩子,在 HTML 字符串层面操作
|
|
47
|
+
hooks.beforeEmit.tapAsync('OgMetaInjectionPlugin', (data, callback) => {
|
|
48
|
+
try {
|
|
49
|
+
let html = data.html;
|
|
50
|
+
// 1. 处理 OG Meta 标签
|
|
51
|
+
const ogTags = this.options.customTags || this.defaultOgTags;
|
|
52
|
+
ogTags.forEach(({ property, placeholder }) => {
|
|
53
|
+
// 转义特殊字符,避免正则表达式错误
|
|
54
|
+
const escapedProperty = property.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
55
|
+
// 检查是否存在该 property 的 meta 标签
|
|
56
|
+
const metaRegex = new RegExp(`<meta\\s+[^>]*property=["']?${escapedProperty}["']?[^>]*>`, 'i');
|
|
57
|
+
if (metaRegex.test(html)) {
|
|
58
|
+
// 如果存在,替换其 content 为占位符
|
|
59
|
+
const replaceRegex = new RegExp(`(<meta\\s+[^>]*property=["']?${escapedProperty}["']?[^>]*content=)["'][^"']*["']([^>]*>)`, 'gi');
|
|
60
|
+
html = html.replace(replaceRegex, `$1"${placeholder}"$2`);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// 如果不存在,在 </head> 前插入新标签
|
|
64
|
+
const newMetaTag = `\n <meta property="${property}" content="${placeholder}">`;
|
|
65
|
+
html = html.replace('</head>', `${newMetaTag}\n </head>`);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
// 2. 处理 <title> 标签
|
|
69
|
+
const titlePlaceholder = this.options.titlePlaceholder || this.defaultTitlePlaceholder;
|
|
70
|
+
const titleRegex = /<title>[^<]*<\/title>/i;
|
|
71
|
+
if (titleRegex.test(html)) {
|
|
72
|
+
// 如果存在 title 标签,替换其内容
|
|
73
|
+
html = html.replace(/<title>[^<]*<\/title>/gi, `<title>${titlePlaceholder}</title>`);
|
|
59
74
|
}
|
|
60
75
|
else {
|
|
61
|
-
//
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
voidTag: true,
|
|
65
|
-
attributes: {
|
|
66
|
-
property: property,
|
|
67
|
-
content: placeholder,
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
// 将新标签插入到 headTags 的开头
|
|
71
|
-
data.headTags.unshift(newMetaTag);
|
|
76
|
+
// 如果不存在,在 </head> 前插入
|
|
77
|
+
const newTitleTag = `\n <title>${titlePlaceholder}</title>`;
|
|
78
|
+
html = html.replace('</head>', `${newTitleTag}\n </head>`);
|
|
72
79
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
};
|
|
89
|
-
// 将 title 标签插入到 headTags 的开头
|
|
90
|
-
data.headTags.unshift(newTitleTag);
|
|
91
|
-
}
|
|
92
|
-
// 3. 处理 <link rel="icon"> 标签
|
|
93
|
-
const faviconPlaceholder = this.options.faviconPlaceholder || this.defaultFaviconPlaceholder;
|
|
94
|
-
const iconTagIndex = data.headTags.findIndex((tag) => tag.tagName === 'link' &&
|
|
95
|
-
tag.attributes &&
|
|
96
|
-
(tag.attributes.rel === 'icon' || tag.attributes.rel === 'shortcut icon'));
|
|
97
|
-
if (iconTagIndex !== -1) {
|
|
98
|
-
// 如果存在 icon 标签,更新其 href 为占位符
|
|
99
|
-
data.headTags[iconTagIndex].attributes.href = faviconPlaceholder;
|
|
80
|
+
// 3. 处理 <link rel="icon"> 标签
|
|
81
|
+
const faviconPlaceholder = this.options.faviconPlaceholder || this.defaultFaviconPlaceholder;
|
|
82
|
+
const iconRegex = /<link\s+[^>]*rel=["']?(?:icon|shortcut icon)["']?[^>]*>/i;
|
|
83
|
+
if (iconRegex.test(html)) {
|
|
84
|
+
// 如果存在 icon 标签,替换其 href
|
|
85
|
+
const replaceIconRegex = /(<link\s+[^>]*rel=["']?(?:icon|shortcut icon)["']?[^>]*href=)["'][^"']*["']([^>]*>)/gi;
|
|
86
|
+
html = html.replace(replaceIconRegex, `$1"${faviconPlaceholder}"$2`);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// 如果不存在,在 </head> 前插入
|
|
90
|
+
const newIconTag = `\n <link rel="icon" href="${faviconPlaceholder}">`;
|
|
91
|
+
html = html.replace('</head>', `${newIconTag}\n </head>`);
|
|
92
|
+
}
|
|
93
|
+
data.html = html;
|
|
94
|
+
callback(null, data);
|
|
100
95
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
tagName: 'link',
|
|
105
|
-
voidTag: true,
|
|
106
|
-
attributes: {
|
|
107
|
-
rel: 'icon',
|
|
108
|
-
href: faviconPlaceholder,
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
// 将 icon 标签插入到 headTags
|
|
112
|
-
data.headTags.unshift(newIconTag);
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.error('Error in OgMetaInjectionPlugin:', error);
|
|
98
|
+
callback(error);
|
|
113
99
|
}
|
|
114
|
-
return data;
|
|
115
100
|
});
|
|
116
101
|
}
|
|
117
102
|
catch (error) {
|