@lark-apaas/fullstack-rspack-preset 0.1.2-alpha.4 → 0.1.2-alpha.6

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
@@ -11,6 +11,7 @@ const plugin_react_refresh_1 = __importDefault(require("@rspack/plugin-react-ref
11
11
  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
+ const view_context_injection_plugin_1 = __importDefault(require("./rspack-plugins/view-context-injection-plugin"));
14
15
  function createRecommendRspackConfig(options) {
15
16
  const { isDev = true, enableReactRefresh = isDev, needRoutes = true, clientBasePath = '', publicPath = '', // 静态资源路径
16
17
  } = options;
@@ -152,6 +153,8 @@ function createRecommendRspackConfig(options) {
152
153
  }),
153
154
  // 性能监控插件
154
155
  new slardar_performance_monitor_plugin_1.default(),
156
+ // 视图上下文注入插件
157
+ new view_context_injection_plugin_1.default(),
155
158
  // 开发环境下,解析路由
156
159
  isDev && needRoutes &&
157
160
  new route_parser_plugin_1.default({
@@ -1,6 +1,7 @@
1
1
  interface SlardarPerformanceMonitorPluginOptions {
2
- position?: 'head-end' | 'body-start' | 'body-end';
3
2
  snippet?: string;
3
+ bid?: string;
4
+ globalName?: string;
4
5
  }
5
6
  declare class SlardarPerformanceMonitorPlugin {
6
7
  private options;
@@ -32,21 +32,35 @@ class SlardarPerformanceMonitorPlugin {
32
32
  return;
33
33
  }
34
34
  const hooks = HtmlPlugin.getHooks(compilation);
35
- hooks.beforeEmit.tapAsync('SlardarPerformanceMonitorPlugin', (data, cb) => {
36
- const position = this.options.position ?? 'head-end';
37
- const snippet = this.options.snippet ?? getSlardarScript();
35
+ // 使用 alterAssetTagGroups 钩子,在标签数组层面操作
36
+ hooks.alterAssetTagGroups.tap('SlardarPerformanceMonitorPlugin', (data) => {
37
+ const snippet = this.options.snippet ?? getSlardarScriptContent({
38
+ bid: this.options.bid,
39
+ globalName: this.options.globalName,
40
+ });
38
41
  if (!snippet)
39
- return;
40
- if (position === 'body-end') {
41
- data.html = data.html.replace('</body>', `${snippet}\n</body>`);
42
+ return data;
43
+ // 创建 Slardar 脚本标签对象
44
+ const slardarTag = {
45
+ tagName: 'script',
46
+ voidTag: false,
47
+ innerHTML: snippet,
48
+ attributes: {},
49
+ };
50
+ // 找到 main.js 的索引位置
51
+ const mainJsIndex = data.headTags.findIndex((tag) => tag.tagName === 'script' &&
52
+ tag.attributes &&
53
+ tag.attributes.src &&
54
+ tag.attributes.src.includes('main.js'));
55
+ if (mainJsIndex !== -1) {
56
+ // 在 main.js 之前插入 Slardar 脚本
57
+ data.headTags.splice(mainJsIndex, 0, slardarTag);
42
58
  }
43
- else if (position === 'body-start') {
44
- data.html = data.html.replace('<body>', `<body>\n${snippet}`);
45
- }
46
- else if (position === 'head-end') {
47
- data.html = data.html.replace('</head>', `${snippet}\n</head>`);
59
+ else {
60
+ // 如果没找到 main.js,插入到 headTags 最前面
61
+ data.headTags.unshift(slardarTag);
48
62
  }
49
- cb(null, data);
63
+ return data;
50
64
  });
51
65
  }
52
66
  catch (error) {
@@ -56,11 +70,10 @@ class SlardarPerformanceMonitorPlugin {
56
70
  }
57
71
  }
58
72
  exports.default = SlardarPerformanceMonitorPlugin;
59
- function getSlardarScript(option = {}) {
73
+ function getSlardarScriptContent(option = {}) {
60
74
  const bid = option.bid || 'apaas_ai';
61
75
  const globalName = option.globalName || 'KSlardarWeb';
62
76
  return `
63
- <script>
64
77
  // 创建 Slardar 脚本元素
65
78
  const slardarScript = document.createElement('script');
66
79
  slardarScript.src = 'https://lf3-short.ibytedapm.com/slardar/fe/sdk-web/browser.cn.js?bid=${bid}&globalName=${globalName}';
@@ -69,13 +82,13 @@ function getSlardarScript(option = {}) {
69
82
  // 添加 onload 事件处理
70
83
  slardarScript.onload = function() {
71
84
  // 脚本加载完成后执行初始化
72
- if (window.KSlardarWeb) {
73
- window.KSlardarWeb('init', {
74
- bid: 'apaas_ai',
85
+ if (window.${globalName}) {
86
+ window.${globalName}('init', {
87
+ bid: '${bid}',
75
88
  // 四种类型:dev/boe/pre/online
76
89
  env: 'online',
77
90
  });
78
- window.KSlardarWeb('start');
91
+ window.${globalName}('start');
79
92
  }
80
93
  };
81
94
 
@@ -91,7 +104,5 @@ function getSlardarScript(option = {}) {
91
104
  const performanceScript = document.createElement('script');
92
105
  performanceScript.src = 'https://sf3-scmcdn-cn.feishucdn.com/obj/unpkg/byted/performance/0.1.0/dist/performance.iife.js';
93
106
  document.head.appendChild(performanceScript);
94
- </script>
95
-
96
107
  `;
97
108
  }
@@ -0,0 +1,8 @@
1
+ interface ViewContextInjectionPluginOptions {
2
+ }
3
+ declare class ViewContextInjectionPlugin {
4
+ private options;
5
+ constructor(options?: ViewContextInjectionPluginOptions);
6
+ apply(compiler: any): void;
7
+ }
8
+ export default ViewContextInjectionPlugin;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ViewContextInjectionPlugin {
4
+ constructor(options) {
5
+ this.options = options || {};
6
+ }
7
+ apply(compiler) {
8
+ compiler.hooks.compilation.tap('ViewContextInjectionPlugin', (compilation) => {
9
+ try {
10
+ // 从 compiler.webpack 或 compiler.rspack 获取 HtmlRspackPlugin
11
+ // 这样可以避免在 npm link 环境下的模块解析问题
12
+ let HtmlPlugin;
13
+ try {
14
+ // 尝试从 compiler 中获取 rspack
15
+ const rspack = compiler.webpack || compiler.rspack || compiler.constructor.webpack;
16
+ if (rspack && rspack.HtmlRspackPlugin) {
17
+ HtmlPlugin = rspack.HtmlRspackPlugin;
18
+ }
19
+ else {
20
+ // 降级到 require,但这在 npm link 下可能失败
21
+ try {
22
+ HtmlPlugin = require('html-webpack-plugin');
23
+ }
24
+ catch (e2) {
25
+ console.warn('ViewContextInjectionPlugin: HtmlRspackPlugin not found');
26
+ return;
27
+ }
28
+ }
29
+ }
30
+ catch (e) {
31
+ console.warn('ViewContextInjectionPlugin: Failed to get HtmlRspackPlugin');
32
+ return;
33
+ }
34
+ const hooks = HtmlPlugin.getHooks(compilation);
35
+ // 使用 alterAssetTagGroups 钩子,在标签数组层面操作
36
+ hooks.alterAssetTagGroups.tap('ViewContextInjectionPlugin', (data) => {
37
+ // 创建 ViewContext 脚本标签对象
38
+ const viewContextTag = {
39
+ tagName: 'script',
40
+ voidTag: false,
41
+ innerHTML: getViewContextScriptContent(),
42
+ attributes: {},
43
+ };
44
+ // 找到 main.js 的索引位置
45
+ const mainJsIndex = data.headTags.findIndex((tag) => tag.tagName === 'script' &&
46
+ tag.attributes &&
47
+ tag.attributes.src &&
48
+ tag.attributes.src.includes('main.js'));
49
+ if (mainJsIndex !== -1) {
50
+ // 在 main.js 之前插入 ViewContext 脚本
51
+ data.headTags.splice(mainJsIndex, 0, viewContextTag);
52
+ }
53
+ else {
54
+ // 如果没找到 main.js,插入到 headTags 最前面
55
+ data.headTags.unshift(viewContextTag);
56
+ }
57
+ return data;
58
+ });
59
+ }
60
+ catch (error) {
61
+ console.error('Error in ViewContextInjectionPlugin:', error);
62
+ }
63
+ });
64
+ }
65
+ }
66
+ exports.default = ViewContextInjectionPlugin;
67
+ function getViewContextScriptContent() {
68
+ return `
69
+ window.csrfToken = "{{csrfToken}}";
70
+ window.userId = "{{userId}}";
71
+ window.tenantId = "{{tenantId}}";
72
+ window.appId = "{{appId}}";
73
+ `;
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/fullstack-rspack-preset",
3
- "version": "0.1.2-alpha.4",
3
+ "version": "0.1.2-alpha.6",
4
4
  "files": [
5
5
  "lib"
6
6
  ],