@pactor-app/vite-plugin 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pactor Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # @pactor-app/vite-plugin
2
+
3
+ [Pactor](https://github.com/426-330/pactor) 的 Vite 插件:把 `.page.yaml` / `.fragment.yaml` DSL 文件在构建期转换为 JS 模块(Parse → Validate → Compile),支持 HMR。校验复用 @pactor-app/schema 的 `validatePageDsl`。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @pactor-app/vite-plugin
9
+ # 或
10
+ pnpm add @pactor-app/vite-plugin
11
+ ```
12
+
13
+ `vite >= 5` 为 peer dependency。
14
+
15
+ ## 用法
16
+
17
+ ```ts
18
+ // vite.config.ts
19
+ import { defineConfig } from 'vite';
20
+ import pactor from '@pactor-app/vite-plugin';
21
+
22
+ export default defineConfig({
23
+ plugins: [pactor()],
24
+ });
25
+ ```
26
+
27
+ 之后即可直接 import YAML DSL 文件:
28
+
29
+ ```ts
30
+ import page from './user-list.page.yaml';
31
+ import { dslVersion } from './user-list.page.yaml';
32
+
33
+ console.log(page.page.id, dslVersion);
34
+ ```
35
+
36
+ 约定:
37
+
38
+ - 默认仅匹配 `*.page.yaml/.yml` 与 `*.fragment.yaml/.yml`(不误伤普通 yaml 配置)。
39
+ - `*.page.*` 走 `validatePageDsl` 完整 Schema 校验;`*.fragment.*` 做结构宽松检查(存在 `dsl` 与 `body`)。
40
+ - 校验失败或 YAML 语法错误(带行列位置)使构建报错,错误信息含文件标识与每条 `path: message (keyword)`。
41
+ - dev 时修改 DSL 文件触发热更新;无模块记录时全量刷新。
42
+
43
+ ## 选项
44
+
45
+ ```ts
46
+ pactor({
47
+ // 自定义匹配规则(覆盖默认后缀约定)
48
+ include: /\.dsl\.yaml$/,
49
+ // 关闭构建期 Schema 校验(默认 true),仅编译
50
+ validate: false,
51
+ });
52
+ ```
53
+
54
+ 自定义 `include` 时,文件名含 `.fragment.` 的按 FragmentDsl 处理,其余按 PageDsl 处理。
55
+
56
+ ## TypeScript 模块声明
57
+
58
+ 在业务工程中添加 `dsl-modules.d.ts`,让 TS 识别 YAML 模块:
59
+
60
+ ```ts
61
+ declare module '*.page.yaml' {
62
+ import type { PageDsl } from '@pactor-app/core';
63
+ const page: PageDsl;
64
+ export const dslVersion: string;
65
+ export default page;
66
+ }
67
+
68
+ declare module '*.fragment.yaml' {
69
+ import type { FragmentDsl } from '@pactor-app/core';
70
+ const fragment: FragmentDsl;
71
+ export const dslVersion: string;
72
+ export default fragment;
73
+ }
74
+ ```
75
+
76
+ ## 与 StaticDslLoader 组合
77
+
78
+ 构建期 import 的 DSL 可喂给 @pactor-app/resource 的 `StaticDslLoader`(examples / 单测 / 本地 DSL 开发):
79
+
80
+ ```ts
81
+ import { StaticDslLoader } from '@pactor-app/resource';
82
+ import userListPage from './user-list.page.yaml';
83
+
84
+ const loader = new StaticDslLoader({
85
+ pages: { 'user-list': userListPage },
86
+ });
87
+ ```
88
+
89
+ 注意:主推荐模式仍是 Runtime API 动态加载(`HttpDslResourceLoader` + 服务端 DSL 端点),本插件适用于本地开发、示例与测试场景。
90
+
91
+ ## 文档
92
+
93
+ 完整文档见 <https://github.com/426-330/pactor/tree/main/docs>(`pnpm docs:dev` 本地启动文档站)。
94
+
95
+ ## License
96
+
97
+ [MIT](../../LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ default: () => index_default,
24
+ pactor: () => pactor
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_yaml = require("yaml");
28
+ var import_schema = require("@pactor-app/schema");
29
+ var DEFAULT_INCLUDE = /\.(page|fragment)\.(yaml|yml)$/;
30
+ var FRAGMENT_RE = /\.fragment\.(yaml|yml)$/;
31
+ function toIncludeList(include) {
32
+ if (include === void 0) return [DEFAULT_INCLUDE];
33
+ return Array.isArray(include) ? include : [include];
34
+ }
35
+ function pactor(options = {}) {
36
+ const includeList = toIncludeList(options.include);
37
+ const shouldValidate = options.validate !== false;
38
+ const matches = (id) => includeList.some((re) => re.test(id));
39
+ return {
40
+ name: "pactor",
41
+ enforce: "pre",
42
+ transform(code, id) {
43
+ if (!matches(id)) return null;
44
+ const doc = (0, import_yaml.parseDocument)(code);
45
+ if (doc.errors.length > 0) {
46
+ const details = doc.errors.map((error) => {
47
+ const pos = error.linePos?.[0];
48
+ const where = pos ? `\u884C ${pos.line} \u5217 ${pos.col}` : "\u672A\u77E5\u4F4D\u7F6E";
49
+ return `${where}: ${error.message}`;
50
+ }).join("\n ");
51
+ this.error(`[pactor] YAML \u8BED\u6CD5\u9519\u8BEF ${id}:
52
+ ${details}`);
53
+ }
54
+ const data = doc.toJS();
55
+ const isFragment = FRAGMENT_RE.test(id);
56
+ if (isFragment) {
57
+ if (shouldValidate) {
58
+ const fragment = data;
59
+ if (fragment === null || typeof fragment !== "object" || fragment.dsl === void 0 || fragment.body === void 0) {
60
+ this.error(
61
+ `[pactor] Fragment DSL \u7ED3\u6784\u4E0D\u5B8C\u6574 ${id}:
62
+ /: \u5FC5\u987B\u5B58\u5728 dsl \u4E0E body \u5B57\u6BB5 (required)`
63
+ );
64
+ }
65
+ }
66
+ const json2 = JSON.stringify(data);
67
+ return {
68
+ code: `const fragment = ${json2};
69
+ export const dslVersion = fragment.dsl;
70
+ export default fragment;
71
+ `,
72
+ map: null
73
+ };
74
+ }
75
+ if (shouldValidate) {
76
+ const result = (0, import_schema.validatePageDsl)(data);
77
+ if (!result.valid) {
78
+ const details = result.errors.map((error) => `${error.path}: ${error.message} (${error.keyword})`).join("\n ");
79
+ this.error(`[pactor] Page DSL \u6821\u9A8C\u5931\u8D25 ${id}:
80
+ ${details}`);
81
+ }
82
+ }
83
+ const json = JSON.stringify(data);
84
+ return {
85
+ code: `const page = ${json};
86
+ export const dslVersion = page.dsl;
87
+ export default page;
88
+ `,
89
+ map: null
90
+ };
91
+ },
92
+ handleHotUpdate({ file, server, modules }) {
93
+ if (!matches(file)) return void 0;
94
+ if (modules.length > 0) return modules;
95
+ server.ws.send({ type: "full-reload" });
96
+ return [];
97
+ }
98
+ };
99
+ }
100
+ var index_default = pactor;
101
+ // Annotate the CommonJS export names for ESM import in node:
102
+ 0 && (module.exports = {
103
+ pactor
104
+ });
@@ -0,0 +1,22 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /** 插件选项 */
4
+ interface PactorPluginOptions {
5
+ /**
6
+ * 自定义匹配规则(覆盖默认的 `.page./.fragment.` 后缀约定)。
7
+ * 自定义规则下文件名含 `.fragment.` 的按 FragmentDsl 处理,其余按 PageDsl 处理。
8
+ */
9
+ include?: RegExp | RegExp[];
10
+ /** 是否执行 Schema 校验(默认 true;false 时仅编译不校验) */
11
+ validate?: boolean;
12
+ }
13
+ /**
14
+ * 创建 Pactor Vite 插件。
15
+ *
16
+ * 转换管线:YAML 解析(语法错误带位置)→ page 走 `validatePageDsl` 完整校验 /
17
+ * fragment 宽松检查(存在 `dsl` 与 `body`)→ 输出
18
+ * `export default <DSL 对象>` 与 `export const dslVersion`。
19
+ */
20
+ declare function pactor(options?: PactorPluginOptions): Plugin;
21
+
22
+ export { type PactorPluginOptions, pactor as default, pactor };
@@ -0,0 +1,22 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /** 插件选项 */
4
+ interface PactorPluginOptions {
5
+ /**
6
+ * 自定义匹配规则(覆盖默认的 `.page./.fragment.` 后缀约定)。
7
+ * 自定义规则下文件名含 `.fragment.` 的按 FragmentDsl 处理,其余按 PageDsl 处理。
8
+ */
9
+ include?: RegExp | RegExp[];
10
+ /** 是否执行 Schema 校验(默认 true;false 时仅编译不校验) */
11
+ validate?: boolean;
12
+ }
13
+ /**
14
+ * 创建 Pactor Vite 插件。
15
+ *
16
+ * 转换管线:YAML 解析(语法错误带位置)→ page 走 `validatePageDsl` 完整校验 /
17
+ * fragment 宽松检查(存在 `dsl` 与 `body`)→ 输出
18
+ * `export default <DSL 对象>` 与 `export const dslVersion`。
19
+ */
20
+ declare function pactor(options?: PactorPluginOptions): Plugin;
21
+
22
+ export { type PactorPluginOptions, pactor as default, pactor };
package/dist/index.js ADDED
@@ -0,0 +1,79 @@
1
+ // src/index.ts
2
+ import { parseDocument } from "yaml";
3
+ import { validatePageDsl } from "@pactor-app/schema";
4
+ var DEFAULT_INCLUDE = /\.(page|fragment)\.(yaml|yml)$/;
5
+ var FRAGMENT_RE = /\.fragment\.(yaml|yml)$/;
6
+ function toIncludeList(include) {
7
+ if (include === void 0) return [DEFAULT_INCLUDE];
8
+ return Array.isArray(include) ? include : [include];
9
+ }
10
+ function pactor(options = {}) {
11
+ const includeList = toIncludeList(options.include);
12
+ const shouldValidate = options.validate !== false;
13
+ const matches = (id) => includeList.some((re) => re.test(id));
14
+ return {
15
+ name: "pactor",
16
+ enforce: "pre",
17
+ transform(code, id) {
18
+ if (!matches(id)) return null;
19
+ const doc = parseDocument(code);
20
+ if (doc.errors.length > 0) {
21
+ const details = doc.errors.map((error) => {
22
+ const pos = error.linePos?.[0];
23
+ const where = pos ? `\u884C ${pos.line} \u5217 ${pos.col}` : "\u672A\u77E5\u4F4D\u7F6E";
24
+ return `${where}: ${error.message}`;
25
+ }).join("\n ");
26
+ this.error(`[pactor] YAML \u8BED\u6CD5\u9519\u8BEF ${id}:
27
+ ${details}`);
28
+ }
29
+ const data = doc.toJS();
30
+ const isFragment = FRAGMENT_RE.test(id);
31
+ if (isFragment) {
32
+ if (shouldValidate) {
33
+ const fragment = data;
34
+ if (fragment === null || typeof fragment !== "object" || fragment.dsl === void 0 || fragment.body === void 0) {
35
+ this.error(
36
+ `[pactor] Fragment DSL \u7ED3\u6784\u4E0D\u5B8C\u6574 ${id}:
37
+ /: \u5FC5\u987B\u5B58\u5728 dsl \u4E0E body \u5B57\u6BB5 (required)`
38
+ );
39
+ }
40
+ }
41
+ const json2 = JSON.stringify(data);
42
+ return {
43
+ code: `const fragment = ${json2};
44
+ export const dslVersion = fragment.dsl;
45
+ export default fragment;
46
+ `,
47
+ map: null
48
+ };
49
+ }
50
+ if (shouldValidate) {
51
+ const result = validatePageDsl(data);
52
+ if (!result.valid) {
53
+ const details = result.errors.map((error) => `${error.path}: ${error.message} (${error.keyword})`).join("\n ");
54
+ this.error(`[pactor] Page DSL \u6821\u9A8C\u5931\u8D25 ${id}:
55
+ ${details}`);
56
+ }
57
+ }
58
+ const json = JSON.stringify(data);
59
+ return {
60
+ code: `const page = ${json};
61
+ export const dslVersion = page.dsl;
62
+ export default page;
63
+ `,
64
+ map: null
65
+ };
66
+ },
67
+ handleHotUpdate({ file, server, modules }) {
68
+ if (!matches(file)) return void 0;
69
+ if (modules.length > 0) return modules;
70
+ server.ws.send({ type: "full-reload" });
71
+ return [];
72
+ }
73
+ };
74
+ }
75
+ var index_default = pactor;
76
+ export {
77
+ index_default as default,
78
+ pactor
79
+ };
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@pactor-app/vite-plugin",
3
+ "version": "0.1.0",
4
+ "description": "Pactor Vite plugin: import .page.yaml / .fragment.yaml DSL files as JS modules with build-time validation and HMR",
5
+ "keywords": [
6
+ "pactor",
7
+ "dsl",
8
+ "server-driven-ui",
9
+ "vite",
10
+ "vite-plugin",
11
+ "yaml"
12
+ ],
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/426-330/pactor.git",
17
+ "directory": "packages/vite-plugin"
18
+ },
19
+ "homepage": "https://github.com/426-330/pactor/tree/main/packages/vite-plugin",
20
+ "bugs": "https://github.com/426-330/pactor/issues",
21
+ "type": "module",
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "import": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ },
31
+ "require": {
32
+ "types": "./dist/index.d.cts",
33
+ "default": "./dist/index.cjs"
34
+ }
35
+ }
36
+ },
37
+ "sideEffects": false,
38
+ "engines": {
39
+ "node": ">=18"
40
+ },
41
+ "files": [
42
+ "dist"
43
+ ],
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "dependencies": {
48
+ "yaml": "^2.9.0",
49
+ "@pactor-app/core": "0.1.0",
50
+ "@pactor-app/schema": "0.1.0"
51
+ },
52
+ "peerDependencies": {
53
+ "vite": ">=5"
54
+ },
55
+ "devDependencies": {
56
+ "vite": "^8.2.1"
57
+ },
58
+ "scripts": {
59
+ "build": "tsup",
60
+ "test": "vitest run",
61
+ "typecheck": "tsc --noEmit"
62
+ }
63
+ }