@macrowing/inbiz-codegen 0.0.0-stage → 1.0.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/README.md +592 -2
- package/bin/inbiz-codegen.mjs +7 -0
- package/package.json +32 -4
- package/scripts/prepare-templates.mjs +35 -0
- package/src/backend/init.mjs +170 -0
- package/src/config.mjs +150 -0
- package/src/favicon.mjs +100 -0
- package/src/generate.mjs +935 -0
- package/src/generated-output.mjs +107 -0
- package/src/index.mjs +346 -0
- package/src/menu/build.mjs +252 -0
- package/src/menu/check.mjs +163 -0
- package/src/menu/command.mjs +179 -0
- package/src/migrate-runtime.mjs +106 -0
- package/src/platform/client.mjs +167 -0
- package/src/platform/command.mjs +386 -0
- package/src/platform/pack.mjs +591 -0
- package/src/platform/recover.mjs +236 -0
- package/src/platform/startup.mjs +343 -0
- package/src/platform/state.mjs +116 -0
- package/src/platform/update.mjs +157 -0
- package/src/platform/zip.mjs +134 -0
- package/src/pull.mjs +284 -0
- package/src/scaffold/commands.mjs +349 -0
- package/src/scaffold/init.mjs +370 -0
- package/src/scaffold/template-files.mjs +40 -0
- package/src/scaffold/template-source.mjs +235 -0
- package/src/scaffold/template-sync.mjs +335 -0
- package/src/transform/heaviness.mjs +380 -0
- package/src/transform/preview-props.mjs +108 -0
- package/src/transform/published-page-links.mjs +61 -0
- package/src/transform/published-script.mjs +110 -0
- package/src/transform/registry.mjs +781 -0
- package/src/transform/schema-to-jsx.mjs +1708 -0
- package/src/util/des.mjs +10 -0
- package/templates/README.md +34 -0
- package/templates/backend/Directory.Build.props +19 -0
- package/templates/backend/Directory.Packages.props +49 -0
- package/templates/backend/Macrowing.Scaffold.slnx +13 -0
- package/templates/backend/host/Macrowing.Start/Init/app.json +8 -0
- package/templates/backend/host/Macrowing.Start/Init/menus.json +4 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationDefinition.cs +245 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationEndpoints.cs +57 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationHostedService.cs +63 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationOptions.cs +44 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationRunner.cs +294 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationState.cs +72 -0
- package/templates/backend/host/Macrowing.Start/Initialization/MenuFactory.cs +118 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PackageBuilder.cs +317 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PlatformAppImporter.cs +371 -0
- package/templates/backend/host/Macrowing.Start/Macrowing.Start.csproj +26 -0
- package/templates/backend/host/Macrowing.Start/Program.cs +33 -0
- package/templates/backend/host/Macrowing.Start/StartModule.cs +67 -0
- package/templates/backend/host/Macrowing.Start/appsettings.json +38 -0
- package/templates/backend/modules/Macrowing.Generated/Application/GeneratedApplicationAutoMapperProfile.cs +42 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/Entities/GeneratedEntityBase.cs +72 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/GeneratedConsts.cs +50 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContext.cs +55 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContextModelBuilderExtensions.cs +61 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/IGeneratedDbContext.cs +32 -0
- package/templates/backend/modules/Macrowing.Generated/GeneratedModule.cs +89 -0
- package/templates/backend/modules/Macrowing.Generated/Macrowing.Generated.csproj +22 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsClient.cs +95 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsModels.cs +81 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Dictionary/DictionaryClient.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InBizSdkModule.cs +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InterfaceEngine/InterfaceEngineClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Localization/LocalizationClient.cs +28 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Macrowing.InBiz.SDK.csproj +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageClient.cs +99 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageModels.cs +63 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelClient.cs +131 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelQuery.cs +108 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelWrite.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Permission/PermissionClient.cs +86 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformException.cs +42 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformRegistration.cs +60 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformTransport.cs +96 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizRequestContext.cs +27 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/README.md +117 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsClient.cs +35 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsQuery.cs +20 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationRequests.cs +105 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationsClient.cs +53 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/README.md +132 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Tracking/WorkFlowTrackingClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeClient.cs +78 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeModels.cs +34 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowContracts.cs +88 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowRegistration.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowTransport.cs +163 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchClient.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchModels.cs +76 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Macrowing.OC.SDK.csproj +20 -0
- package/templates/backend/modules/Macrowing.OC.SDK/OcSdkModule.cs +18 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Protos/README.md +22 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/Macrowing.InBiz.SDK.Tests.csproj +14 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformContractTests.cs +288 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformLiveTests.cs +173 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowHttpServer.cs +113 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowLiveTests.cs +269 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowOperationsTests.cs +80 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowQueryTests.cs +163 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowTransportTests.cs +111 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowUpgradeTests.cs +87 -0
- package/templates/frontend/components/README.md +39 -0
- package/templates/frontend/components/app-components/README.md +33 -0
- package/templates/frontend/components/app-components/common/.gitkeep +0 -0
- package/templates/frontend/components/app-components/components/.gitkeep +0 -0
- package/templates/frontend/components/app-components/package.json +20 -0
- package/templates/frontend/mgt/.umirc.ts +574 -0
- package/templates/frontend/mgt/package.json +96 -0
- package/templates/frontend/mgt/src/app-extends.ts +9 -0
- package/templates/frontend/mgt/src/app.tsx +20 -0
- package/templates/frontend/mgt/src/config/app.ts +31 -0
- package/templates/frontend/mgt/src/layout/header.tsx +14 -0
- package/templates/frontend/mgt/src/layout/index.tsx +11 -0
- package/templates/frontend/mgt/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/mgt/src/locales.ts +8 -0
- package/templates/frontend/mgt/src/pages/.gitkeep +0 -0
- package/templates/frontend/mgt/src/root.tsx +12 -0
- package/templates/frontend/mgt/src/route-view.tsx +3 -0
- package/templates/frontend/mgt/src/runtime.ts +21 -0
- package/templates/frontend/mgt/src/styles/app.css +1 -0
- package/templates/frontend/mgt/src/styles/layout.less +79 -0
- package/templates/frontend/mgt/tsconfig.json +65 -0
- package/templates/frontend/mgt/typings.d.ts +57 -0
- package/templates/frontend/wap/.umirc.ts +574 -0
- package/templates/frontend/wap/package.json +73 -0
- package/templates/frontend/wap/src/app-extends.ts +9 -0
- package/templates/frontend/wap/src/app.tsx +28 -0
- package/templates/frontend/wap/src/config/app.ts +31 -0
- package/templates/frontend/wap/src/layout/header.tsx +14 -0
- package/templates/frontend/wap/src/layout/index.tsx +18 -0
- package/templates/frontend/wap/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/wap/src/locales.ts +8 -0
- package/templates/frontend/wap/src/pages/.gitkeep +0 -0
- package/templates/frontend/wap/src/root.tsx +12 -0
- package/templates/frontend/wap/src/route-view.tsx +3 -0
- package/templates/frontend/wap/src/runtime.ts +21 -0
- package/templates/frontend/wap/src/styles/adaptation.less +190 -0
- package/templates/frontend/wap/src/styles/app.css +7 -0
- package/templates/frontend/wap/src/styles/global.less +81 -0
- package/templates/frontend/wap/src/styles/layout.less +92 -0
- package/templates/frontend/wap/tsconfig.json +66 -0
- package/templates/frontend/wap/typings.d.ts +24 -0
- package/templates/frontend/web/.umirc.ts +574 -0
- package/templates/frontend/web/package.json +26 -0
- package/templates/frontend/web/src/app-extends.ts +9 -0
- package/templates/frontend/web/src/app.tsx +25 -0
- package/templates/frontend/web/src/config/app.ts +31 -0
- package/templates/frontend/web/src/layout/header.tsx +14 -0
- package/templates/frontend/web/src/layout/index.tsx +11 -0
- package/templates/frontend/web/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/web/src/locales.ts +8 -0
- package/templates/frontend/web/src/root.tsx +12 -0
- package/templates/frontend/web/src/route-view.tsx +3 -0
- package/templates/frontend/web/src/runtime.ts +21 -0
- package/templates/frontend/web/src/styles/app.css +104 -0
- package/templates/frontend/web/src/styles/layout.less +124 -0
- package/templates/frontend/web/tsconfig.json +91 -0
- package/templates/frontend/web/typings.d.ts +46 -0
- package/templates/package.json +14 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { CODEGEN_ROOT, COMPONENTS_ROOT, COMPONENTS_BASIC_ROOTS } from '../config.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 重型组件判定:组件源码是否(传递地)import 了大体积依赖。
|
|
7
|
+
*
|
|
8
|
+
* ============================================================================
|
|
9
|
+
* 结论:x6 / bpmn-js 是怎么进入产物里的(2026-11 实测的 import 链路)
|
|
10
|
+
* ============================================================================
|
|
11
|
+
*
|
|
12
|
+
* 【x6 —— 当前产物里其实已经没有它了,但机制要保留】
|
|
13
|
+
*
|
|
14
|
+
* 唯一的真实引入点是 `@rxdrag/minions-logicflow-editor`,它依赖 @antv/x6:
|
|
15
|
+
* inbiz-render/render/sdk/Controller/Engine.ts
|
|
16
|
+
* → @rxdrag/minions-runtime-react → @rxdrag/minions-logicflow-editor → @antv/x6
|
|
17
|
+
* 而 `render/sdk/Controller/index.ts` **没有被任何地方 import**:sdk/index.ts、
|
|
18
|
+
* sdk/core/Page.ts、sdk/core/Layout.ts 里对它的引用都已被注释掉。
|
|
19
|
+
* 加上 @settings / @config/const/theme 两个构建期替身(web/src/shims)已经
|
|
20
|
+
* 去掉了 `import { Background } from '@antv/x6'`,dist 里 grep 不到任何
|
|
21
|
+
* `x6-graph` / `x6-common` / `registerNode` 字样 —— 当前 22MB 的 dist 中
|
|
22
|
+
* **x6 体积是 0**,文档里「x6 14MB」是设计器入口还在时的旧数据。
|
|
23
|
+
*
|
|
24
|
+
* 所以这条链路的风险不是「现在很重」,而是「谁把 Controller 重新接回
|
|
25
|
+
* sdk/index.ts,x6 就会悄悄回来」。判定逻辑按源码图扫描,重新接回时
|
|
26
|
+
* 会自动把 ProcessLogBtn / ProcessCenter 这类组件标成重型。
|
|
27
|
+
*
|
|
28
|
+
* 【bpmn-js —— 真实在产物里,且现在跟着首屏一起加载】
|
|
29
|
+
*
|
|
30
|
+
* core/components/business/process/ProcessCenter/preview/web/index.tsx
|
|
31
|
+
* → components/CenterIndex/…、components/EasyType/…、components/TableInfo
|
|
32
|
+
* → components/ProcessLogModal (流程日志弹窗)
|
|
33
|
+
* → @inbiz/basic/ProcessLog
|
|
34
|
+
* → ../BpmnViewer ← 这里是 bpmn-js 的入口
|
|
35
|
+
* → ../InbizBpmn/Bpmn/customModeler → bpmn-js/lib/Modeler
|
|
36
|
+
* → camunda-bpmn-moddle/resources/camunda.json
|
|
37
|
+
*
|
|
38
|
+
* 另外两条同源:
|
|
39
|
+
* core/components/form/process/ProcessLogBtn/preview/web/index.tsx
|
|
40
|
+
* → @inbiz/basic/ProcessLog → BpmnViewer (同一个入口)
|
|
41
|
+
* engine/process/ProcessManagement、engine/process/ProcessMonitoring
|
|
42
|
+
* → ProcessCenter 同一条链
|
|
43
|
+
*
|
|
44
|
+
* 实测印证:dist/1654.*.async.js(581KB,纯 bpmn 资产,含 BPMNDiagram /
|
|
45
|
+
* bpmn-js 字样)与 dist/1768.*.async.js(300KB)都挂在**首页** shouye 的
|
|
46
|
+
* 依赖列表里 —— 因为首页 schama 里有一块 ProcessCenter。流程中心按钮点开
|
|
47
|
+
* 才需要流程图,但 ProcessLogModal 是静态 import,整包被同步拉下来。
|
|
48
|
+
*
|
|
49
|
+
* 这就是本模块存在的理由:ProcessCenter 在 3 个页面(首页、流程中心、
|
|
50
|
+
* 另一个)出现,只有用户点「流程日志」时才需要 bpmn-js。
|
|
51
|
+
*
|
|
52
|
+
* 【echarts / tinymce】同理,命中即懒加载:
|
|
53
|
+
* BiBarMap / BiLineMap / BIPieChart → echarts.init(真用)
|
|
54
|
+
* EformRichText → @tinymce/tinymce-react
|
|
55
|
+
*
|
|
56
|
+
* 【一个必须避开的误判】
|
|
57
|
+
* EformStaticList/preview/public/index.ts 里有一行 `import { number } from 'echarts'`,
|
|
58
|
+
* 而 `number` 只出现在 `id: number` 这类**类型位置**,TS 编译时整条 import
|
|
59
|
+
* 被擦掉,产物里根本没有 echarts。但 EformStaticList 被 58 个页面引用,
|
|
60
|
+
* 按「出现 echarts 就算重」会把它全量懒加载,首屏反而更慢。
|
|
61
|
+
* 所以命名导入要额外确认绑定在**值位置**被用到(见 usedAsValue)。
|
|
62
|
+
* ============================================================================
|
|
63
|
+
*
|
|
64
|
+
* 判定依据是**组件源码的静态 import 图**,不维护组件名单:
|
|
65
|
+
* 从组件的 `preview/web/index.tsx` 出发做一次传递闭包,闭包里出现
|
|
66
|
+
* 大体积依赖(x6 / bpmn / echarts / tinymce …)就判定为重型,
|
|
67
|
+
* 由 schema-to-jsx 改成 `React.lazy` + `Suspense` 动态引入。
|
|
68
|
+
*
|
|
69
|
+
* 为什么不写死清单:
|
|
70
|
+
* 组件库有 100+ 个组件,哪个用到流程图库会随版本变。写死一张表,
|
|
71
|
+
* 换一次组件库版本就有一个页面把几 MB 打进首屏 chunk。
|
|
72
|
+
*
|
|
73
|
+
* 为什么只看静态 import:
|
|
74
|
+
* 已经写成 `import()` 的依赖本来就单独成 chunk,不会落到调用方的
|
|
75
|
+
* 同步 chunk 里,不需要再包一层。
|
|
76
|
+
*
|
|
77
|
+
* 只做代码字符串的静态扫描,不依赖 bundler 产物,也不需要构建。
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 大体积依赖。命中即判定为重型。
|
|
82
|
+
*
|
|
83
|
+
* 名单是「能力型重依赖」而不是「某个组件」—— 加一条就覆盖所有使用者。
|
|
84
|
+
* 数值是实测体积,仅用于日志说明,不参与判定。
|
|
85
|
+
*/
|
|
86
|
+
const HEAVY_PACKAGES = [
|
|
87
|
+
{ pkg: '@antv/x6', why: 'x6 图编辑器(约 14MB,仅流程/关系图类页面需要)' },
|
|
88
|
+
{ pkg: '@antv/x6-react-shape', why: 'x6 的 React 渲染器' },
|
|
89
|
+
{ pkg: '@antv/x6-plugin-dnd', why: 'x6 拖拽插件' },
|
|
90
|
+
{ pkg: '@antv/x6-plugin-minimap', why: 'x6 缩略图插件' },
|
|
91
|
+
{ pkg: '@antv/x6-plugin-selection', why: 'x6 框选插件' },
|
|
92
|
+
{ pkg: 'bpmn-js', why: 'bpmn-js 流程查看器(约 2MB)' },
|
|
93
|
+
{ pkg: 'diagram-js', why: 'bpmn-js 的底层绘图库' },
|
|
94
|
+
{ pkg: 'camunda-bpmn-moddle', why: 'BPMN 流程模型描述文件' },
|
|
95
|
+
{ pkg: 'echarts', why: 'echarts 图表库' },
|
|
96
|
+
{ pkg: 'tinymce', why: 'tinymce 富文本编辑器' },
|
|
97
|
+
{ pkg: '@tinymce/tinymce-react', why: 'tinymce 的 React 封装' },
|
|
98
|
+
{ pkg: 'react-ace', why: 'ace 代码编辑器' },
|
|
99
|
+
{ pkg: '@rxdrag/minions-logicflow-editor', why: '逻辑流编辑器(内部依赖 x6)' },
|
|
100
|
+
{ pkg: '@rxdrag/minions-runtime-react', why: '逻辑流运行时' },
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 这些目录在构建期被整体替换成空实现,运行态一行都不进来,不参与判定。
|
|
105
|
+
*
|
|
106
|
+
* 见 scaffolds/frontend/web/.umirc.ts 里的 NormalModuleReplacementPlugin:
|
|
107
|
+
* `/[\/]designer[\/]/` → designer-stub.js。若不排除,Table / FormGrid 这类
|
|
108
|
+
* 「preview 里带一个 isDesigner 分支」的组件全会命中 @inbiz/setters,
|
|
109
|
+
* 等于把所有组件都懒加载,首屏反而更慢。
|
|
110
|
+
*/
|
|
111
|
+
const IGNORED_PATH_SEGMENTS = ['/designer/'];
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 组件包里的实际位置,由 config.mjs 统一解析(npm 安装的组件包,编译产物布局)。
|
|
115
|
+
* 以前这里写死 `packages/inbiz-components/src`,那份目录已删除。
|
|
116
|
+
*/
|
|
117
|
+
const BASIC_ROOTS = COMPONENTS_BASIC_ROOTS;
|
|
118
|
+
|
|
119
|
+
const CODE_EXTS = ['.tsx', '.ts', '.jsx', '.js'];
|
|
120
|
+
|
|
121
|
+
/** 文件 → 解析出的 import 语句,避免同一文件被多次读盘 */
|
|
122
|
+
const fileCache = new Map();
|
|
123
|
+
/** 组件入口 → 判定结果 */
|
|
124
|
+
const classifyCache = new Map();
|
|
125
|
+
|
|
126
|
+
function packageOf(spec) {
|
|
127
|
+
const parts = spec.split('/');
|
|
128
|
+
return spec.charAt(0) === '@' ? parts.slice(0, 2).join('/') : parts[0];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 命中重型包返回说明,否则 null */
|
|
132
|
+
function heavyOf(spec) {
|
|
133
|
+
const pkg = packageOf(spec);
|
|
134
|
+
for (const h of HEAVY_PACKAGES) {
|
|
135
|
+
if (pkg === h.pkg || spec === h.pkg || spec.indexOf(h.pkg + '/') === 0) return h.why;
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isIgnored(file) {
|
|
141
|
+
const p = '/' + file.replace(/\\/g, '/');
|
|
142
|
+
return IGNORED_PATH_SEGMENTS.some((seg) => p.indexOf(seg) >= 0);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 去掉注释,避免把注释掉的 import 当成真实依赖 */
|
|
146
|
+
function stripComments(src) {
|
|
147
|
+
return src.replace(/\/\*[\s\S]*?\*\//g, '').replace(/(^|[^:\\])\/\/[^\n]*/g, '$1');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** 去掉字符串字面量,避免把 `case 'number':` 当成对 number 的引用 */
|
|
151
|
+
function stripStrings(src) {
|
|
152
|
+
return src
|
|
153
|
+
.replace(/'(?:\\.|[^'\\\n])*'/g, "''")
|
|
154
|
+
.replace(/"(?:\\.|[^"\\\n])*"/g, '""')
|
|
155
|
+
.replace(/`(?:\\.|[^`\\])*`/g, '``');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 抽取一个模块的所有模块说明符。
|
|
160
|
+
*
|
|
161
|
+
* 覆盖:
|
|
162
|
+
* import X from 'p' import { A, B as C } from 'p' import * as ns from 'p'
|
|
163
|
+
* import 'p' export { A } from 'p' export * from 'p'
|
|
164
|
+
* import type { T } from 'p' ← 标 typeOnly,不参与判定
|
|
165
|
+
* require('p') import('p') ← 动态引入,不参与判定
|
|
166
|
+
*
|
|
167
|
+
* @returns {{ spec: string, typeOnly: boolean, bindings: string[], alwaysValue: boolean }[]}
|
|
168
|
+
* alwaysValue:命名空间导入 / 默认导入 / 副作用导入。这类一定被求值,不需要再看用没用。
|
|
169
|
+
*/
|
|
170
|
+
function extractImports(src) {
|
|
171
|
+
const text = stripComments(src);
|
|
172
|
+
const out = [];
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 压缩产物里的 import 长这样:
|
|
176
|
+
* `...e[t]=r,e}import{forwardRef as e}from"react";import{RenderDropableWidget as t}from"@inbiz/react";`
|
|
177
|
+
*
|
|
178
|
+
* 两个细节都不能按源码写法来写:
|
|
179
|
+
* 1. 前一个 token 是 `}` 而不是 `;` —— 只用 `[\n;]` 做边界会漏掉整条语句;
|
|
180
|
+
* 2. `import` 和 `{` 之间、`from` 和引号之间没有空格 —— `\s+` 会要求空格。
|
|
181
|
+
* 漏掉的后果是重型依赖(bpmn-js / echarts)一个都识别不到,ProcessCenter
|
|
182
|
+
* 这类组件不会被标成重型,首屏照旧同步拉下整包。
|
|
183
|
+
*
|
|
184
|
+
* 边界用「前一个字符不是标识符字符」,既覆盖 `}import`、`;import`,也不会
|
|
185
|
+
* 误命中 `ximport`。
|
|
186
|
+
*/
|
|
187
|
+
const staticRe =
|
|
188
|
+
/(?:^|[^\w$])(import|export)\s*(type\s+)?([^;'"]*?)\s*from\s*['"]([^'"\n]+)['"]/g;
|
|
189
|
+
let m;
|
|
190
|
+
while ((m = staticRe.exec(text))) {
|
|
191
|
+
const isType = !!m[2];
|
|
192
|
+
const decl = (m[3] || '').trim();
|
|
193
|
+
const spec = m[4];
|
|
194
|
+
const bindings = [];
|
|
195
|
+
let alwaysValue = false;
|
|
196
|
+
|
|
197
|
+
if (/^\*/.test(decl)) {
|
|
198
|
+
// import * as ns
|
|
199
|
+
alwaysValue = true;
|
|
200
|
+
} else {
|
|
201
|
+
const named = decl.match(/\{([\s\S]*)\}/);
|
|
202
|
+
const defaultPart = decl.replace(/\{[\s\S]*\}/, '').replace(/,\s*$/, '').trim();
|
|
203
|
+
// `import a, { b }` 的 defaultPart 是 `a`;`import { b }` 是空串
|
|
204
|
+
if (defaultPart && /^[A-Za-z_$][\w$]*$/.test(defaultPart)) alwaysValue = true;
|
|
205
|
+
if (named) {
|
|
206
|
+
for (const piece of named[1].split(',')) {
|
|
207
|
+
const parts = piece.replace(/^\s*type\s+/, '').trim().split(/\s+as\s+/);
|
|
208
|
+
const local = (parts[1] || parts[0]).trim();
|
|
209
|
+
if (local) bindings.push(local);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
out.push({ spec, typeOnly: isType, bindings, alwaysValue });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 副作用导入:import 'bpmn-js/dist/assets/diagram-js.css'
|
|
218
|
+
// 压缩形态是 `;import"x.css"` / `}import"x.css"`,中间没有空格 —— 同 staticRe 的理由。
|
|
219
|
+
const bareRe = /(?:^|[^\w$])import\s*['"]([^'"\n]+)['"]/g;
|
|
220
|
+
while ((m = bareRe.exec(text))) {
|
|
221
|
+
out.push({ spec: m[1], typeOnly: false, bindings: [], alwaysValue: true });
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 已经在 import() 里的模块单独成 chunk,不是同步依赖。
|
|
225
|
+
// require() 的是运行期同步加载,按静态依赖处理。
|
|
226
|
+
const reqRe = /(?:^|[^\w$.])require\s*\(\s*['"]([^'"\n]+)['"]\s*\)/g;
|
|
227
|
+
while ((m = reqRe.exec(text))) {
|
|
228
|
+
out.push({ spec: m[1], typeOnly: false, bindings: [], alwaysValue: true });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 命名导入的绑定是否在运行期被真正用到。
|
|
236
|
+
*
|
|
237
|
+
* 为什么要判:组件库里有把类型名当值导入的写法,例如
|
|
238
|
+
* import { number } from 'echarts' // 只在 `id: number` 这类类型注解里出现
|
|
239
|
+
* TypeScript 编译时会把这条 import 整个擦掉,构建产物里没有 echarts。
|
|
240
|
+
* 若按「出现了 echarts 就算重型」判定,EformStaticList(58 个页面在用)
|
|
241
|
+
* 会被误判成图表组件而整页懒加载,首屏反而更慢。
|
|
242
|
+
*
|
|
243
|
+
* 判据保守:只有出现在「调用 / 成员访问 / 下标 / new / JSX」位置才算值使用。
|
|
244
|
+
* 漏判的代价只是少懒加载一个组件(体积问题),不会产出错误代码。
|
|
245
|
+
*/
|
|
246
|
+
function usedAsValue(sourceNoStrings, binding) {
|
|
247
|
+
const esc = binding.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
248
|
+
const patterns = [
|
|
249
|
+
new RegExp(`\\b${esc}\\s*[.(\\[]`), // fn() / obj.prop / arr[0]
|
|
250
|
+
new RegExp(`\\bnew\\s+${esc}\\b`), // new Modeler()
|
|
251
|
+
new RegExp(`<${esc}[\\s/>]`), // <Tinymce />
|
|
252
|
+
];
|
|
253
|
+
return patterns.some((re) => re.test(sourceNoStrings));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function statKind(p) {
|
|
257
|
+
try {
|
|
258
|
+
return fs.statSync(p);
|
|
259
|
+
} catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** 模块说明符 → 本地源码文件;解析不到(外部依赖)返回 null */
|
|
265
|
+
function resolveLocal(spec, fromFile) {
|
|
266
|
+
const tryDir = (dir) => {
|
|
267
|
+
for (const ext of CODE_EXTS) {
|
|
268
|
+
const f = path.join(dir, 'index' + ext);
|
|
269
|
+
if (statKind(f)?.isFile()) return f;
|
|
270
|
+
}
|
|
271
|
+
for (const ext of CODE_EXTS) {
|
|
272
|
+
const f = dir + ext;
|
|
273
|
+
if (statKind(f)?.isFile()) return f;
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
if (spec.charAt(0) === '.') {
|
|
279
|
+
const base = path.resolve(path.dirname(fromFile), spec);
|
|
280
|
+
const st = statKind(base);
|
|
281
|
+
if (st?.isFile()) return /\.(tsx?|jsx?)$/.test(base) ? base : null;
|
|
282
|
+
return tryDir(base);
|
|
283
|
+
}
|
|
284
|
+
if (spec.indexOf('@components/') === 0) {
|
|
285
|
+
return tryDir(path.join(COMPONENTS_ROOT, spec.slice('@components/'.length)));
|
|
286
|
+
}
|
|
287
|
+
// 组件库内部用 `@inbiz/basic/<X>` 引用基础组件,实际位置随版本变动
|
|
288
|
+
if (spec.indexOf('@inbiz/basic/') === 0) {
|
|
289
|
+
const name = spec.slice('@inbiz/basic/'.length);
|
|
290
|
+
for (const root of BASIC_ROOTS) {
|
|
291
|
+
const hit = tryDir(path.join(root, name));
|
|
292
|
+
if (hit) return hit;
|
|
293
|
+
}
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** 从入口文件做传递闭包,收集命中的重型依赖 */
|
|
300
|
+
function analyze(entryFile) {
|
|
301
|
+
const heavy = new Map();
|
|
302
|
+
const seen = new Set();
|
|
303
|
+
const queue = [entryFile];
|
|
304
|
+
|
|
305
|
+
while (queue.length) {
|
|
306
|
+
const file = queue.pop();
|
|
307
|
+
if (seen.has(file) || isIgnored(file)) continue;
|
|
308
|
+
seen.add(file);
|
|
309
|
+
|
|
310
|
+
let parsed = fileCache.get(file);
|
|
311
|
+
if (!parsed) {
|
|
312
|
+
let src = '';
|
|
313
|
+
try {
|
|
314
|
+
src = fs.readFileSync(file, 'utf8');
|
|
315
|
+
} catch {
|
|
316
|
+
parsed = { imports: [], valueSource: '' };
|
|
317
|
+
}
|
|
318
|
+
if (!parsed) {
|
|
319
|
+
parsed = {
|
|
320
|
+
imports: extractImports(src),
|
|
321
|
+
valueSource: stripStrings(stripComments(src)),
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
fileCache.set(file, parsed);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
for (const imp of parsed.imports) {
|
|
328
|
+
if (imp.typeOnly) continue;
|
|
329
|
+
if (!heavyOf(imp.spec)) {
|
|
330
|
+
const next = resolveLocal(imp.spec, file);
|
|
331
|
+
if (next && !seen.has(next)) queue.push(next);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
// 命中重型包。默认导入 / 命名空间导入 / 副作用导入直接算;
|
|
335
|
+
// 命名导入要确认至少有一个绑定在值位置被用到。
|
|
336
|
+
//
|
|
337
|
+
// 编译产物(.js)不做这项确认:terser 把绑定名压成单字母,`usedAsValue` 的
|
|
338
|
+
// 文本判据两种方向都会错。实测 `@tinymce/tinymce-react` 被导入成 `s`、
|
|
339
|
+
// 以 `b(s,{...})` 渲染 —— 既不是 `.`/`(`/`[` 也不是 `<s`,判据全部落空,
|
|
340
|
+
// EformRichText 因此不算重型,tinymce 被同步打进首屏 chunk;
|
|
341
|
+
// 反过来单字母绑定又会撞上无关的 `s.push`,产生误判。
|
|
342
|
+
//
|
|
343
|
+
// 而「类型位置导入」在 TS 编译期就被擦掉了,编译产物里根本不会出现,
|
|
344
|
+
// 所以对 .js 直接按值使用处理是安全的(EformStaticList 的产物里没有 echarts)。
|
|
345
|
+
const compiled = /\.jsx?$/.test(file);
|
|
346
|
+
if (imp.alwaysValue || compiled || imp.bindings.some((b) => usedAsValue(parsed.valueSource, b))) {
|
|
347
|
+
if (!heavy.has(packageOf(imp.spec))) heavy.set(packageOf(imp.spec), heavyOf(imp.spec));
|
|
348
|
+
}
|
|
349
|
+
// 重型包内部不再往下走
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return heavy;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 判定一个组件入口是否重型。
|
|
358
|
+
*
|
|
359
|
+
* @param {string} entryFile 组件的 preview/web/index.tsx 绝对路径
|
|
360
|
+
* @returns {{ lazy: boolean, reasons: string[] }}
|
|
361
|
+
*/
|
|
362
|
+
export function classifyComponent(entryFile) {
|
|
363
|
+
if (!entryFile) return { lazy: false, reasons: [] };
|
|
364
|
+
const key = entryFile.replace(/\\/g, '/');
|
|
365
|
+
const cached = classifyCache.get(key);
|
|
366
|
+
if (cached) return cached;
|
|
367
|
+
|
|
368
|
+
let result = { lazy: false, reasons: [] };
|
|
369
|
+
if (fs.existsSync(key)) {
|
|
370
|
+
const heavy = analyze(key);
|
|
371
|
+
if (heavy.size) {
|
|
372
|
+
result = {
|
|
373
|
+
lazy: true,
|
|
374
|
+
reasons: [...heavy.entries()].map(([pkg, why]) => `${pkg}:${why}`),
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
classifyCache.set(key, result);
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { WEB_DIR } from '../config.mjs';
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const workspaceRequire = createRequire(path.join(WEB_DIR, 'package.json'));
|
|
7
|
+
const loadTypeScript = () => {
|
|
8
|
+
try {
|
|
9
|
+
return require('typescript');
|
|
10
|
+
} catch (error) {
|
|
11
|
+
if (error.code !== 'MODULE_NOT_FOUND') throw error;
|
|
12
|
+
return workspaceRequire('typescript');
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Follow local default imports selected by an exported preview alias. */
|
|
17
|
+
export function previewImplementationImports(source, componentName) {
|
|
18
|
+
const ts = loadTypeScript();
|
|
19
|
+
const tree = ts.createSourceFile('preview.tsx', source, ts.ScriptTarget.Latest, true);
|
|
20
|
+
const aliases = new Map();
|
|
21
|
+
const imports = new Map();
|
|
22
|
+
const entries = new Set();
|
|
23
|
+
for (const node of tree.statements) {
|
|
24
|
+
if (ts.isImportDeclaration(node) && node.importClause?.name &&
|
|
25
|
+
ts.isStringLiteral(node.moduleSpecifier) && node.moduleSpecifier.text.startsWith('.')) {
|
|
26
|
+
imports.set(node.importClause.name.text, node.moduleSpecifier.text);
|
|
27
|
+
} else if (ts.isVariableStatement(node)) {
|
|
28
|
+
for (const { name, initializer } of node.declarationList.declarations) {
|
|
29
|
+
if (!ts.isIdentifier(name) || !initializer) continue;
|
|
30
|
+
aliases.set(name.text, initializer);
|
|
31
|
+
if (name.text === componentName && node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
32
|
+
entries.add(initializer);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
} else if (ts.isExportAssignment(node) && !node.isExportEquals) {
|
|
36
|
+
entries.add(node.expression);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const results = new Set();
|
|
40
|
+
const seen = new Set();
|
|
41
|
+
function visit(node) {
|
|
42
|
+
if (!node || seen.has(node)) return;
|
|
43
|
+
seen.add(node);
|
|
44
|
+
if (ts.isIdentifier(node)) {
|
|
45
|
+
if (imports.has(node.text)) results.add(imports.get(node.text));
|
|
46
|
+
else visit(aliases.get(node.text));
|
|
47
|
+
} else if (ts.isConditionalExpression(node)) {
|
|
48
|
+
visit(node.whenTrue);
|
|
49
|
+
visit(node.whenFalse);
|
|
50
|
+
} else if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
51
|
+
// `expr as unknown as T` 会嵌套两层 AsExpression,逐层解开即可追踪到实现。
|
|
52
|
+
visit(node.expression);
|
|
53
|
+
} else if (ts.isPropertyAccessExpression(node)) {
|
|
54
|
+
/**
|
|
55
|
+
* 编译产物的入口常写成成员访问:
|
|
56
|
+
* `export var VerticalMenu = getPortal() ? Portal.VerticalMenu : OldMen.VerticalMenu;`
|
|
57
|
+
* 这里要顺着 `Portal` / `OldMen` 这两个导入回到实现模块,
|
|
58
|
+
* 否则 `props.inbiz` 这类只有实现里才读的字段会被判成「组件不需要」,
|
|
59
|
+
* 生成的 JSX 就漏传 inbiz,运行期菜单拿不到平台 SDK。
|
|
60
|
+
*/
|
|
61
|
+
visit(node.expression);
|
|
62
|
+
} else if (ts.isCallExpression(node)) {
|
|
63
|
+
// `observer(...)` / `memo(...)` 之类的包装:追踪其参数里的实现
|
|
64
|
+
for (const arg of node.arguments) visit(arg);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const entry of entries) visit(entry);
|
|
68
|
+
return results;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Detect explicitly declared or consumed props, not names in imports or strings. */
|
|
72
|
+
export function readsPreviewProp(source, prop) {
|
|
73
|
+
if (!source.includes(prop)) return false;
|
|
74
|
+
const ts = loadTypeScript();
|
|
75
|
+
const tree = ts.createSourceFile('preview.tsx', source, ts.ScriptTarget.Latest, true);
|
|
76
|
+
function isProp(name) {
|
|
77
|
+
return name && (ts.isIdentifier(name) || ts.isStringLiteral(name)) && name.text === prop;
|
|
78
|
+
}
|
|
79
|
+
// Follow component parameters by scope rather than treating every object
|
|
80
|
+
// that happens to have an `inbiz` member as the preview's props.
|
|
81
|
+
function isParameterReference(node) {
|
|
82
|
+
if (!ts.isIdentifier(node)) return false;
|
|
83
|
+
for (let scope = node.parent; scope; scope = scope.parent) {
|
|
84
|
+
if (ts.isFunctionLike(scope) && scope.parameters.some(parameter =>
|
|
85
|
+
ts.isIdentifier(parameter.name) && parameter.name.text === node.text &&
|
|
86
|
+
!(parameter.type && ts.isTypeReferenceNode(parameter.type) &&
|
|
87
|
+
ts.isIdentifier(parameter.type.typeName) && parameter.type.typeName.text === 'Ref'),
|
|
88
|
+
)) return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
function isPropsReference(node) {
|
|
93
|
+
return isParameterReference(node) ||
|
|
94
|
+
(ts.isPropertyAccessExpression(node) && node.expression.kind === ts.SyntaxKind.ThisKeyword && node.name.text === 'props');
|
|
95
|
+
}
|
|
96
|
+
function visit(node) {
|
|
97
|
+
if (ts.isPropertySignature(node) && isProp(node.name)) return true;
|
|
98
|
+
if (ts.isBindingElement(node) && isProp(node.propertyName ?? node.name)) {
|
|
99
|
+
const { parent } = node.parent;
|
|
100
|
+
if (ts.isParameter(parent) || (ts.isVariableDeclaration(parent) &&
|
|
101
|
+
parent.initializer && isPropsReference(parent.initializer))) return true;
|
|
102
|
+
}
|
|
103
|
+
if ((ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) &&
|
|
104
|
+
isProp(node.name ?? node.argumentExpression) && isPropsReference(node.expression)) return true;
|
|
105
|
+
return ts.forEachChild(node, visit);
|
|
106
|
+
}
|
|
107
|
+
return !!visit(tree);
|
|
108
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { WEB_DIR } from '../config.mjs';
|
|
4
|
+
|
|
5
|
+
export const pageUrlImport = "import { buildAppPageUrl } from '@macrowing/inbiz-runtime';\n";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Compile exact same-application publication URLs to standalone page URLs.
|
|
9
|
+
* Only literal routes from the pull are eligible. Never rewrite menu aliases,
|
|
10
|
+
* an API path, a different application/target, a comment, or an external origin.
|
|
11
|
+
*/
|
|
12
|
+
export function compilePublishedPageLinks(script, { domain, routes, menuRoutes, target = 'web' } = {}) {
|
|
13
|
+
const prefix = `/inbiz/${target}/${domain}`;
|
|
14
|
+
if (!domain || !routes?.size || !script?.includes(prefix)) return script;
|
|
15
|
+
|
|
16
|
+
const ts = createRequire(path.join(WEB_DIR, 'package.json'))('typescript');
|
|
17
|
+
const source = ts.createSourceFile('page-extends.ts', script, ts.ScriptTarget.Latest, true);
|
|
18
|
+
const edits = [];
|
|
19
|
+
// Look for the pathname/hash delimiters in literal text, not in interpolated
|
|
20
|
+
// expressions. Restore the source expressions verbatim after splitting.
|
|
21
|
+
let marker = '\u0000';
|
|
22
|
+
while (script.includes(marker)) marker += '\u0000';
|
|
23
|
+
function visit(node) {
|
|
24
|
+
const template = ts.isTemplateExpression(node);
|
|
25
|
+
if (template || ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
26
|
+
if (ts.isImportDeclaration(node.parent) || ts.isExportDeclaration(node.parent) ||
|
|
27
|
+
ts.isTaggedTemplateExpression(node.parent) || ts.isLiteralTypeNode(node.parent) ||
|
|
28
|
+
node.parent.name === node) return;
|
|
29
|
+
const expressions = [];
|
|
30
|
+
let text = node.text;
|
|
31
|
+
if (template) {
|
|
32
|
+
text = node.head.rawText ?? node.head.text;
|
|
33
|
+
for (const span of node.templateSpans) {
|
|
34
|
+
expressions.push('${' + span.expression.getText(source) + '}');
|
|
35
|
+
text += marker + (span.literal.rawText ?? span.literal.text);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (text.startsWith(prefix)) {
|
|
39
|
+
const link = /^([^#]*)#\/(page\/)?([\w.-]+)(\?[\s\S]*)?$/.exec(text.slice(prefix.length));
|
|
40
|
+
if (link && routes.has(link[3]) && (link[2] || !menuRoutes?.has(link[3]))) {
|
|
41
|
+
const [, entry = '', , route, query = ''] = link;
|
|
42
|
+
const queryOnly = (value) => ts.isConditionalExpression(value)
|
|
43
|
+
? queryOnly(value.whenTrue) && queryOnly(value.whenFalse)
|
|
44
|
+
: (ts.isStringLiteralLike(value) ? value.text === '' || value.text.startsWith('?')
|
|
45
|
+
: ts.isTemplateExpression(value) && value.head.text.startsWith('?'));
|
|
46
|
+
if (entry && !entry.startsWith('?') &&
|
|
47
|
+
!(entry === marker && template && queryOnly(node.templateSpans[0].expression))) return;
|
|
48
|
+
const restore = (part) => part.replaceAll(marker, () => expressions.shift());
|
|
49
|
+
const literal = (part) => template ? '`' + restore(part) + '`' : JSON.stringify(part);
|
|
50
|
+
const args = JSON.stringify(route) + (entry && entry !== '?' ? ', ' + literal(entry) : '');
|
|
51
|
+
const call = `buildAppPageUrl(${args})`;
|
|
52
|
+
edits.push([node.getStart(source), node.end, query ? `(${call} + ${literal(query)})` : call]);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
ts.forEachChild(node, visit);
|
|
58
|
+
}
|
|
59
|
+
visit(source);
|
|
60
|
+
return edits.reverse().reduce((text, [start, end, value]) => text.slice(0, start) + value + text.slice(end), script);
|
|
61
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { WEB_DIR } from '../config.mjs';
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const workspaceRequire = createRequire(path.join(WEB_DIR, 'package.json'));
|
|
7
|
+
let ts;
|
|
8
|
+
function typescript() {
|
|
9
|
+
if (ts) return ts;
|
|
10
|
+
try {
|
|
11
|
+
return ts = require('typescript');
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (error.code !== 'MODULE_NOT_FOUND') throw error;
|
|
14
|
+
return ts = workspaceRequire('typescript');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function collectPageFieldRefs(scripts) {
|
|
19
|
+
const ts = typescript();
|
|
20
|
+
const names = new Set();
|
|
21
|
+
for (const script of scripts) {
|
|
22
|
+
if (!script?.includes('inbizGlobal')) continue;
|
|
23
|
+
const source = ts.createSourceFile('extension.js', script, ts.ScriptTarget.Latest, true);
|
|
24
|
+
function visit(node) {
|
|
25
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
|
|
26
|
+
const { expression: lookup, name } = node.expression;
|
|
27
|
+
const [target] = node.arguments;
|
|
28
|
+
if (name.text === 'page' && ts.isCallExpression(lookup) &&
|
|
29
|
+
ts.isIdentifier(lookup.expression) && lookup.expression.text === 'inbizGlobal' &&
|
|
30
|
+
target && ts.isStringLiteralLike(target)) names.add(target.text);
|
|
31
|
+
}
|
|
32
|
+
ts.forEachChild(node, visit);
|
|
33
|
+
}
|
|
34
|
+
visit(source);
|
|
35
|
+
}
|
|
36
|
+
return names;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Platform scripts run via new Function('inbiz', body), without implicit
|
|
41
|
+
* strict mode. Make their undeclared assignment targets explicit in modules.
|
|
42
|
+
* Bare reads still resolve through the global environment (and still fail if
|
|
43
|
+
* nothing has initialized the name); deferred writes run when the callback does.
|
|
44
|
+
*/
|
|
45
|
+
export function compilePublishedScript(script = '') {
|
|
46
|
+
if (!script.trim()) return { body: script, globalsParam: '' };
|
|
47
|
+
const ts = typescript();
|
|
48
|
+
const file = 'published-script.ts';
|
|
49
|
+
const prefix = 'function published(inbiz) {\n';
|
|
50
|
+
const source = ts.createSourceFile(file, `${prefix}${script}\n}`, ts.ScriptTarget.Latest, true);
|
|
51
|
+
const program = ts.createProgram([file], { noLib: true, noResolve: true, types: [] }, {
|
|
52
|
+
getSourceFile: name => name === file ? source : undefined,
|
|
53
|
+
getDefaultLibFileName: () => '',
|
|
54
|
+
writeFile: () => {},
|
|
55
|
+
getCurrentDirectory: () => '',
|
|
56
|
+
getDirectories: () => [],
|
|
57
|
+
fileExists: name => name === file,
|
|
58
|
+
readFile: name => name === file ? source.text : undefined,
|
|
59
|
+
getCanonicalFileName: name => name,
|
|
60
|
+
useCaseSensitiveFileNames: () => true,
|
|
61
|
+
getNewLine: () => '\n',
|
|
62
|
+
});
|
|
63
|
+
const body = source.statements[0].body;
|
|
64
|
+
const checker = program.getTypeChecker();
|
|
65
|
+
const edits = [];
|
|
66
|
+
let globalAlias;
|
|
67
|
+
function strict(block) {
|
|
68
|
+
for (const statement of block.statements) {
|
|
69
|
+
if (!ts.isExpressionStatement(statement) || !ts.isStringLiteral(statement.expression)) break;
|
|
70
|
+
if (['"use strict"', "'use strict'"].includes(statement.expression.getText(source))) return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
function visit(node) {
|
|
75
|
+
if (ts.isClassLike(node) || (ts.isFunctionLike(node) && node.body && ts.isBlock(node.body) && strict(node.body))) return;
|
|
76
|
+
if (ts.isIdentifier(node) && ts.getAssignmentTargetKind(node) === ts.AssignmentKind.Definite
|
|
77
|
+
&& !(ts.isPropertyAssignment(node.parent) && node.parent.name === node)
|
|
78
|
+
&& !checker.resolveName(node.text, node, ts.SymbolFlags.Value, false)) {
|
|
79
|
+
const start = node.getStart(source);
|
|
80
|
+
const name = node.getText(source);
|
|
81
|
+
/**
|
|
82
|
+
* 改写目标统一走一个可注入的别名,而不是直接写 `globalThis`。
|
|
83
|
+
*
|
|
84
|
+
* 这个编译单元是 `noLib: true` / `noResolve: true` 造的:没有 lib.d.ts,
|
|
85
|
+
* `globalThis` 在类型检查器里**永远解析不到声明**。原来的判据是
|
|
86
|
+
* 「`globalThis` 能解析到声明时才用别名」,于是这个分支一次都不会进,
|
|
87
|
+
* 产物退化成 `globalThis.xxx = ...` —— 发布页里的全局写入再也注入不进去,
|
|
88
|
+
* `publishedGlobals = globalThis` 默认参数也一起消失,重新出码与基线对不上。
|
|
89
|
+
*
|
|
90
|
+
* 现在无条件用别名:语义不变(默认值就是 `globalThis`),但宿主/测试
|
|
91
|
+
* 可以传入自己的全局对象。
|
|
92
|
+
*/
|
|
93
|
+
if (!globalAlias) {
|
|
94
|
+
globalAlias = 'publishedGlobals';
|
|
95
|
+
while (script.includes(globalAlias)) globalAlias += '_';
|
|
96
|
+
}
|
|
97
|
+
const target = `${globalAlias || 'globalThis'}.${name}`;
|
|
98
|
+
edits.push([start - prefix.length, node.end - prefix.length,
|
|
99
|
+
ts.isShorthandPropertyAssignment(node.parent) ? `${name}: ${target}` : target]);
|
|
100
|
+
}
|
|
101
|
+
ts.forEachChild(node, visit);
|
|
102
|
+
}
|
|
103
|
+
if (!strict(body)) visit(body);
|
|
104
|
+
return {
|
|
105
|
+
body: edits.reverse().reduce((text, [start, end, target]) =>
|
|
106
|
+
text.slice(0, start) + target + text.slice(end), script),
|
|
107
|
+
// Default parameters are evaluated outside the function body's scope.
|
|
108
|
+
globalsParam: globalAlias ? `${globalAlias} = globalThis` : '',
|
|
109
|
+
};
|
|
110
|
+
}
|