@joplo-app/server 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/LICENSE +55 -0
- package/dist/build.d.ts +21 -0
- package/dist/build.js +218 -0
- package/dist/contract.d.ts +76 -0
- package/dist/contract.js +1 -0
- package/dist/dev.d.ts +100 -0
- package/dist/dev.js +667 -0
- package/dist/export.d.ts +82 -0
- package/dist/export.js +245 -0
- package/dist/home.d.ts +49 -0
- package/dist/home.js +168 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +3 -0
- package/dist/page-report.js +82 -0
- package/dist/project.d.ts +14 -0
- package/dist/project.js +79 -0
- package/dist/render.d.ts +9 -0
- package/dist/render.js +150 -0
- package/dist/selection.d.ts +35 -0
- package/dist/selection.js +123 -0
- package/dist/vite-shared.d.ts +29 -0
- package/dist/vite-shared.js +100 -0
- package/dist/web-assets.d.ts +35 -0
- package/dist/web-assets.js +127 -0
- package/package.json +53 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
16
|
+
|
|
17
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
18
|
+
|
|
19
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
20
|
+
|
|
21
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
22
|
+
|
|
23
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
24
|
+
|
|
25
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
26
|
+
|
|
27
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
28
|
+
|
|
29
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
30
|
+
|
|
31
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
32
|
+
|
|
33
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
34
|
+
|
|
35
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
36
|
+
|
|
37
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
38
|
+
|
|
39
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
40
|
+
|
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
42
|
+
|
|
43
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
package/dist/build.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Project } from '@joplo-app/core';
|
|
2
|
+
import type { BuildOptions, BuildResult } from './contract.js';
|
|
3
|
+
import type { DistWeb } from './web-assets.js';
|
|
4
|
+
/** 发布站只需要的 IR:去掉创作侧字段(publish 模式下它们本来就为空)。层级与镜头属于演示,保留(D43)。 */
|
|
5
|
+
export declare function publishPayload(project: Project, home: string | null): {
|
|
6
|
+
project: Record<string, unknown>;
|
|
7
|
+
home: string | null;
|
|
8
|
+
};
|
|
9
|
+
/** 把 JSON 安全地放进 <script type="application/json">。 */
|
|
10
|
+
export declare const jsonForHtml: (v: unknown) => string;
|
|
11
|
+
/** 后处理 vite 产出的 HTML:内联 IR、内联入口脚本、去掉 crossorigin(file:// 下 CORS 会失败)。 */
|
|
12
|
+
export declare function finalizeHtml(html: string, readAsset: (rel: string) => string, inline: {
|
|
13
|
+
project: unknown;
|
|
14
|
+
home: string | null;
|
|
15
|
+
}): {
|
|
16
|
+
html: string;
|
|
17
|
+
inlined: string[];
|
|
18
|
+
};
|
|
19
|
+
export declare function buildSite(opts: BuildOptions): Promise<BuildResult>;
|
|
20
|
+
/** 从 npm 安装时:复制预构建的发布站,单独编译作者 className 的 CSS 并在 site.html 里引用。 */
|
|
21
|
+
export declare function stageDistSite(web: DistWeb, projectDir: string, staging: string): Promise<void>;
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `joplo build`:静态发布站。
|
|
3
|
+
*
|
|
4
|
+
* 1. 用 Vite(中间件模式,不监听端口)ssrLoadModule 载入项目,compile(…, 'publish'):只含选定方案,
|
|
5
|
+
* 已去掉规则、情况、问题、备注、源路径(D33)。这里再去掉 IR 中创作侧的空字段与约束、角色。
|
|
6
|
+
* 2. vite build 发布站入口(packages/web/site.html),Tailwind 以项目目录为 @source。
|
|
7
|
+
* 3. 后处理:IR 内联到 index.html;入口脚本内联为 <script type="module">、去掉 crossorigin,
|
|
8
|
+
* 这样直接双击 index.html(file://)也能离线运行。产物是普通静态目录,可 `wrangler deploy --assets` 或任何静态托管发布。
|
|
9
|
+
*
|
|
10
|
+
* 目标目录非空且没有 overwrite 时拒绝写入;永远不清空项目目录本身或它的上级目录。
|
|
11
|
+
*
|
|
12
|
+
* 从 npm 安装时(web-assets.ts 的 dist 形态)第 2 步不编译界面源码:复制 @joplo-app/web 预构建的发布站,
|
|
13
|
+
* 只用 Tailwind 编译作者 className 用到的工具类(project.css → assets/project-*.css),再做第 3 步。
|
|
14
|
+
*/
|
|
15
|
+
import fs from 'node:fs';
|
|
16
|
+
import os from 'node:os';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import tailwindcss from '@tailwindcss/vite';
|
|
19
|
+
import { build as viteBuild, createServer } from 'vite';
|
|
20
|
+
import { JoploError } from '@joplo-app/core';
|
|
21
|
+
import { compileProject } from "./project.js";
|
|
22
|
+
import { joploProjectPlugin, kitAliases, locateWebAssets, realProjectDir } from "./vite-shared.js";
|
|
23
|
+
/** 发布站只需要的 IR:去掉创作侧字段(publish 模式下它们本来就为空)。层级与镜头属于演示,保留(D43)。 */
|
|
24
|
+
export function publishPayload(project, home) {
|
|
25
|
+
const { rules: _r, cases: _c, issues: _i, ...rest } = project;
|
|
26
|
+
const features = Object.fromEntries(Object.entries(project.features).map(([id, f]) => [id, { id: f.id, name: f.name, goal: f.goal, optionIds: f.optionIds, pick: f.pick }]));
|
|
27
|
+
const options = Object.fromEntries(Object.entries(project.options).map(([id, o]) => [id, { id: o.id, featureId: o.featureId, key: o.key, name: o.name, idea: o.idea, routes: o.routes, coverage: {}, layers: o.layers ?? {} }]));
|
|
28
|
+
const scenarios = Object.fromEntries(Object.entries(project.scenarios).map(([id, s]) => {
|
|
29
|
+
const { caseIds: _ci, ...sr } = s;
|
|
30
|
+
return [id, { ...sr, caseIds: [], steps: s.steps.map(({ ruleIds: _ri, ...st }) => ({ ...st, ruleIds: [] })) }];
|
|
31
|
+
}));
|
|
32
|
+
return { project: { ...rest, features, options, scenarios }, home: home && project.routes[home] ? home : null };
|
|
33
|
+
}
|
|
34
|
+
/** 把 JSON 安全地放进 <script type="application/json">。 */
|
|
35
|
+
export const jsonForHtml = (v) => JSON.stringify(v).replace(/</g, '\\u003c').replace(/>/g, '\\u003e').replace(/&/g, '\\u0026').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
|
|
36
|
+
/** 后处理 vite 产出的 HTML:内联 IR、内联入口脚本、去掉 crossorigin(file:// 下 CORS 会失败)。 */
|
|
37
|
+
export function finalizeHtml(html, readAsset, inline) {
|
|
38
|
+
const inlined = [];
|
|
39
|
+
let out = html.replace(/\s+crossorigin(?:="[^"]*")?/g, '');
|
|
40
|
+
out = out.replace(/<link rel="modulepreload"[^>]*>\s*/g, '');
|
|
41
|
+
out = out.replace(/<script type="module" src="\.\/([^"]+)"><\/script>/g, (_m, rel) => {
|
|
42
|
+
inlined.push(rel);
|
|
43
|
+
const code = readAsset(rel).replace(/<\/script/gi, '<\\/script');
|
|
44
|
+
return `<script type="module">${code}</script>`;
|
|
45
|
+
});
|
|
46
|
+
const data = `<script type="application/json" id="joplo-ir">${jsonForHtml(inline)}</script>`;
|
|
47
|
+
out = out.replace('</head>', ` ${data}\n </head>`);
|
|
48
|
+
return { html: out, inlined };
|
|
49
|
+
}
|
|
50
|
+
function listFiles(dir, base = dir) {
|
|
51
|
+
const out = [];
|
|
52
|
+
for (const name of fs.readdirSync(dir).sort()) {
|
|
53
|
+
const p = path.join(dir, name);
|
|
54
|
+
if (fs.statSync(p).isDirectory())
|
|
55
|
+
out.push(...listFiles(p, base));
|
|
56
|
+
else
|
|
57
|
+
out.push(path.relative(base, p).split(path.sep).join('/'));
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
function realish(p) {
|
|
62
|
+
const abs = path.resolve(p);
|
|
63
|
+
const rest = [];
|
|
64
|
+
let cur = abs;
|
|
65
|
+
for (;;) {
|
|
66
|
+
try {
|
|
67
|
+
return path.join(fs.realpathSync(cur), ...rest);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
const parent = path.dirname(cur);
|
|
71
|
+
if (parent === cur)
|
|
72
|
+
return abs;
|
|
73
|
+
rest.unshift(path.basename(cur));
|
|
74
|
+
cur = parent;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export async function buildSite(opts) {
|
|
79
|
+
const projectDir = realProjectDir(opts.projectDir);
|
|
80
|
+
if (!fs.existsSync(projectDir))
|
|
81
|
+
throw new JoploError('NOT_FOUND', `项目目录不存在:${projectDir}`);
|
|
82
|
+
const outDir = realish(opts.outDir ?? path.join(projectDir, 'dist'));
|
|
83
|
+
// 目标目录安全检查(先于任何耗时工作)
|
|
84
|
+
if (outDir === projectDir || projectDir.startsWith(outDir + path.sep) || outDir === os.homedir() || outDir === path.parse(outDir).root)
|
|
85
|
+
throw new JoploError('USAGE', `不能把发布站写到 ${outDir}:它是项目目录本身或它的上级目录`);
|
|
86
|
+
if (outDir === path.join(projectDir, '.joplo') || outDir.startsWith(path.join(projectDir, '.joplo') + path.sep))
|
|
87
|
+
throw new JoploError('USAGE', `不能写入 .joplo/:${outDir}`);
|
|
88
|
+
const nonEmpty = fs.existsSync(outDir) && (!fs.statSync(outDir).isDirectory() || fs.readdirSync(outDir).length > 0);
|
|
89
|
+
if (nonEmpty && !opts.overwrite)
|
|
90
|
+
throw new JoploError('USAGE', `目标目录非空:${outDir}。确认可以清空后请传 overwrite: true(CLI:--overwrite)`);
|
|
91
|
+
if (nonEmpty && fs.existsSync(path.join(outDir, 'joplo.config.ts')))
|
|
92
|
+
throw new JoploError('USAGE', `目标目录看起来是一个 Joplo 项目(含 joplo.config.ts),拒绝清空:${outDir}`);
|
|
93
|
+
const web = locateWebAssets();
|
|
94
|
+
// 1. 编译(publish)
|
|
95
|
+
const loader = await createServer({
|
|
96
|
+
configFile: false,
|
|
97
|
+
envDir: false,
|
|
98
|
+
root: projectDir,
|
|
99
|
+
logLevel: 'error',
|
|
100
|
+
appType: 'custom',
|
|
101
|
+
resolve: { alias: kitAliases(web, projectDir) },
|
|
102
|
+
plugins: [joploProjectPlugin(projectDir, web)],
|
|
103
|
+
server: { middlewareMode: true, hmr: false, watch: null },
|
|
104
|
+
optimizeDeps: { noDiscovery: true, include: [] },
|
|
105
|
+
});
|
|
106
|
+
let compiled;
|
|
107
|
+
try {
|
|
108
|
+
compiled = await compileProject(loader, projectDir, 'publish');
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
await loader.close();
|
|
112
|
+
}
|
|
113
|
+
if (!compiled.project) {
|
|
114
|
+
const errs = compiled.diagnostics.filter((d) => d.severity === 'error');
|
|
115
|
+
throw new JoploError('EXPORT_FAILED', `项目编译失败,未生成发布站:\n${errs.map((d) => `${d.at ?? ''} ${d.message}`).join('\n')}`, { diagnostics: compiled.diagnostics });
|
|
116
|
+
}
|
|
117
|
+
const project = compiled.project;
|
|
118
|
+
const inline = publishPayload(project, compiled.home);
|
|
119
|
+
// 2. vite build 到临时目录
|
|
120
|
+
const staging = fs.mkdtempSync(path.join(os.tmpdir(), 'joplo-build-'));
|
|
121
|
+
// React 与 plugin-react 依据 NODE_ENV 判断生产构建(测试运行器会把它设为 test);构建期间强制 production。
|
|
122
|
+
const prevEnv = process.env.NODE_ENV;
|
|
123
|
+
process.env.NODE_ENV = 'production';
|
|
124
|
+
try {
|
|
125
|
+
if (web.mode === 'dist')
|
|
126
|
+
await stageDistSite(web, projectDir, staging);
|
|
127
|
+
else
|
|
128
|
+
await buildSourceSite(web, projectDir, staging);
|
|
129
|
+
// 3. 后处理
|
|
130
|
+
const siteHtml = path.join(staging, 'site.html');
|
|
131
|
+
const { html, inlined } = finalizeHtml(fs.readFileSync(siteHtml, 'utf8'), (rel) => fs.readFileSync(path.join(staging, rel), 'utf8'), inline);
|
|
132
|
+
fs.rmSync(siteHtml);
|
|
133
|
+
for (const rel of inlined)
|
|
134
|
+
fs.rmSync(path.join(staging, rel));
|
|
135
|
+
fs.writeFileSync(path.join(staging, 'index.html'), html.replace('<title>Joplo · 概念原型</title>', `<title>${escapeHtml(project.name)} · 概念原型</title>`));
|
|
136
|
+
// 4. 写入目标目录
|
|
137
|
+
if (fs.existsSync(outDir))
|
|
138
|
+
fs.rmSync(outDir, { recursive: true, force: true });
|
|
139
|
+
fs.mkdirSync(path.dirname(outDir), { recursive: true });
|
|
140
|
+
fs.cpSync(staging, outDir, { recursive: true });
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
if (prevEnv === undefined)
|
|
144
|
+
delete process.env.NODE_ENV;
|
|
145
|
+
else
|
|
146
|
+
process.env.NODE_ENV = prevEnv;
|
|
147
|
+
fs.rmSync(staging, { recursive: true, force: true });
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
outDir,
|
|
151
|
+
files: listFiles(outDir),
|
|
152
|
+
routes: Object.keys(project.routes).sort(),
|
|
153
|
+
scenarios: Object.keys(project.scenarios),
|
|
154
|
+
diagnostics: compiled.diagnostics,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/** 仓库里:直接编译 packages/web 的发布站入口。 */
|
|
158
|
+
async function buildSourceSite(web, projectDir, staging) {
|
|
159
|
+
const { default: react } = await import('@vitejs/plugin-react');
|
|
160
|
+
await viteBuild({
|
|
161
|
+
configFile: false,
|
|
162
|
+
envDir: false,
|
|
163
|
+
root: web.webDir,
|
|
164
|
+
base: './',
|
|
165
|
+
mode: 'production',
|
|
166
|
+
logLevel: 'error',
|
|
167
|
+
resolve: { alias: [{ find: /^@joplo-app\/core$/, replacement: path.join(web.webDir, 'src/site/core-lite.ts') }, ...kitAliases(web, projectDir)] },
|
|
168
|
+
define: { 'process.env.NODE_ENV': '"production"' },
|
|
169
|
+
plugins: [joploProjectPlugin(projectDir, web), react(), tailwindcss()],
|
|
170
|
+
build: {
|
|
171
|
+
minify: true,
|
|
172
|
+
sourcemap: false,
|
|
173
|
+
outDir: staging,
|
|
174
|
+
emptyOutDir: true,
|
|
175
|
+
assetsInlineLimit: 0,
|
|
176
|
+
chunkSizeWarningLimit: 4000,
|
|
177
|
+
modulePreload: false,
|
|
178
|
+
reportCompressedSize: false,
|
|
179
|
+
rolldownOptions: { input: path.join(web.webDir, 'site.html'), output: { codeSplitting: false } },
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/** 从 npm 安装时:复制预构建的发布站,单独编译作者 className 的 CSS 并在 site.html 里引用。 */
|
|
184
|
+
export async function stageDistSite(web, projectDir, staging) {
|
|
185
|
+
for (const rel of web.siteFiles) {
|
|
186
|
+
const to = path.join(staging, rel);
|
|
187
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
188
|
+
fs.copyFileSync(path.join(web.appDir, rel), to);
|
|
189
|
+
}
|
|
190
|
+
const cssOut = fs.mkdtempSync(path.join(os.tmpdir(), 'joplo-css-'));
|
|
191
|
+
try {
|
|
192
|
+
const out = (await viteBuild({
|
|
193
|
+
configFile: false,
|
|
194
|
+
envDir: false,
|
|
195
|
+
root: path.dirname(web.projectCss),
|
|
196
|
+
base: './',
|
|
197
|
+
mode: 'production',
|
|
198
|
+
logLevel: 'error',
|
|
199
|
+
plugins: [joploProjectPlugin(projectDir, web), tailwindcss()],
|
|
200
|
+
build: { outDir: cssOut, emptyOutDir: true, minify: true, sourcemap: false, reportCompressedSize: false, write: true, rolldownOptions: { input: { project: web.projectCss } } },
|
|
201
|
+
}));
|
|
202
|
+
const files = (Array.isArray(out) ? out : [out]).flatMap((o) => o.output.map((x) => x.fileName)).filter((f) => f.endsWith('.css'));
|
|
203
|
+
if (files.length !== 1)
|
|
204
|
+
throw new JoploError('EXPORT_FAILED', `作者 className 的样式编译结果异常:${files.join(', ') || '没有 CSS'}`);
|
|
205
|
+
const rel = `assets/${path.basename(files[0])}`;
|
|
206
|
+
fs.mkdirSync(path.join(staging, 'assets'), { recursive: true });
|
|
207
|
+
fs.copyFileSync(path.join(cssOut, files[0]), path.join(staging, rel));
|
|
208
|
+
const siteHtml = path.join(staging, 'site.html');
|
|
209
|
+
const html = fs.readFileSync(siteHtml, 'utf8');
|
|
210
|
+
if (!html.includes('</head>'))
|
|
211
|
+
throw new JoploError('EXPORT_FAILED', '预构建的 site.html 缺少 </head>');
|
|
212
|
+
fs.writeFileSync(siteHtml, html.replace('</head>', ` <link rel="stylesheet" href="./${rel}">\n </head>`));
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
fs.rmSync(cssOut, { recursive: true, force: true });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const escapeHtml = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v2 接口(ADR-0002):CLI 只依赖这里导出的三个函数。由 T-board 实现,届时 index.ts 改为导出它们。
|
|
3
|
+
*/
|
|
4
|
+
import type { Diagnostic } from '@joplo-app/core';
|
|
5
|
+
export type DevOptions = {
|
|
6
|
+
projectDir: string;
|
|
7
|
+
/** 缺省 4790;被占用时报错,不静默换端口。 */
|
|
8
|
+
port?: number;
|
|
9
|
+
open?: boolean;
|
|
10
|
+
log?: (line: string) => void;
|
|
11
|
+
};
|
|
12
|
+
/** 单进程:同一个 JOPLO_HOME 下只有一个看板进程,同时服务多个项目(编译、热更新、选中、导出)。 */
|
|
13
|
+
/**
|
|
14
|
+
* url:项目在看板中的链接。attached = true 表示本机已有存活的看板,项目只是注册进去(没有启动第二个进程,close 不做事)。
|
|
15
|
+
*/
|
|
16
|
+
export type DevHandle = {
|
|
17
|
+
url: string;
|
|
18
|
+
attached?: boolean;
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
};
|
|
21
|
+
export declare function startDev(opts: DevOptions): Promise<DevHandle>;
|
|
22
|
+
export type BuildOptions = {
|
|
23
|
+
projectDir: string;
|
|
24
|
+
/** 缺省 `<projectDir>/dist`。 */
|
|
25
|
+
outDir?: string;
|
|
26
|
+
/** 目标目录非空时,必须显式允许才清空重建。 */
|
|
27
|
+
overwrite?: boolean;
|
|
28
|
+
};
|
|
29
|
+
/** 静态发布站:只含选定方案(compile 的 publish 模式),离线可打开,可直接 `wrangler deploy`。 */
|
|
30
|
+
export type BuildResult = {
|
|
31
|
+
outDir: string;
|
|
32
|
+
files: string[];
|
|
33
|
+
routes: string[];
|
|
34
|
+
scenarios: string[];
|
|
35
|
+
diagnostics: Diagnostic[];
|
|
36
|
+
};
|
|
37
|
+
export declare function buildSite(opts: BuildOptions): Promise<BuildResult>;
|
|
38
|
+
export type RenderOptions = {
|
|
39
|
+
projectDir: string;
|
|
40
|
+
scenarioId: string;
|
|
41
|
+
/** 渲染该步骤执行之后的状态;缺省为初始状态。 */
|
|
42
|
+
stepId?: string;
|
|
43
|
+
/** 主题名(D51):内置主题或项目 themes/<name>.css;缺省用项目配置的主题。 */
|
|
44
|
+
theme?: string;
|
|
45
|
+
out: string;
|
|
46
|
+
overwrite?: boolean;
|
|
47
|
+
/** 截图尺寸(D44):舞台宽度仍是页面自己的 viewport,按比例放进这个尺寸。缺省为舞台尺寸。 */
|
|
48
|
+
viewport?: {
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
};
|
|
52
|
+
scale?: number;
|
|
53
|
+
};
|
|
54
|
+
export type RenderedElement = {
|
|
55
|
+
id: string;
|
|
56
|
+
type: string;
|
|
57
|
+
label: string;
|
|
58
|
+
visible: boolean;
|
|
59
|
+
disabled: boolean;
|
|
60
|
+
at: string | null;
|
|
61
|
+
box: {
|
|
62
|
+
x: number;
|
|
63
|
+
y: number;
|
|
64
|
+
width: number;
|
|
65
|
+
height: number;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export type RenderResult = {
|
|
69
|
+
path: string;
|
|
70
|
+
sceneId: string;
|
|
71
|
+
position: number;
|
|
72
|
+
scale: number;
|
|
73
|
+
elements: RenderedElement[];
|
|
74
|
+
warnings: string[];
|
|
75
|
+
};
|
|
76
|
+
export declare function renderStep(opts: RenderOptions): Promise<RenderResult>;
|
package/dist/contract.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/dev.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type http from 'node:http';
|
|
2
|
+
import { type ViteDevServer } from 'vite';
|
|
3
|
+
import { type Diagnostic, type Project } from '@joplo-app/core';
|
|
4
|
+
import type { DevHandle, DevOptions } from './contract.js';
|
|
5
|
+
import { type ExportDeps } from './export.js';
|
|
6
|
+
import type { DistWeb } from './web-assets.js';
|
|
7
|
+
/** 推给看板的编译结果。`project` 始终是最近一次成功的 IR;`ok=false` 时看板显示错误列表、保留画面。 */
|
|
8
|
+
export type DevPayload = {
|
|
9
|
+
version: number;
|
|
10
|
+
ok: boolean;
|
|
11
|
+
project: Project | null;
|
|
12
|
+
diagnostics: Diagnostic[];
|
|
13
|
+
home: string | null;
|
|
14
|
+
};
|
|
15
|
+
/** 首页项目列表的一行。status:idle 未编译、compiling 首次编译中、ok、error(编译出错)、missing(目录不存在)。 */
|
|
16
|
+
export type ProjectSummary = {
|
|
17
|
+
id: string;
|
|
18
|
+
dir: string;
|
|
19
|
+
name: string;
|
|
20
|
+
lastOpened: string;
|
|
21
|
+
status: 'idle' | 'compiling' | 'ok' | 'error' | 'missing';
|
|
22
|
+
errors: number;
|
|
23
|
+
version: number;
|
|
24
|
+
};
|
|
25
|
+
export declare const IR_EVENT = "joplo:ir";
|
|
26
|
+
export declare const PROJECTS_EVENT = "joplo:projects";
|
|
27
|
+
export declare function sendJson(res: http.ServerResponse, status: number, body: unknown): void;
|
|
28
|
+
/** Content-Type 必须是 application/json,只允许附带 charset=utf-8 参数。 */
|
|
29
|
+
export declare function isJsonContentType(ct: string | undefined): boolean;
|
|
30
|
+
/** dist 形态:在看板入口脚本之前创建 HMR 句柄、导入作者 className 的 CSS。 */
|
|
31
|
+
export declare function injectDistBoot(html: string, web: DistWeb): string;
|
|
32
|
+
/** 一个已启动的项目:加载器、编译结果、串行化的重新编译。 */
|
|
33
|
+
export type ProjectSlot = {
|
|
34
|
+
id: string;
|
|
35
|
+
dir: string;
|
|
36
|
+
loader: ViteDevServer | null;
|
|
37
|
+
ready: Promise<void>;
|
|
38
|
+
payload: DevPayload;
|
|
39
|
+
compiling: boolean;
|
|
40
|
+
closed: boolean;
|
|
41
|
+
running: Promise<void> | null;
|
|
42
|
+
dirty: boolean;
|
|
43
|
+
timer: ReturnType<typeof setTimeout> | null;
|
|
44
|
+
};
|
|
45
|
+
export type HubOptions = {
|
|
46
|
+
/** 缺省 4790;被占用时报错,不静默换端口。0 = 随机端口。 */
|
|
47
|
+
port?: number;
|
|
48
|
+
log?: (line: string) => void;
|
|
49
|
+
/** false:只编译一次、不监听文件(renderStep 用它保证截图与元素索引来自同一份 IR)。 */
|
|
50
|
+
watch?: boolean;
|
|
51
|
+
/** 持久化目录(projects.json、server.json);缺省 null:只在内存里记项目,不写 server.json。 */
|
|
52
|
+
home?: string | null;
|
|
53
|
+
/** 测试注入:导出用的 Playwright 载入、视频录制。 */
|
|
54
|
+
exportDeps?: Partial<ExportDeps>;
|
|
55
|
+
};
|
|
56
|
+
export type Hub = {
|
|
57
|
+
url: string;
|
|
58
|
+
port: number;
|
|
59
|
+
token: string;
|
|
60
|
+
server: ViteDevServer;
|
|
61
|
+
/** 登记并(可选)开始编译一个项目;返回它的 id 与看板链接。 */
|
|
62
|
+
addProject(dir: string, opts?: {
|
|
63
|
+
compile?: boolean;
|
|
64
|
+
}): {
|
|
65
|
+
id: string;
|
|
66
|
+
url: string;
|
|
67
|
+
slot: ProjectSlot | null;
|
|
68
|
+
};
|
|
69
|
+
/** 已启动的项目;未启动时启动它(目录不存在时抛 NOT_FOUND)。 */
|
|
70
|
+
ensure(id: string): Promise<ProjectSlot>;
|
|
71
|
+
projects(): ProjectSummary[];
|
|
72
|
+
close(): Promise<void>;
|
|
73
|
+
};
|
|
74
|
+
export declare function createHub(opts?: HubOptions): Promise<Hub>;
|
|
75
|
+
export type InternalDevOptions = DevOptions & Pick<HubOptions, 'watch' | 'home' | 'exportDeps'>;
|
|
76
|
+
export type InternalDevHandle = DevHandle & {
|
|
77
|
+
token: string;
|
|
78
|
+
port: number;
|
|
79
|
+
/** 首个项目的 id 与看板链接(演示页)。 */
|
|
80
|
+
projectId: string;
|
|
81
|
+
projectUrl: string;
|
|
82
|
+
payload(): DevPayload;
|
|
83
|
+
server: ViteDevServer;
|
|
84
|
+
hub: Hub;
|
|
85
|
+
};
|
|
86
|
+
/** 启动一个看板进程并打开一个项目(等首次编译完成)。缺省不持久化。 */
|
|
87
|
+
export declare function createDev(opts: InternalDevOptions): Promise<InternalDevHandle>;
|
|
88
|
+
/** 已有存活的看板:用令牌注册项目,返回项目链接;没有或不可用时返回 null。 */
|
|
89
|
+
export declare function attachToRunning(home: string, projectDir: string, log: (s: string) => void): Promise<{
|
|
90
|
+
url: string;
|
|
91
|
+
port: number;
|
|
92
|
+
} | null>;
|
|
93
|
+
/**
|
|
94
|
+
* CLI 的 `joplo dev`:同一个 JOPLO_HOME 下只运行一个看板进程。
|
|
95
|
+
* 已有存活的看板 → 注册项目、(--open 时)打开浏览器,返回 attached;否则启动新的看板并登记项目。
|
|
96
|
+
*/
|
|
97
|
+
export declare function startDev(opts: DevOptions & {
|
|
98
|
+
home?: string;
|
|
99
|
+
exportDeps?: Partial<ExportDeps>;
|
|
100
|
+
}): Promise<DevHandle>;
|