@hyacine/helper 0.0.1

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.
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Runtime API for Hyacine Plugin System
3
+ *
4
+ * This module provides runtime utilities that are available to runtime-only plugins.
5
+ * The actual implementation is generated by the plugin system at build time.
6
+ *
7
+ * Usage in runtime-only plugins:
8
+ * ```typescript
9
+ * import { getInjectPointSelector } from '@hyacine/helper/runtime';
10
+ *
11
+ * export function init(options: Record<string, unknown>) {
12
+ * const selector = getInjectPointSelector('footer');
13
+ * const element = document.querySelector(selector);
14
+ * console.log('Plugin initialized with options:', options);
15
+ * // ...
16
+ * }
17
+ * ```
18
+ */
19
+ /**
20
+ * Runtime-only 插件的 init 函数类型签名
21
+ * @param options - 从编译时传递到运行时的配置选项,由插件系统在生成时序列化
22
+ */
23
+ export type PluginInitFunction<O> = (options: O) => void;
24
+
25
+ /**
26
+ * 获取注入点对应的 CSS 选择器
27
+ * @param injectPoint - 注入点名称
28
+ * @returns CSS 选择器字符串
29
+ * @throws {Error} 如果映射表中不包含该 injectPoint
30
+ */
31
+ export declare function getInjectPointSelector(injectPoint: string): string;
32
+
33
+ /**
34
+ * Script 标签配置选项
35
+ */
36
+ export interface ScriptOptions {
37
+ /**
38
+ * script 标签的 src 属性(外部脚本)
39
+ */
40
+ src?: string;
41
+ /**
42
+ * script 标签的内联内容(内联脚本)
43
+ */
44
+ content?: string;
45
+ /**
46
+ * script 标签的类型,默认为 "text/javascript"
47
+ */
48
+ type?: string;
49
+ /**
50
+ * 是否异步加载脚本
51
+ */
52
+ async?: boolean;
53
+ /**
54
+ * 是否延迟加载脚本
55
+ */
56
+ defer?: boolean;
57
+ /**
58
+ * 跨域设置
59
+ */
60
+ crossOrigin?: "anonymous" | "use-credentials";
61
+ /**
62
+ * 脚本的完整性校验值(Subresource Integrity)
63
+ */
64
+ integrity?: string;
65
+ /**
66
+ * 其他自定义属性
67
+ */
68
+ attributes?: Record<string, string>;
69
+ }
70
+
71
+ /**
72
+ * Link CSS 标签配置选项
73
+ */
74
+ export interface LinkCSSOptions {
75
+ /**
76
+ * link 标签的 href 属性(CSS 文件路径)
77
+ */
78
+ href: string;
79
+ /**
80
+ * 跨域设置
81
+ */
82
+ crossOrigin?: "anonymous" | "use-credentials";
83
+ /**
84
+ * 资源的完整性校验值(Subresource Integrity)
85
+ */
86
+ integrity?: string;
87
+ /**
88
+ * 媒体查询条件
89
+ */
90
+ media?: string;
91
+ /**
92
+ * 其他自定义属性
93
+ */
94
+ attributes?: Record<string, string>;
95
+ }
96
+
97
+ /**
98
+ * 向 head 或 body 注入 script 标签
99
+ *
100
+ * @param options - Script 标签配置选项
101
+ * @param target - 注入目标,默认为 'head'
102
+ * @returns 创建的 script 元素
103
+ * @throws {Error} 如果既没有提供 src 也没有提供 content
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * // 注入外部脚本到 head
108
+ * injectScript({
109
+ * src: 'https://example.com/script.js',
110
+ * async: true
111
+ * });
112
+ *
113
+ * // 注入内联脚本到 body
114
+ * injectScript({
115
+ * content: 'console.log("Hello from inline script");'
116
+ * }, 'body');
117
+ *
118
+ * // 注入带有完整性校验的外部脚本
119
+ * injectScript({
120
+ * src: 'https://cdn.example.com/library.js',
121
+ * integrity: 'sha384-xxx',
122
+ * crossOrigin: 'anonymous'
123
+ * });
124
+ * ```
125
+ */
126
+ export declare function injectScript(
127
+ options: ScriptOptions,
128
+ target?: "head" | "body",
129
+ ): HTMLScriptElement;
130
+
131
+ /**
132
+ * 向 head 注入 link CSS 标签
133
+ *
134
+ * @param options - Link CSS 标签配置选项
135
+ * @returns 创建的 link 元素
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * // 注入外部 CSS
140
+ * injectLinkCSS({
141
+ * href: 'https://example.com/style.css'
142
+ * });
143
+ *
144
+ * // 注入带有媒体查询的 CSS
145
+ * injectLinkCSS({
146
+ * href: '/mobile.css',
147
+ * media: '(max-width: 768px)'
148
+ * });
149
+ *
150
+ * // 注入带有完整性校验的 CSS
151
+ * injectLinkCSS({
152
+ * href: 'https://cdn.example.com/library.css',
153
+ * integrity: 'sha384-xxx',
154
+ * crossOrigin: 'anonymous'
155
+ * });
156
+ * ```
157
+ */
158
+ export declare function injectLinkCSS(options: LinkCSSOptions): HTMLLinkElement;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Integration tests for runtime API
3
+ *
4
+ * This file ensures that the runtime module correctly re-exports
5
+ * all functionality from its sub-modules.
6
+ */
7
+
8
+ import { describe, expect, test } from "bun:test";
9
+ import type { PluginInitFunction } from "./runtime";
10
+ import { getInjectPointSelector, injectLinkCSS, injectScript } from "./runtime";
11
+
12
+ describe("Runtime API re-exports", () => {
13
+ test("应该正确导出 PluginInitFunction 类型", () => {
14
+ // 类型测试 - 确保类型可以正常使用
15
+ const validInit: PluginInitFunction = (options) => {
16
+ console.log(options);
17
+ };
18
+ expect(typeof validInit).toBe("function");
19
+ });
20
+
21
+ test("应该正确导出 getInjectPointSelector", () => {
22
+ expect(typeof getInjectPointSelector).toBe("function");
23
+ expect(getInjectPointSelector.name).toBe("getInjectPointSelector");
24
+ });
25
+
26
+ test("应该正确导出 injectScript", () => {
27
+ expect(typeof injectScript).toBe("function");
28
+ expect(injectScript.name).toBe("injectScript");
29
+ });
30
+
31
+ test("应该正确导出 injectLinkCSS", () => {
32
+ expect(typeof injectLinkCSS).toBe("function");
33
+ expect(injectLinkCSS.name).toBe("injectLinkCSS");
34
+ });
35
+ });
package/src/runtime.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Runtime API for Hyacine Plugin System
3
+ *
4
+ * This module provides runtime utilities that are available to runtime-only plugins.
5
+ *
6
+ * Note: In production, these functions will use the global inject points map
7
+ * that is initialized by the generated runtime file.
8
+ */
9
+
10
+ /**
11
+ * Runtime-only 插件的 init 函数类型签名
12
+ * @param options - 从编译时传递到运行时的配置选项,由插件系统在生成时序列化
13
+ */
14
+ export type PluginInitFunction<O = Record<string, unknown>> = (options: O) => void;
15
+
16
+ // Re-export inject point API
17
+ export { getInjectPointSelector } from "./inject-point";
18
+
19
+ // Re-export DOM injection API
20
+ export { injectLinkCSS, injectScript, type LinkCSSOptions, type ScriptOptions } from "./dom-inject";