@lark-apaas/fullstack-rspack-preset 1.0.21-alpha.2 → 1.0.21-alpha.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.
package/lib/preset.js CHANGED
@@ -12,6 +12,7 @@ const dev_server_listener_1 = require("./utils/dev-server-listener");
12
12
  const route_parser_plugin_1 = __importDefault(require("./rspack-plugins/route-parser-plugin"));
13
13
  const slardar_performance_monitor_plugin_1 = __importDefault(require("./rspack-plugins/slardar-performance-monitor-plugin"));
14
14
  const view_context_injection_plugin_1 = __importDefault(require("./rspack-plugins/view-context-injection-plugin"));
15
+ const og_meta_injection_plugin_1 = __importDefault(require("./rspack-plugins/og-meta-injection-plugin"));
15
16
  const dev_server_snapdom_proxy_1 = require("./utils/dev-server-snapdom-proxy");
16
17
  function createRecommendRspackConfig(options) {
17
18
  const { isDev = true, enableReactRefresh = isDev, needRoutes = true, clientBasePath = '', publicPath = '', // 静态资源路径
@@ -163,6 +164,8 @@ function createRecommendRspackConfig(options) {
163
164
  new slardar_performance_monitor_plugin_1.default(),
164
165
  // 视图上下文注入插件
165
166
  new view_context_injection_plugin_1.default(),
167
+ // OG Meta 标签注入插件
168
+ new og_meta_injection_plugin_1.default(),
166
169
  // 开发环境下,解析路由
167
170
  isDev && needRoutes &&
168
171
  new route_parser_plugin_1.default({
@@ -281,6 +284,11 @@ function createRecommendRspackConfig(options) {
281
284
  target: `http://localhost:${serverPort}`,
282
285
  changeOrigin: true,
283
286
  },
287
+ {
288
+ context: [`${clientBasePath}/__innerapi__`],
289
+ target: `http://localhost:${serverPort}`,
290
+ changeOrigin: true,
291
+ },
284
292
  {
285
293
  context: (pathname, req) => {
286
294
  // 代理所有请求 HTML 响应的请求(页面路由)
@@ -0,0 +1,17 @@
1
+ interface OgMetaInjectionPluginOptions {
2
+ customTags?: Array<{
3
+ property: string;
4
+ placeholder: string;
5
+ }>;
6
+ titlePlaceholder?: string;
7
+ faviconPlaceholder?: string;
8
+ }
9
+ declare class OgMetaInjectionPlugin {
10
+ private options;
11
+ private defaultOgTags;
12
+ private defaultTitlePlaceholder;
13
+ private defaultFaviconPlaceholder;
14
+ constructor(options?: OgMetaInjectionPluginOptions);
15
+ apply(compiler: any): void;
16
+ }
17
+ export default OgMetaInjectionPlugin;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class OgMetaInjectionPlugin {
4
+ constructor(options) {
5
+ // 默认需要处理的 OG 标签配置
6
+ this.defaultOgTags = [
7
+ { property: 'og:title', placeholder: '{{appName}}' },
8
+ { property: 'og:description', placeholder: '{{appDescription}}' },
9
+ { property: 'og:image', placeholder: '{{appAvatar}}' },
10
+ { property: 'og:url', placeholder: '{{currentUrl}}' },
11
+ ];
12
+ // 默认 title 占位符
13
+ this.defaultTitlePlaceholder = '{{appName}}';
14
+ // 默认 favicon 占位符
15
+ this.defaultFaviconPlaceholder = '{{appAvatar}}';
16
+ this.options = options || {};
17
+ }
18
+ apply(compiler) {
19
+ compiler.hooks.compilation.tap('OgMetaInjectionPlugin', (compilation) => {
20
+ try {
21
+ // 从 compiler.webpack 或 compiler.rspack 获取 HtmlRspackPlugin
22
+ // 这样可以避免在 npm link 环境下的模块解析问题
23
+ let HtmlPlugin;
24
+ try {
25
+ // 尝试从 compiler 中获取 rspack
26
+ const rspack = compiler.webpack || compiler.rspack || compiler.constructor.webpack;
27
+ if (rspack && rspack.HtmlRspackPlugin) {
28
+ HtmlPlugin = rspack.HtmlRspackPlugin;
29
+ }
30
+ else {
31
+ // 降级到 require,但这在 npm link 下可能失败
32
+ try {
33
+ HtmlPlugin = require('html-webpack-plugin');
34
+ }
35
+ catch (e2) {
36
+ console.warn('OgMetaInjectionPlugin: HtmlRspackPlugin not found');
37
+ return;
38
+ }
39
+ }
40
+ }
41
+ catch (e) {
42
+ console.warn('OgMetaInjectionPlugin: Failed to get HtmlRspackPlugin');
43
+ return;
44
+ }
45
+ const hooks = HtmlPlugin.getHooks(compilation);
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>`);
74
+ }
75
+ else {
76
+ // 如果不存在,在 </head> 前插入
77
+ const newTitleTag = `\n <title>${titlePlaceholder}</title>`;
78
+ html = html.replace('</head>', `${newTitleTag}\n </head>`);
79
+ }
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);
95
+ }
96
+ catch (error) {
97
+ console.error('Error in OgMetaInjectionPlugin:', error);
98
+ callback(error);
99
+ }
100
+ });
101
+ }
102
+ catch (error) {
103
+ console.error('Error in OgMetaInjectionPlugin:', error);
104
+ }
105
+ });
106
+ }
107
+ }
108
+ exports.default = OgMetaInjectionPlugin;
@@ -70,5 +70,13 @@ function getViewContextScriptContent() {
70
70
  window.userId = "{{userId}}";
71
71
  window.tenantId = "{{tenantId}}";
72
72
  window.appId = "{{appId}}";
73
+ const appInfo = {
74
+ name: "{{appName}}",
75
+ avatar: "{{appAvatar}}",
76
+ description: "{{appDescription}}",
77
+ };
78
+ if (appInfo.name) {
79
+ window._appInfo = appInfo;
80
+ }
73
81
  `;
74
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/fullstack-rspack-preset",
3
- "version": "1.0.21-alpha.2",
3
+ "version": "1.0.21-alpha.3",
4
4
  "files": [
5
5
  "lib",
6
6
  "patches",
@@ -31,7 +31,7 @@
31
31
  "@babel/parser": "^7.28.0",
32
32
  "@babel/traverse": "^7.28.0",
33
33
  "@babel/types": "^7.28.2",
34
- "@lark-apaas/devtool-kits": "^1.2.5",
34
+ "@lark-apaas/devtool-kits": "1.2.8-alpha.1",
35
35
  "@lark-apaas/miaoda-inspector-babel-plugin": "^1.0.0",
36
36
  "@lark-apaas/miaoda-inspector-jsx-runtime": "^1.0.0",
37
37
  "@rspack/plugin-react-refresh": "^1.5.1",