@baojiliu/iris-plugin-client 0.0.0-stage → 0.1.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.
- package/README.md +4 -2
- package/dist/index.d.ts +68 -0
- package/dist/index.js +6 -0
- package/dist/sharedRuntime.d.ts +5 -0
- package/dist/sharedRuntime.js +36 -0
- package/dist/vite.d.ts +25 -0
- package/dist/vite.js +69 -0
- package/package.json +41 -4
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @baojiliu/iris-plugin-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Iris 前端插件与宿主共用的契约。插件从这里导入 `Context`,不在插件里再声明一份。宿主实现 `slots`、`sidebar` 和 `locale`,并在 `inject` 列出的服务就绪后调用 `apply(ctx)`。
|
|
4
|
+
|
|
5
|
+
`@baojiliu/iris-plugin-client/vite` 导出 `irisPlugin()`。插件构建用它把 `react`、`react-dom`、`react/jsx-runtime` 换成宿主提供的同一份,并按 `peerDependencies` 写入口上的 `peers`。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract shared by the Iris host and frontend plugins.
|
|
3
|
+
* Plugins depend on this package; they do not declare Context themselves.
|
|
4
|
+
*/
|
|
5
|
+
export type Dispose = () => void;
|
|
6
|
+
/** Services a plugin may list in `inject`. The loader waits until each one exists. */
|
|
7
|
+
export declare const serviceNames: readonly ["slots", "sidebar", "locale"];
|
|
8
|
+
export type ServiceName = (typeof serviceNames)[number];
|
|
9
|
+
export type Translate = (key: string) => string;
|
|
10
|
+
export interface LocaleMessages {
|
|
11
|
+
zh: Record<string, string>;
|
|
12
|
+
en: Record<string, string>;
|
|
13
|
+
}
|
|
14
|
+
export interface Locale {
|
|
15
|
+
register(namespace: string, messages: LocaleMessages): Dispose;
|
|
16
|
+
bind(namespace: string): Translate;
|
|
17
|
+
}
|
|
18
|
+
export interface MainPanelProps {
|
|
19
|
+
pinned: boolean;
|
|
20
|
+
t: Translate;
|
|
21
|
+
}
|
|
22
|
+
export interface MainSlotMeta {
|
|
23
|
+
name: "main";
|
|
24
|
+
key: string;
|
|
25
|
+
locale: string;
|
|
26
|
+
inject: () => Record<string, never>;
|
|
27
|
+
}
|
|
28
|
+
export interface SidebarSlotMeta {
|
|
29
|
+
name: "sidebar.panellist";
|
|
30
|
+
id: string;
|
|
31
|
+
order?: number;
|
|
32
|
+
locale: string;
|
|
33
|
+
/** Host calls this on each sidebar render. */
|
|
34
|
+
label: () => string;
|
|
35
|
+
}
|
|
36
|
+
export interface SidebarIconProps {
|
|
37
|
+
size: number;
|
|
38
|
+
}
|
|
39
|
+
export interface SidebarPinEvent {
|
|
40
|
+
id: string;
|
|
41
|
+
pinned: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* React slots. `inject` waits until the host has declared the slot, then runs `register`.
|
|
45
|
+
* The function returned from `register` removes that contribution.
|
|
46
|
+
*/
|
|
47
|
+
export interface Slots {
|
|
48
|
+
inject(name: MainSlotMeta["name"] | SidebarSlotMeta["name"], register: () => Dispose): Dispose;
|
|
49
|
+
register(meta: MainSlotMeta, render: (props: MainPanelProps) => unknown): Dispose;
|
|
50
|
+
register(meta: SidebarSlotMeta, render: (props: SidebarIconProps) => unknown): Dispose;
|
|
51
|
+
}
|
|
52
|
+
/** Read-only view of sidebar pin state. Changes arrive through `ctx.on("sidebar/pin")`. */
|
|
53
|
+
export interface Sidebar {
|
|
54
|
+
pinned(id: string): boolean;
|
|
55
|
+
}
|
|
56
|
+
export interface Context {
|
|
57
|
+
slots: Slots;
|
|
58
|
+
sidebar: Sidebar;
|
|
59
|
+
locale: Locale;
|
|
60
|
+
on(name: "sidebar/pin", handler: (event: SidebarPinEvent) => void): Dispose;
|
|
61
|
+
/** Runs `setup` now. If `setup` returns a function, the host calls it when the plugin unloads. */
|
|
62
|
+
effect(setup: () => void | Dispose): void;
|
|
63
|
+
}
|
|
64
|
+
export interface PluginModule {
|
|
65
|
+
inject: readonly ServiceName[];
|
|
66
|
+
apply(ctx: Context): void;
|
|
67
|
+
peers?: Record<string, string>;
|
|
68
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract shared by the Iris host and frontend plugins.
|
|
3
|
+
* Plugins depend on this package; they do not declare Context themselves.
|
|
4
|
+
*/
|
|
5
|
+
/** Services a plugin may list in `inject`. The loader waits until each one exists. */
|
|
6
|
+
export const serviceNames = ["slots", "sidebar", "locale"];
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type SharedField = "react" | "reactDOM" | "jsxRuntime";
|
|
2
|
+
export declare function sharedModuleSource(field: SharedField, exportNames: readonly string[]): string;
|
|
3
|
+
export declare function peersExportSource(peerDependencies: unknown): string | null;
|
|
4
|
+
export declare function appendPeersExport(code: string, peersSource: string): string;
|
|
5
|
+
export declare function sourceExportsPeers(code: string): boolean;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const identifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
2
|
+
export function sharedModuleSource(field, exportNames) {
|
|
3
|
+
const named = exportNames
|
|
4
|
+
.filter((name) => name !== "default" && name !== "__esModule" && identifier.test(name))
|
|
5
|
+
.map((name) => `export const ${name} = api.${name}`)
|
|
6
|
+
.join("\n");
|
|
7
|
+
return [
|
|
8
|
+
"const runtime = globalThis.__IRIS_PLUGIN_SHARED__",
|
|
9
|
+
`if (!runtime || runtime.${field} == null) {`,
|
|
10
|
+
' throw new Error("iris plugin shared runtime missing")',
|
|
11
|
+
"}",
|
|
12
|
+
`const api = runtime.${field}`,
|
|
13
|
+
named,
|
|
14
|
+
"export default api",
|
|
15
|
+
"",
|
|
16
|
+
].join("\n");
|
|
17
|
+
}
|
|
18
|
+
export function peersExportSource(peerDependencies) {
|
|
19
|
+
if (peerDependencies == null)
|
|
20
|
+
return null;
|
|
21
|
+
if (typeof peerDependencies !== "object" || Array.isArray(peerDependencies))
|
|
22
|
+
return null;
|
|
23
|
+
const peers = {};
|
|
24
|
+
for (const [key, value] of Object.entries(peerDependencies)) {
|
|
25
|
+
if (typeof value === "string")
|
|
26
|
+
peers[key] = value;
|
|
27
|
+
}
|
|
28
|
+
return `export const peers = ${JSON.stringify(peers)}`;
|
|
29
|
+
}
|
|
30
|
+
export function appendPeersExport(code, peersSource) {
|
|
31
|
+
return `${code.trimEnd()}\n${peersSource}\n`;
|
|
32
|
+
}
|
|
33
|
+
export function sourceExportsPeers(code) {
|
|
34
|
+
return (/export\s+(?:const|let|var|function|class)\s+peers\b/.test(code) ||
|
|
35
|
+
/export\s*\{[^}]*\bpeers\b/.test(code));
|
|
36
|
+
}
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
type ResolveFn = (source: string, importer?: string, options?: {
|
|
2
|
+
skipSelf?: boolean;
|
|
3
|
+
}) => Promise<{
|
|
4
|
+
id: string;
|
|
5
|
+
} | null>;
|
|
6
|
+
export declare function irisPlugin(): {
|
|
7
|
+
name: string;
|
|
8
|
+
enforce: "pre";
|
|
9
|
+
configResolved(config: {
|
|
10
|
+
root: string;
|
|
11
|
+
build: {
|
|
12
|
+
rollupOptions: {
|
|
13
|
+
input?: unknown;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
}): void;
|
|
17
|
+
resolveId(id: string): string | null;
|
|
18
|
+
load(this: {
|
|
19
|
+
resolve: ResolveFn;
|
|
20
|
+
}, id: string): Promise<string | null>;
|
|
21
|
+
transform(this: {
|
|
22
|
+
error: (message: string) => never;
|
|
23
|
+
}, code: string, id: string): string | null;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
import { appendPeersExport, peersExportSource, sharedModuleSource, sourceExportsPeers, } from "./sharedRuntime.js";
|
|
13
|
+
const sharedFields = {
|
|
14
|
+
react: "react",
|
|
15
|
+
"react-dom": "reactDOM",
|
|
16
|
+
"react/jsx-runtime": "jsxRuntime",
|
|
17
|
+
};
|
|
18
|
+
const prefix = "\0iris-shared:";
|
|
19
|
+
export function irisPlugin() {
|
|
20
|
+
let entryFile = "";
|
|
21
|
+
let peersSource = null;
|
|
22
|
+
return {
|
|
23
|
+
name: "iris-shared-runtime",
|
|
24
|
+
enforce: "pre",
|
|
25
|
+
configResolved(config) {
|
|
26
|
+
const input = config.build.rollupOptions.input;
|
|
27
|
+
if (typeof input !== "string") {
|
|
28
|
+
throw new Error("irisPlugin requires rollupOptions.input to be a string");
|
|
29
|
+
}
|
|
30
|
+
entryFile = input;
|
|
31
|
+
const pkgPath = path.join(config.root, "package.json");
|
|
32
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
33
|
+
peersSource = peersExportSource(pkg.peerDependencies);
|
|
34
|
+
},
|
|
35
|
+
resolveId(id) {
|
|
36
|
+
if (Object.prototype.hasOwnProperty.call(sharedFields, id))
|
|
37
|
+
return prefix + id;
|
|
38
|
+
return null;
|
|
39
|
+
},
|
|
40
|
+
async load(id) {
|
|
41
|
+
if (!id.startsWith(prefix))
|
|
42
|
+
return null;
|
|
43
|
+
const specifier = id.slice(prefix.length);
|
|
44
|
+
const field = sharedFields[specifier];
|
|
45
|
+
if (!field)
|
|
46
|
+
return null;
|
|
47
|
+
const resolved = await this.resolve(specifier, entryFile, { skipSelf: true });
|
|
48
|
+
if (!resolved) {
|
|
49
|
+
throw new Error(`iris plugin cannot resolve shared module ${specifier}`);
|
|
50
|
+
}
|
|
51
|
+
const nested = resolved.meta?.commonjs?.resolved?.id;
|
|
52
|
+
const fileId = (nested ?? resolved.id).replace(/^\0/, "").split("?")[0];
|
|
53
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(fileId).href)));
|
|
54
|
+
const names = Object.keys(mod);
|
|
55
|
+
return sharedModuleSource(field, names);
|
|
56
|
+
},
|
|
57
|
+
transform(code, id) {
|
|
58
|
+
const clean = id.split("?")[0];
|
|
59
|
+
if (!entryFile || path.resolve(clean) !== path.resolve(entryFile))
|
|
60
|
+
return null;
|
|
61
|
+
if (sourceExportsPeers(code)) {
|
|
62
|
+
this.error("plugin source already exports peers");
|
|
63
|
+
}
|
|
64
|
+
if (!peersSource)
|
|
65
|
+
return null;
|
|
66
|
+
return appendPeersExport(code, peersSource);
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baojiliu/iris-plugin-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Iris 前端插件与宿主共用的契约,以及插件构建时使用的 Vite 插件。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/dsers/iris-plugin-client.git"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./vite": {
|
|
17
|
+
"types": "./dist/vite.d.ts",
|
|
18
|
+
"default": "./dist/vite.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.build.json",
|
|
27
|
+
"prepublishOnly": "npm run build",
|
|
28
|
+
"test": "node --test src/sharedRuntime.test.ts src/vite.test.ts"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^22.20.7",
|
|
35
|
+
"@types/react": "^19.0.0",
|
|
36
|
+
"@types/react-dom": "^19.0.0",
|
|
37
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
38
|
+
"react": "^19.0.0",
|
|
39
|
+
"react-dom": "^19.0.0",
|
|
40
|
+
"typescript": "~5.7.2",
|
|
41
|
+
"vite": "^6.0.0"
|
|
42
|
+
}
|
|
43
|
+
}
|