@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.
- package/lib/rspack-plugins/slardar-performance-monitor-plugin.d.ts +2 -1
- package/lib/rspack-plugins/slardar-performance-monitor-plugin.js +31 -20
- package/lib/rspack-plugins/view-context-injection-plugin.d.ts +0 -1
- package/lib/rspack-plugins/view-context-injection-plugin.js +24 -14
- package/package.json +1 -1
|
@@ -32,21 +32,35 @@ class SlardarPerformanceMonitorPlugin {
|
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
34
34
|
const hooks = HtmlPlugin.getHooks(compilation);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const snippet = this.options.snippet ??
|
|
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
|
-
|
|
41
|
-
|
|
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
|
|
44
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
73
|
-
window
|
|
74
|
-
bid: '
|
|
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
|
|
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
|
}
|
|
@@ -32,19 +32,29 @@ class ViewContextInjectionPlugin {
|
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
34
34
|
const hooks = HtmlPlugin.getHooks(compilation);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
|
42
|
-
|
|
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
|
-
|
|
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
|
|
58
|
-
return `
|
|
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
|
-
|
|
73
|
+
`;
|
|
64
74
|
}
|