@lark-apaas/fullstack-rspack-preset 0.1.2-alpha.5 → 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.
@@ -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
  }
@@ -1,5 +1,4 @@
1
1
  interface ViewContextInjectionPluginOptions {
2
- position?: 'head-end' | 'body-start' | 'body-end';
3
2
  }
4
3
  declare class ViewContextInjectionPlugin {
5
4
  private options;
@@ -32,19 +32,29 @@ class ViewContextInjectionPlugin {
32
32
  return;
33
33
  }
34
34
  const hooks = HtmlPlugin.getHooks(compilation);
35
- hooks.beforeEmit.tapAsync('ViewContextInjectionPlugin', (data, cb) => {
36
- const position = this.options.position ?? 'head-end';
37
- const snippet = getViewContextScript();
38
- if (position === 'body-end') {
39
- data.html = data.html.replace('</body>', `${snippet}\n</body>`);
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);
40
52
  }
41
- else if (position === 'body-start') {
42
- data.html = data.html.replace('<body>', `<body>\n${snippet}`);
43
- }
44
- else if (position === 'head-end') {
45
- data.html = data.html.replace('</head>', `${snippet}\n</head>`);
53
+ else {
54
+ // 如果没找到 main.js,插入到 headTags 最前面
55
+ data.headTags.unshift(viewContextTag);
46
56
  }
47
- cb(null, data);
57
+ return data;
48
58
  });
49
59
  }
50
60
  catch (error) {
@@ -54,11 +64,11 @@ class ViewContextInjectionPlugin {
54
64
  }
55
65
  }
56
66
  exports.default = ViewContextInjectionPlugin;
57
- function getViewContextScript() {
58
- return ` <script>
67
+ function getViewContextScriptContent() {
68
+ return `
59
69
  window.csrfToken = "{{csrfToken}}";
60
70
  window.userId = "{{userId}}";
61
71
  window.tenantId = "{{tenantId}}";
62
72
  window.appId = "{{appId}}";
63
- </script>`;
73
+ `;
64
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.5",
3
+ "version": "0.1.2-alpha.6",
4
4
  "files": [
5
5
  "lib"
6
6
  ],