@nocobase/flow-engine 2.0.0-beta.9 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/BlockScopedFlowEngine.js +0 -1
- package/lib/FlowDefinition.d.ts +2 -0
- package/lib/JSRunner.d.ts +6 -0
- package/lib/JSRunner.js +32 -2
- package/lib/ViewScopedFlowEngine.js +3 -0
- package/lib/acl/Acl.js +13 -3
- package/lib/components/FlowContextSelector.js +155 -10
- package/lib/components/settings/wrappers/component/SwitchWithTitle.js +2 -1
- package/lib/components/settings/wrappers/contextual/DefaultSettingsIcon.js +76 -15
- package/lib/components/settings/wrappers/contextual/FlowsContextMenu.js +24 -4
- package/lib/components/settings/wrappers/contextual/StepSettingsDialog.js +5 -1
- package/lib/components/variables/VariableInput.js +9 -4
- package/lib/components/variables/VariableTag.js +46 -39
- package/lib/components/variables/utils.d.ts +7 -0
- package/lib/components/variables/utils.js +42 -2
- package/lib/data-source/index.d.ts +7 -27
- package/lib/data-source/index.js +81 -51
- package/lib/executor/FlowExecutor.d.ts +2 -1
- package/lib/executor/FlowExecutor.js +163 -22
- package/lib/flowContext.d.ts +230 -7
- package/lib/flowContext.js +2267 -148
- package/lib/flowEngine.d.ts +21 -0
- package/lib/flowEngine.js +56 -8
- package/lib/flowI18n.js +6 -4
- package/lib/flowSettings.js +17 -11
- package/lib/index.d.ts +7 -1
- package/lib/index.js +21 -0
- package/lib/locale/en-US.json +9 -2
- package/lib/locale/index.d.ts +14 -0
- package/lib/locale/zh-CN.json +8 -1
- package/lib/models/CollectionFieldModel.d.ts +1 -0
- package/lib/models/CollectionFieldModel.js +3 -2
- package/lib/models/flowModel.js +12 -1
- package/lib/provider.js +5 -5
- package/lib/resources/baseRecordResource.d.ts +5 -0
- package/lib/resources/baseRecordResource.js +24 -0
- package/lib/resources/multiRecordResource.d.ts +1 -0
- package/lib/resources/multiRecordResource.js +11 -4
- package/lib/resources/singleRecordResource.js +2 -0
- package/lib/resources/sqlResource.d.ts +4 -3
- package/lib/resources/sqlResource.js +8 -3
- package/lib/runjs-context/contexts/FormJSFieldItemRunJSContext.js +12 -2
- package/lib/runjs-context/contexts/JSBlockRunJSContext.js +2 -2
- package/lib/runjs-context/contexts/JSEditableFieldRunJSContext.d.ts +16 -0
- package/lib/runjs-context/contexts/JSEditableFieldRunJSContext.js +125 -0
- package/lib/runjs-context/contexts/JSItemRunJSContext.js +12 -2
- package/lib/runjs-context/contexts/base.js +706 -41
- package/lib/runjs-context/contributions.d.ts +33 -0
- package/lib/runjs-context/contributions.js +88 -0
- package/lib/runjs-context/helpers.js +12 -1
- package/lib/runjs-context/setup.js +6 -0
- package/lib/runjs-context/snippets/global/api-request.snippet.js +3 -3
- package/lib/runjs-context/snippets/global/import-esm.snippet.js +2 -3
- package/lib/runjs-context/snippets/global/query-selector.snippet.js +8 -3
- package/lib/runjs-context/snippets/global/require-amd.snippet.js +1 -1
- package/lib/runjs-context/snippets/index.d.ts +11 -1
- package/lib/runjs-context/snippets/index.js +61 -40
- package/lib/runjs-context/snippets/scene/block/add-event-listener.snippet.js +10 -7
- package/lib/runjs-context/snippets/scene/block/api-fetch-render-list.snippet.js +3 -3
- package/lib/runjs-context/snippets/scene/block/chartjs-bar.snippet.js +2 -2
- package/lib/runjs-context/snippets/scene/block/echarts-init.snippet.js +2 -2
- package/lib/runjs-context/snippets/scene/block/render-iframe.snippet.js +2 -2
- package/lib/runjs-context/snippets/scene/block/render-react.snippet.js +1 -1
- package/lib/runjs-context/snippets/scene/block/render-statistics.snippet.js +1 -1
- package/lib/runjs-context/snippets/scene/block/render-timeline.snippet.js +1 -1
- package/lib/runjs-context/snippets/scene/block/resource-example.snippet.js +5 -5
- package/lib/runjs-context/snippets/scene/block/three-users-orbit.snippet.js +6 -6
- package/lib/runjs-context/snippets/scene/block/vue-component.snippet.js +3 -4
- package/lib/runjs-context/snippets/scene/detail/color-by-value.snippet.js +1 -1
- package/lib/runjs-context/snippets/scene/detail/copy-to-clipboard.snippet.js +20 -3
- package/lib/runjs-context/snippets/scene/detail/format-number.snippet.js +1 -1
- package/lib/runjs-context/snippets/scene/detail/innerHTML-value.snippet.js +1 -1
- package/lib/runjs-context/snippets/scene/detail/percentage-bar.snippet.js +3 -3
- package/lib/runjs-context/snippets/scene/detail/relative-time.snippet.js +3 -3
- package/lib/runjs-context/snippets/scene/detail/status-tag.snippet.js +2 -2
- package/lib/runjs-context/snippets/scene/form/cascade-select.snippet.js +1 -1
- package/lib/runjs-context/snippets/scene/form/render-basic.snippet.js +2 -2
- package/lib/runjs-context/snippets/scene/table/cell-open-dialog.snippet.js +6 -3
- package/lib/runjs-context/snippets/scene/table/concat-fields.snippet.js +3 -1
- package/lib/runjsLibs.d.ts +28 -0
- package/lib/runjsLibs.js +532 -0
- package/lib/scheduler/ModelOperationScheduler.d.ts +2 -0
- package/lib/scheduler/ModelOperationScheduler.js +25 -21
- package/lib/types.d.ts +27 -0
- package/lib/utils/associationObjectVariable.d.ts +2 -2
- package/lib/utils/createCollectionContextMeta.js +1 -0
- package/lib/utils/createEphemeralContext.js +2 -2
- package/lib/utils/dateVariable.d.ts +16 -0
- package/lib/utils/dateVariable.js +380 -0
- package/lib/utils/exceptions.d.ts +7 -0
- package/lib/utils/exceptions.js +10 -0
- package/lib/utils/index.d.ts +8 -3
- package/lib/utils/index.js +45 -0
- package/lib/utils/params-resolvers.js +16 -9
- package/lib/utils/resolveModuleUrl.d.ts +58 -0
- package/lib/utils/resolveModuleUrl.js +65 -0
- package/lib/utils/resolveRunJSObjectValues.d.ts +16 -0
- package/lib/utils/resolveRunJSObjectValues.js +61 -0
- package/lib/utils/runjsModuleLoader.d.ts +58 -0
- package/lib/utils/runjsModuleLoader.js +422 -0
- package/lib/utils/runjsTemplateCompat.d.ts +35 -0
- package/lib/utils/runjsTemplateCompat.js +743 -0
- package/lib/utils/runjsValue.d.ts +29 -0
- package/lib/utils/runjsValue.js +275 -0
- package/lib/utils/safeGlobals.d.ts +18 -8
- package/lib/utils/safeGlobals.js +164 -17
- package/lib/utils/schema-utils.d.ts +10 -0
- package/lib/utils/schema-utils.js +61 -0
- package/lib/views/createViewMeta.d.ts +0 -7
- package/lib/views/createViewMeta.js +19 -70
- package/lib/views/index.d.ts +1 -2
- package/lib/views/index.js +4 -3
- package/lib/views/useDialog.js +7 -2
- package/lib/views/useDrawer.js +7 -2
- package/lib/views/usePage.d.ts +4 -0
- package/lib/views/usePage.js +43 -6
- package/lib/views/usePopover.js +4 -1
- package/lib/views/viewEvents.d.ts +17 -0
- package/lib/views/viewEvents.js +90 -0
- package/package.json +4 -4
- package/src/BlockScopedFlowEngine.ts +2 -5
- package/src/JSRunner.ts +44 -2
- package/src/ViewScopedFlowEngine.ts +4 -0
- package/src/__tests__/JSRunner.test.ts +64 -0
- package/src/__tests__/createViewMeta.popup.test.ts +62 -1
- package/src/__tests__/flowContext.test.ts +693 -1
- package/src/__tests__/flowEngine.dataSourceDirty.test.ts +63 -0
- package/src/__tests__/flowModel.openView.navigation.test.ts +28 -0
- package/src/__tests__/flowRunJSContextDefine.test.ts +63 -0
- package/src/__tests__/flowRuntimeContext.test.ts +2 -1
- package/src/__tests__/flowSettings.open.test.tsx +123 -19
- package/src/__tests__/runjsContext.test.ts +10 -7
- package/src/__tests__/runjsContextImplementations.test.ts +34 -3
- package/src/__tests__/runjsContextRuntime.test.ts +3 -3
- package/src/__tests__/runjsContributions.test.ts +89 -0
- package/src/__tests__/runjsExternalLibs.test.ts +242 -0
- package/src/__tests__/runjsLibsLazyLoading.test.ts +44 -0
- package/src/__tests__/runjsLocales.test.ts +4 -1
- package/src/__tests__/runjsPreprocessDefault.test.ts +49 -0
- package/src/__tests__/runjsRuntimeFeatures.test.ts +166 -0
- package/src/__tests__/runjsSnippets.test.ts +40 -3
- package/src/acl/Acl.tsx +3 -3
- package/src/components/FlowContextSelector.tsx +208 -12
- package/src/components/settings/wrappers/component/SwitchWithTitle.tsx +2 -1
- package/src/components/settings/wrappers/component/__tests__/InlineControls.test.tsx +74 -0
- package/src/components/settings/wrappers/contextual/DefaultSettingsIcon.tsx +109 -16
- package/src/components/settings/wrappers/contextual/FlowsContextMenu.tsx +41 -7
- package/src/components/settings/wrappers/contextual/StepSettingsDialog.tsx +13 -2
- package/src/components/settings/wrappers/contextual/__tests__/DefaultSettingsIcon.test.tsx +157 -5
- package/src/components/variables/VariableInput.tsx +12 -4
- package/src/components/variables/VariableTag.tsx +54 -45
- package/src/components/variables/__tests__/FlowContextSelector.test.tsx +260 -3
- package/src/components/variables/__tests__/VariableTag.test.tsx +50 -0
- package/src/components/variables/__tests__/utils.test.ts +81 -3
- package/src/components/variables/utils.ts +67 -6
- package/src/data-source/index.ts +85 -110
- package/src/executor/FlowExecutor.ts +200 -23
- package/src/executor/__tests__/flowExecutor.test.ts +66 -0
- package/src/flowContext.ts +2986 -211
- package/src/flowEngine.ts +59 -8
- package/src/flowI18n.ts +7 -5
- package/src/flowSettings.ts +18 -12
- package/src/index.ts +14 -1
- package/src/locale/en-US.json +9 -2
- package/src/locale/zh-CN.json +8 -1
- package/src/models/CollectionFieldModel.tsx +3 -1
- package/src/models/__tests__/dispatchEvent.when.test.ts +554 -0
- package/src/models/__tests__/flowModel.test.ts +20 -4
- package/src/models/flowModel.tsx +13 -1
- package/src/provider.tsx +7 -6
- package/src/resources/__tests__/multiRecordResource.test.ts +44 -0
- package/src/resources/__tests__/sqlResource.test.ts +60 -0
- package/src/resources/baseRecordResource.ts +31 -0
- package/src/resources/multiRecordResource.ts +11 -4
- package/src/resources/singleRecordResource.ts +3 -0
- package/src/resources/sqlResource.ts +11 -6
- package/src/runjs-context/contexts/FormJSFieldItemRunJSContext.ts +10 -0
- package/src/runjs-context/contexts/JSBlockRunJSContext.ts +6 -2
- package/src/runjs-context/contexts/JSEditableFieldRunJSContext.ts +106 -0
- package/src/runjs-context/contexts/JSItemRunJSContext.ts +10 -0
- package/src/runjs-context/contexts/base.ts +715 -44
- package/src/runjs-context/contributions.ts +88 -0
- package/src/runjs-context/helpers.ts +11 -1
- package/src/runjs-context/setup.ts +6 -0
- package/src/runjs-context/snippets/global/api-request.snippet.ts +3 -3
- package/src/runjs-context/snippets/global/import-esm.snippet.ts +2 -3
- package/src/runjs-context/snippets/global/query-selector.snippet.ts +8 -3
- package/src/runjs-context/snippets/global/require-amd.snippet.ts +1 -1
- package/src/runjs-context/snippets/index.ts +75 -41
- package/src/runjs-context/snippets/scene/block/add-event-listener.snippet.ts +11 -13
- package/src/runjs-context/snippets/scene/block/api-fetch-render-list.snippet.ts +3 -3
- package/src/runjs-context/snippets/scene/block/chartjs-bar.snippet.ts +2 -2
- package/src/runjs-context/snippets/scene/block/echarts-init.snippet.ts +2 -2
- package/src/runjs-context/snippets/scene/block/render-iframe.snippet.ts +2 -2
- package/src/runjs-context/snippets/scene/block/render-react.snippet.ts +1 -1
- package/src/runjs-context/snippets/scene/block/render-statistics.snippet.ts +1 -1
- package/src/runjs-context/snippets/scene/block/render-timeline.snippet.ts +1 -1
- package/src/runjs-context/snippets/scene/block/resource-example.snippet.ts +6 -11
- package/src/runjs-context/snippets/scene/block/three-users-orbit.snippet.ts +6 -6
- package/src/runjs-context/snippets/scene/block/vue-component.snippet.ts +3 -4
- package/src/runjs-context/snippets/scene/detail/color-by-value.snippet.ts +1 -1
- package/src/runjs-context/snippets/scene/detail/copy-to-clipboard.snippet.ts +20 -3
- package/src/runjs-context/snippets/scene/detail/format-number.snippet.ts +1 -1
- package/src/runjs-context/snippets/scene/detail/innerHTML-value.snippet.ts +1 -1
- package/src/runjs-context/snippets/scene/detail/percentage-bar.snippet.ts +3 -3
- package/src/runjs-context/snippets/scene/detail/relative-time.snippet.ts +3 -3
- package/src/runjs-context/snippets/scene/detail/status-tag.snippet.ts +2 -2
- package/src/runjs-context/snippets/scene/form/cascade-select.snippet.ts +1 -1
- package/src/runjs-context/snippets/scene/form/render-basic.snippet.ts +3 -8
- package/src/runjs-context/snippets/scene/table/cell-open-dialog.snippet.ts +6 -3
- package/src/runjs-context/snippets/scene/table/concat-fields.snippet.ts +3 -1
- package/src/runjsLibs.ts +622 -0
- package/src/scheduler/ModelOperationScheduler.ts +27 -21
- package/src/types.ts +38 -1
- package/src/utils/__tests__/dateVariable.test.ts +101 -0
- package/src/utils/__tests__/params-resolvers.test.ts +40 -0
- package/src/utils/__tests__/runjsRequireAsyncAutoWhitelist.test.ts +38 -0
- package/src/utils/__tests__/runjsTemplateCompat.test.ts +159 -0
- package/src/utils/__tests__/runjsValue.test.ts +44 -0
- package/src/utils/__tests__/safeGlobals.test.ts +57 -2
- package/src/utils/__tests__/utils.test.ts +95 -0
- package/src/utils/associationObjectVariable.ts +2 -2
- package/src/utils/createCollectionContextMeta.ts +1 -0
- package/src/utils/createEphemeralContext.ts +5 -4
- package/src/utils/dateVariable.ts +397 -0
- package/src/utils/exceptions.ts +11 -0
- package/src/utils/index.ts +37 -3
- package/src/utils/params-resolvers.ts +23 -9
- package/src/utils/resolveModuleUrl.ts +91 -0
- package/src/utils/resolveRunJSObjectValues.ts +46 -0
- package/src/utils/runjsModuleLoader.ts +553 -0
- package/src/utils/runjsTemplateCompat.ts +828 -0
- package/src/utils/runjsValue.ts +287 -0
- package/src/utils/safeGlobals.ts +188 -17
- package/src/utils/schema-utils.ts +79 -0
- package/src/views/__tests__/FlowView.usePage.test.tsx +54 -1
- package/src/views/__tests__/useDialog.closeDestroy.test.tsx +35 -8
- package/src/views/__tests__/viewEvents.resolveOpenerEngine.test.ts +28 -0
- package/src/views/createViewMeta.ts +22 -75
- package/src/views/index.tsx +1 -2
- package/src/views/useDialog.tsx +8 -1
- package/src/views/useDrawer.tsx +8 -1
- package/src/views/usePage.tsx +51 -5
- package/src/views/usePopover.tsx +4 -1
- package/src/views/viewEvents.ts +55 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var runjsTemplateCompat_exports = {};
|
|
29
|
+
__export(runjsTemplateCompat_exports, {
|
|
30
|
+
prepareRunJsCode: () => prepareRunJsCode,
|
|
31
|
+
preprocessRunJsTemplates: () => preprocessRunJsTemplates
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(runjsTemplateCompat_exports);
|
|
34
|
+
var import_jsxTransform = require("./jsxTransform");
|
|
35
|
+
const RESOLVE_JSON_TEMPLATE_CALL = "ctx.resolveJsonTemplate";
|
|
36
|
+
const CTX_TEMPLATE_MARKER_RE = /\{\{\s*ctx(?:\.|\[|\?\.)/;
|
|
37
|
+
const CTX_LIBS_MARKER_RE = /\bctx(?:\?\.|\.)libs\b/;
|
|
38
|
+
const STRINGIFY_HELPER_BASE_NAME = "__runjs_templateValueToString";
|
|
39
|
+
const BARE_PLACEHOLDER_VAR_RE = /\b__runjs_ctx_tpl_\d+\b/;
|
|
40
|
+
const STRINGIFY_HELPER_RE = /\b__runjs_templateValueToString(?:_\d+)?\b/;
|
|
41
|
+
const PREPROCESSED_MARKER_RE = /\b__runjs_ctx_tpl_\d+\b|\b__runjs_templateValueToString(?:_\d+)?\b/;
|
|
42
|
+
const ENSURE_LIBS_MARKER_RE = /\b__runjs_ensure_libs\b/;
|
|
43
|
+
const PREPARE_RUNJS_CODE_CACHE_LIMIT = 256;
|
|
44
|
+
const PREPARE_RUNJS_CODE_CACHE = {
|
|
45
|
+
withTemplates: /* @__PURE__ */ new Map(),
|
|
46
|
+
withoutTemplates: /* @__PURE__ */ new Map()
|
|
47
|
+
};
|
|
48
|
+
function lruGet(map, key) {
|
|
49
|
+
const v = map.get(key);
|
|
50
|
+
if (typeof v === "undefined") return void 0;
|
|
51
|
+
map.delete(key);
|
|
52
|
+
map.set(key, v);
|
|
53
|
+
return v;
|
|
54
|
+
}
|
|
55
|
+
__name(lruGet, "lruGet");
|
|
56
|
+
function lruSet(map, key, value, limit) {
|
|
57
|
+
if (map.has(key)) map.delete(key);
|
|
58
|
+
map.set(key, value);
|
|
59
|
+
if (map.size <= limit) return;
|
|
60
|
+
const oldestKey = map.keys().next().value;
|
|
61
|
+
if (typeof oldestKey !== "undefined") map.delete(oldestKey);
|
|
62
|
+
}
|
|
63
|
+
__name(lruSet, "lruSet");
|
|
64
|
+
function isIdentChar(ch) {
|
|
65
|
+
return !!ch && /[A-Za-z0-9_$]/.test(ch);
|
|
66
|
+
}
|
|
67
|
+
__name(isIdentChar, "isIdentChar");
|
|
68
|
+
function isCtxTemplatePlaceholder(placeholder) {
|
|
69
|
+
if (!placeholder.startsWith("{{") || !placeholder.endsWith("}}")) return false;
|
|
70
|
+
const inner = placeholder.slice(2, -2).trim();
|
|
71
|
+
return inner.startsWith("ctx.") || inner.startsWith("ctx[") || inner.startsWith("ctx?.");
|
|
72
|
+
}
|
|
73
|
+
__name(isCtxTemplatePlaceholder, "isCtxTemplatePlaceholder");
|
|
74
|
+
function readsResolveJsonTemplateCall(code, index) {
|
|
75
|
+
if (!code.startsWith(RESOLVE_JSON_TEMPLATE_CALL, index)) return false;
|
|
76
|
+
const before = index > 0 ? code[index - 1] : "";
|
|
77
|
+
const after = code[index + RESOLVE_JSON_TEMPLATE_CALL.length] || "";
|
|
78
|
+
if (isIdentChar(before) || isIdentChar(after)) return false;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
__name(readsResolveJsonTemplateCall, "readsResolveJsonTemplateCall");
|
|
82
|
+
function readLineComment(code, start) {
|
|
83
|
+
let i = start + 2;
|
|
84
|
+
while (i < code.length) {
|
|
85
|
+
const ch = code[i];
|
|
86
|
+
i += 1;
|
|
87
|
+
if (ch === "\n") break;
|
|
88
|
+
}
|
|
89
|
+
return i;
|
|
90
|
+
}
|
|
91
|
+
__name(readLineComment, "readLineComment");
|
|
92
|
+
function readBlockComment(code, start) {
|
|
93
|
+
let i = start + 2;
|
|
94
|
+
while (i < code.length) {
|
|
95
|
+
const ch = code[i];
|
|
96
|
+
const next = code[i + 1];
|
|
97
|
+
if (ch === "*" && next === "/") {
|
|
98
|
+
return i + 2;
|
|
99
|
+
}
|
|
100
|
+
i += 1;
|
|
101
|
+
}
|
|
102
|
+
return i;
|
|
103
|
+
}
|
|
104
|
+
__name(readBlockComment, "readBlockComment");
|
|
105
|
+
function readQuotedString(code, start, quote) {
|
|
106
|
+
let i = start + 1;
|
|
107
|
+
while (i < code.length) {
|
|
108
|
+
const ch = code[i];
|
|
109
|
+
if (ch === "\\") {
|
|
110
|
+
i += 2;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
i += 1;
|
|
114
|
+
if (ch === quote) break;
|
|
115
|
+
}
|
|
116
|
+
return i;
|
|
117
|
+
}
|
|
118
|
+
__name(readQuotedString, "readQuotedString");
|
|
119
|
+
function readTemplateLiteral(code, start) {
|
|
120
|
+
let i = start + 1;
|
|
121
|
+
while (i < code.length) {
|
|
122
|
+
const ch = code[i];
|
|
123
|
+
if (ch === "\\") {
|
|
124
|
+
i += 2;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (ch === "`") {
|
|
128
|
+
return i + 1;
|
|
129
|
+
}
|
|
130
|
+
if (ch === "$" && code[i + 1] === "{") {
|
|
131
|
+
i += 2;
|
|
132
|
+
let depth = 1;
|
|
133
|
+
while (i < code.length && depth > 0) {
|
|
134
|
+
const c = code[i];
|
|
135
|
+
const n = code[i + 1];
|
|
136
|
+
if (c === "'" || c === '"') {
|
|
137
|
+
i = readQuotedString(code, i, c);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (c === "`") {
|
|
141
|
+
i = readTemplateLiteral(code, i);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (c === "/" && n === "/") {
|
|
145
|
+
i = readLineComment(code, i);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (c === "/" && n === "*") {
|
|
149
|
+
i = readBlockComment(code, i);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (c === "{") depth += 1;
|
|
153
|
+
else if (c === "}") depth -= 1;
|
|
154
|
+
i += 1;
|
|
155
|
+
}
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
i += 1;
|
|
159
|
+
}
|
|
160
|
+
return i;
|
|
161
|
+
}
|
|
162
|
+
__name(readTemplateLiteral, "readTemplateLiteral");
|
|
163
|
+
function extractCtxPlaceholders(source) {
|
|
164
|
+
if (!CTX_TEMPLATE_MARKER_RE.test(source)) return [];
|
|
165
|
+
const out = [];
|
|
166
|
+
let idx = 0;
|
|
167
|
+
while (idx < source.length) {
|
|
168
|
+
const start = source.indexOf("{{", idx);
|
|
169
|
+
if (start === -1) break;
|
|
170
|
+
const end = source.indexOf("}}", start + 2);
|
|
171
|
+
if (end === -1) break;
|
|
172
|
+
const placeholder = source.slice(start, end + 2);
|
|
173
|
+
if (isCtxTemplatePlaceholder(placeholder)) out.push(placeholder);
|
|
174
|
+
idx = end + 2;
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
__name(extractCtxPlaceholders, "extractCtxPlaceholders");
|
|
179
|
+
function skipWhitespaceForward(code, start) {
|
|
180
|
+
let i = start;
|
|
181
|
+
while (i < code.length && /\s/.test(code[i])) i += 1;
|
|
182
|
+
return i;
|
|
183
|
+
}
|
|
184
|
+
__name(skipWhitespaceForward, "skipWhitespaceForward");
|
|
185
|
+
function skipWhitespaceBackward(code, start) {
|
|
186
|
+
let i = start;
|
|
187
|
+
while (i >= 0 && /\s/.test(code[i])) i -= 1;
|
|
188
|
+
return i;
|
|
189
|
+
}
|
|
190
|
+
__name(skipWhitespaceBackward, "skipWhitespaceBackward");
|
|
191
|
+
function skipSpaceAndCommentsForward(code, start) {
|
|
192
|
+
let i = start;
|
|
193
|
+
for (; ; ) {
|
|
194
|
+
i = skipWhitespaceForward(code, i);
|
|
195
|
+
const ch = code[i];
|
|
196
|
+
const next = code[i + 1];
|
|
197
|
+
if (ch === "/" && next === "/") {
|
|
198
|
+
i = readLineComment(code, i);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (ch === "/" && next === "*") {
|
|
202
|
+
i = readBlockComment(code, i);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
return i;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
__name(skipSpaceAndCommentsForward, "skipSpaceAndCommentsForward");
|
|
209
|
+
function isIdentStartChar(ch) {
|
|
210
|
+
return !!ch && /[A-Za-z_$]/.test(ch);
|
|
211
|
+
}
|
|
212
|
+
__name(isIdentStartChar, "isIdentStartChar");
|
|
213
|
+
function readIdentifier(code, start) {
|
|
214
|
+
const first = code[start];
|
|
215
|
+
if (!isIdentStartChar(first)) return null;
|
|
216
|
+
let i = start + 1;
|
|
217
|
+
while (i < code.length && isIdentChar(code[i])) i += 1;
|
|
218
|
+
return { name: code.slice(start, i), end: i };
|
|
219
|
+
}
|
|
220
|
+
__name(readIdentifier, "readIdentifier");
|
|
221
|
+
function readSimpleStringLiteralValue(code, start, quote) {
|
|
222
|
+
if (code[start] !== quote) return null;
|
|
223
|
+
let i = start + 1;
|
|
224
|
+
let value = "";
|
|
225
|
+
while (i < code.length) {
|
|
226
|
+
const ch = code[i];
|
|
227
|
+
if (ch === "\\") {
|
|
228
|
+
const n = code[i + 1];
|
|
229
|
+
if (typeof n === "undefined") break;
|
|
230
|
+
value += n;
|
|
231
|
+
i += 2;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (ch === quote) {
|
|
235
|
+
return { value, end: i + 1 };
|
|
236
|
+
}
|
|
237
|
+
value += ch;
|
|
238
|
+
i += 1;
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
__name(readSimpleStringLiteralValue, "readSimpleStringLiteralValue");
|
|
243
|
+
function readsCtxLibsBase(code, index) {
|
|
244
|
+
if (!code.startsWith("ctx", index)) return null;
|
|
245
|
+
const before = index > 0 ? code[index - 1] : "";
|
|
246
|
+
const after = code[index + 3] || "";
|
|
247
|
+
if (isIdentChar(before) || isIdentChar(after)) return null;
|
|
248
|
+
const tail = code.slice(index + 3);
|
|
249
|
+
if (tail.startsWith(".libs")) return index + 3 + 5;
|
|
250
|
+
if (tail.startsWith("?.libs")) return index + 3 + 6;
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
__name(readsCtxLibsBase, "readsCtxLibsBase");
|
|
254
|
+
function tryReadCtxLibAccess(code, index) {
|
|
255
|
+
const baseEnd = readsCtxLibsBase(code, index);
|
|
256
|
+
if (baseEnd === null) return null;
|
|
257
|
+
let i = skipSpaceAndCommentsForward(code, baseEnd);
|
|
258
|
+
const ch = code[i];
|
|
259
|
+
const next = code[i + 1];
|
|
260
|
+
const next2 = code[i + 2];
|
|
261
|
+
if (ch === "?" && next === "." && next2 === "[") {
|
|
262
|
+
i = skipSpaceAndCommentsForward(code, i + 3);
|
|
263
|
+
const q = code[i];
|
|
264
|
+
if (q === "'" || q === '"') {
|
|
265
|
+
const parsed = readSimpleStringLiteralValue(code, i, q);
|
|
266
|
+
if (!parsed) return { end: i + 1 };
|
|
267
|
+
let j = skipSpaceAndCommentsForward(code, parsed.end);
|
|
268
|
+
if (code[j] === "]") j += 1;
|
|
269
|
+
return { end: j, key: parsed.value };
|
|
270
|
+
}
|
|
271
|
+
return { end: i };
|
|
272
|
+
}
|
|
273
|
+
if (ch === "[") {
|
|
274
|
+
i = skipSpaceAndCommentsForward(code, i + 1);
|
|
275
|
+
const q = code[i];
|
|
276
|
+
if (q === "'" || q === '"') {
|
|
277
|
+
const parsed = readSimpleStringLiteralValue(code, i, q);
|
|
278
|
+
if (!parsed) return { end: i + 1 };
|
|
279
|
+
let j = skipSpaceAndCommentsForward(code, parsed.end);
|
|
280
|
+
if (code[j] === "]") j += 1;
|
|
281
|
+
return { end: j, key: parsed.value };
|
|
282
|
+
}
|
|
283
|
+
return { end: i };
|
|
284
|
+
}
|
|
285
|
+
if (ch === "?" && next === ".") {
|
|
286
|
+
i = skipSpaceAndCommentsForward(code, i + 2);
|
|
287
|
+
const ident = readIdentifier(code, i);
|
|
288
|
+
if (!ident) return { end: i };
|
|
289
|
+
return { end: ident.end, key: ident.name };
|
|
290
|
+
}
|
|
291
|
+
if (ch === ".") {
|
|
292
|
+
i = skipSpaceAndCommentsForward(code, i + 1);
|
|
293
|
+
const ident = readIdentifier(code, i);
|
|
294
|
+
if (!ident) return { end: i };
|
|
295
|
+
return { end: ident.end, key: ident.name };
|
|
296
|
+
}
|
|
297
|
+
return { end: baseEnd };
|
|
298
|
+
}
|
|
299
|
+
__name(tryReadCtxLibAccess, "tryReadCtxLibAccess");
|
|
300
|
+
function parseDestructuredKeysFromObjectPattern(pattern) {
|
|
301
|
+
const out = [];
|
|
302
|
+
const src = String(pattern ?? "");
|
|
303
|
+
let itemStart = 0;
|
|
304
|
+
let braceDepth = 0;
|
|
305
|
+
let bracketDepth = 0;
|
|
306
|
+
let parenDepth = 0;
|
|
307
|
+
const pushItem = /* @__PURE__ */ __name((raw) => {
|
|
308
|
+
const s = String(raw ?? "").trim();
|
|
309
|
+
if (!s) return;
|
|
310
|
+
if (s.startsWith("...")) return;
|
|
311
|
+
let keyPart = s;
|
|
312
|
+
let i2 = 0;
|
|
313
|
+
let b = 0;
|
|
314
|
+
let br = 0;
|
|
315
|
+
let p = 0;
|
|
316
|
+
while (i2 < s.length) {
|
|
317
|
+
const ch = s[i2];
|
|
318
|
+
const next = s[i2 + 1];
|
|
319
|
+
if (ch === "/" && next === "/") {
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
if (ch === "/" && next === "*") {
|
|
323
|
+
const end = s.indexOf("*/", i2 + 2);
|
|
324
|
+
i2 = end === -1 ? s.length : end + 2;
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (ch === "'" || ch === '"') {
|
|
328
|
+
let j = i2 + 1;
|
|
329
|
+
while (j < s.length) {
|
|
330
|
+
const c = s[j];
|
|
331
|
+
if (c === "\\") {
|
|
332
|
+
j += 2;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
j += 1;
|
|
336
|
+
if (c === ch) break;
|
|
337
|
+
}
|
|
338
|
+
i2 = j;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (ch === "`") {
|
|
342
|
+
let j = i2 + 1;
|
|
343
|
+
while (j < s.length) {
|
|
344
|
+
const c = s[j];
|
|
345
|
+
if (c === "\\") {
|
|
346
|
+
j += 2;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
j += 1;
|
|
350
|
+
if (c === "`") break;
|
|
351
|
+
}
|
|
352
|
+
i2 = j;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
if (ch === "{") b += 1;
|
|
356
|
+
else if (ch === "}") b -= 1;
|
|
357
|
+
else if (ch === "[") br += 1;
|
|
358
|
+
else if (ch === "]") br -= 1;
|
|
359
|
+
else if (ch === "(") p += 1;
|
|
360
|
+
else if (ch === ")") p -= 1;
|
|
361
|
+
if (b === 0 && br === 0 && p === 0) {
|
|
362
|
+
if (ch === ":") {
|
|
363
|
+
keyPart = s.slice(0, i2).trim();
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
if (ch === "=") {
|
|
367
|
+
keyPart = s.slice(0, i2).trim();
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
i2 += 1;
|
|
372
|
+
}
|
|
373
|
+
if (!keyPart) return;
|
|
374
|
+
if (keyPart.startsWith("'") || keyPart.startsWith('"')) {
|
|
375
|
+
const q = keyPart[0];
|
|
376
|
+
const parsed = readSimpleStringLiteralValue(keyPart, 0, q);
|
|
377
|
+
if (parsed) out.push(parsed.value);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const m = keyPart.match(/^[A-Za-z_$][A-Za-z0-9_$]*/);
|
|
381
|
+
if (m) out.push(m[0]);
|
|
382
|
+
}, "pushItem");
|
|
383
|
+
let i = 0;
|
|
384
|
+
while (i < src.length) {
|
|
385
|
+
const ch = src[i];
|
|
386
|
+
const next = src[i + 1];
|
|
387
|
+
if (ch === "/" && next === "/") {
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
if (ch === "/" && next === "*") {
|
|
391
|
+
const end = src.indexOf("*/", i + 2);
|
|
392
|
+
i = end === -1 ? src.length : end + 2;
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
if (ch === "'" || ch === '"') {
|
|
396
|
+
i = readQuotedString(src, i, ch);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (ch === "`") {
|
|
400
|
+
i = readTemplateLiteral(src, i);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
if (ch === "{") braceDepth += 1;
|
|
404
|
+
else if (ch === "}") braceDepth -= 1;
|
|
405
|
+
else if (ch === "[") bracketDepth += 1;
|
|
406
|
+
else if (ch === "]") bracketDepth -= 1;
|
|
407
|
+
else if (ch === "(") parenDepth += 1;
|
|
408
|
+
else if (ch === ")") parenDepth -= 1;
|
|
409
|
+
if (braceDepth === 0 && bracketDepth === 0 && parenDepth === 0 && ch === ",") {
|
|
410
|
+
pushItem(src.slice(itemStart, i));
|
|
411
|
+
itemStart = i + 1;
|
|
412
|
+
}
|
|
413
|
+
i += 1;
|
|
414
|
+
}
|
|
415
|
+
pushItem(src.slice(itemStart));
|
|
416
|
+
return out;
|
|
417
|
+
}
|
|
418
|
+
__name(parseDestructuredKeysFromObjectPattern, "parseDestructuredKeysFromObjectPattern");
|
|
419
|
+
function extractUsedCtxLibKeys(code) {
|
|
420
|
+
if (!CTX_LIBS_MARKER_RE.test(code)) return [];
|
|
421
|
+
const out = /* @__PURE__ */ new Set();
|
|
422
|
+
const scanTemplateExpression = /* @__PURE__ */ __name((start) => {
|
|
423
|
+
let i2 = start;
|
|
424
|
+
let braceDepth = 1;
|
|
425
|
+
while (i2 < code.length && braceDepth > 0) {
|
|
426
|
+
const ch = code[i2];
|
|
427
|
+
const next = code[i2 + 1];
|
|
428
|
+
if (ch === "/" && next === "/") {
|
|
429
|
+
i2 = readLineComment(code, i2);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (ch === "/" && next === "*") {
|
|
433
|
+
i2 = readBlockComment(code, i2);
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
if (ch === "'" || ch === '"') {
|
|
437
|
+
i2 = readQuotedString(code, i2, ch);
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
if (ch === "`") {
|
|
441
|
+
i2 = scanTemplateLiteral(i2 + 1);
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
const access = tryReadCtxLibAccess(code, i2);
|
|
445
|
+
if (access) {
|
|
446
|
+
if (access.key) out.add(access.key);
|
|
447
|
+
i2 = access.end;
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (ch === "{") braceDepth += 1;
|
|
451
|
+
else if (ch === "}") braceDepth -= 1;
|
|
452
|
+
i2 += 1;
|
|
453
|
+
}
|
|
454
|
+
return i2;
|
|
455
|
+
}, "scanTemplateExpression");
|
|
456
|
+
const scanTemplateLiteral = /* @__PURE__ */ __name((start) => {
|
|
457
|
+
let i2 = start;
|
|
458
|
+
while (i2 < code.length) {
|
|
459
|
+
const ch = code[i2];
|
|
460
|
+
const next = code[i2 + 1];
|
|
461
|
+
if (ch === "\\") {
|
|
462
|
+
i2 += 2;
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
if (ch === "`") return i2 + 1;
|
|
466
|
+
if (ch === "$" && next === "{") {
|
|
467
|
+
i2 = scanTemplateExpression(i2 + 2);
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
i2 += 1;
|
|
471
|
+
}
|
|
472
|
+
return i2;
|
|
473
|
+
}, "scanTemplateLiteral");
|
|
474
|
+
let i = 0;
|
|
475
|
+
while (i < code.length) {
|
|
476
|
+
const ch = code[i];
|
|
477
|
+
const next = code[i + 1];
|
|
478
|
+
if (ch === "/" && next === "/") {
|
|
479
|
+
i = readLineComment(code, i);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (ch === "/" && next === "*") {
|
|
483
|
+
i = readBlockComment(code, i);
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
if (ch === "'" || ch === '"') {
|
|
487
|
+
i = readQuotedString(code, i, ch);
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (ch === "`") {
|
|
491
|
+
i = scanTemplateLiteral(i + 1);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
const access = tryReadCtxLibAccess(code, i);
|
|
495
|
+
if (access) {
|
|
496
|
+
if (access.key) out.add(access.key);
|
|
497
|
+
i = access.end;
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
if (ch === "=" && next !== "=" && next !== ">" && next !== "<") {
|
|
501
|
+
const right = skipSpaceAndCommentsForward(code, i + 1);
|
|
502
|
+
const rhsBaseEnd = readsCtxLibsBase(code, right);
|
|
503
|
+
if (rhsBaseEnd !== null) {
|
|
504
|
+
const leftEnd = skipWhitespaceBackward(code, i - 1);
|
|
505
|
+
if (code[leftEnd] === "}") {
|
|
506
|
+
let depth = 0;
|
|
507
|
+
let j = leftEnd;
|
|
508
|
+
while (j >= 0) {
|
|
509
|
+
const c = code[j];
|
|
510
|
+
if (c === "}") depth += 1;
|
|
511
|
+
else if (c === "{") {
|
|
512
|
+
depth -= 1;
|
|
513
|
+
if (depth === 0) break;
|
|
514
|
+
}
|
|
515
|
+
j -= 1;
|
|
516
|
+
}
|
|
517
|
+
if (j >= 0 && code[j] === "{") {
|
|
518
|
+
const inner = code.slice(j + 1, leftEnd);
|
|
519
|
+
for (const k of parseDestructuredKeysFromObjectPattern(inner)) out.add(k);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
i += 1;
|
|
525
|
+
}
|
|
526
|
+
return Array.from(out);
|
|
527
|
+
}
|
|
528
|
+
__name(extractUsedCtxLibKeys, "extractUsedCtxLibKeys");
|
|
529
|
+
function injectEnsureLibsPreamble(code) {
|
|
530
|
+
if (!CTX_LIBS_MARKER_RE.test(code)) return code;
|
|
531
|
+
if (ENSURE_LIBS_MARKER_RE.test(code)) return code;
|
|
532
|
+
const keys = extractUsedCtxLibKeys(code);
|
|
533
|
+
if (!keys.length) return code;
|
|
534
|
+
return `/* __runjs_ensure_libs */
|
|
535
|
+
await ctx.__ensureLibs(${JSON.stringify(keys)});
|
|
536
|
+
${code}`;
|
|
537
|
+
}
|
|
538
|
+
__name(injectEnsureLibsPreamble, "injectEnsureLibsPreamble");
|
|
539
|
+
function isObjectLikeKeyPosition(code, tokenStart, tokenEnd) {
|
|
540
|
+
const next = skipWhitespaceForward(code, tokenEnd);
|
|
541
|
+
if (code[next] !== ":") return false;
|
|
542
|
+
const prev = skipWhitespaceBackward(code, tokenStart - 1);
|
|
543
|
+
const prevCh = prev >= 0 ? code[prev] : "";
|
|
544
|
+
return prevCh === "{" || prevCh === ",";
|
|
545
|
+
}
|
|
546
|
+
__name(isObjectLikeKeyPosition, "isObjectLikeKeyPosition");
|
|
547
|
+
function wrapStringTokenWithReplacements(token, placeholders, placeholderVar, stringifyHelperName) {
|
|
548
|
+
const uniq = Array.from(new Set(placeholders));
|
|
549
|
+
if (!uniq.length) return token;
|
|
550
|
+
let expr = token;
|
|
551
|
+
for (const p of uniq) {
|
|
552
|
+
const v = placeholderVar(p);
|
|
553
|
+
expr = `(${expr}).split(${JSON.stringify(p)}).join(${stringifyHelperName}(${v}, ${JSON.stringify(p)}))`;
|
|
554
|
+
}
|
|
555
|
+
return expr;
|
|
556
|
+
}
|
|
557
|
+
__name(wrapStringTokenWithReplacements, "wrapStringTokenWithReplacements");
|
|
558
|
+
function preprocessRunJsTemplates(code, options = {}) {
|
|
559
|
+
if (!CTX_TEMPLATE_MARKER_RE.test(code)) return code;
|
|
560
|
+
const processBarePlaceholders = options.processBarePlaceholders !== false;
|
|
561
|
+
const processStringLiterals = options.processStringLiterals !== false;
|
|
562
|
+
if (processBarePlaceholders && processStringLiterals && PREPROCESSED_MARKER_RE.test(code)) return code;
|
|
563
|
+
if (processBarePlaceholders && !processStringLiterals && BARE_PLACEHOLDER_VAR_RE.test(code)) return code;
|
|
564
|
+
if (!processBarePlaceholders && processStringLiterals && STRINGIFY_HELPER_RE.test(code)) return code;
|
|
565
|
+
const placeholderVars = /* @__PURE__ */ new Map();
|
|
566
|
+
const preambleLines = [];
|
|
567
|
+
let needsStringifyHelper = false;
|
|
568
|
+
const usedNames = /* @__PURE__ */ new Set();
|
|
569
|
+
const pickUniqueName = /* @__PURE__ */ __name((base) => {
|
|
570
|
+
if (!code.includes(base) && !usedNames.has(base)) {
|
|
571
|
+
usedNames.add(base);
|
|
572
|
+
return base;
|
|
573
|
+
}
|
|
574
|
+
let i2 = 1;
|
|
575
|
+
while (code.includes(`${base}_${i2}`) || usedNames.has(`${base}_${i2}`)) i2 += 1;
|
|
576
|
+
const name = `${base}_${i2}`;
|
|
577
|
+
usedNames.add(name);
|
|
578
|
+
return name;
|
|
579
|
+
}, "pickUniqueName");
|
|
580
|
+
const stringifyHelperName = pickUniqueName(STRINGIFY_HELPER_BASE_NAME);
|
|
581
|
+
let placeholderCounter = 0;
|
|
582
|
+
const nextPlaceholderVarName = /* @__PURE__ */ __name(() => {
|
|
583
|
+
for (; ; ) {
|
|
584
|
+
const name = `__runjs_ctx_tpl_${placeholderCounter}`;
|
|
585
|
+
placeholderCounter += 1;
|
|
586
|
+
if (code.includes(name) || usedNames.has(name)) continue;
|
|
587
|
+
usedNames.add(name);
|
|
588
|
+
return name;
|
|
589
|
+
}
|
|
590
|
+
}, "nextPlaceholderVarName");
|
|
591
|
+
const getPlaceholderVar = /* @__PURE__ */ __name((placeholder) => {
|
|
592
|
+
const existing = placeholderVars.get(placeholder);
|
|
593
|
+
if (existing) return existing;
|
|
594
|
+
const name = nextPlaceholderVarName();
|
|
595
|
+
placeholderVars.set(placeholder, name);
|
|
596
|
+
preambleLines.push(`const ${name} = await ctx.resolveJsonTemplate(${JSON.stringify(placeholder)});`);
|
|
597
|
+
return name;
|
|
598
|
+
}, "getPlaceholderVar");
|
|
599
|
+
let out = "";
|
|
600
|
+
let i = 0;
|
|
601
|
+
let resolveCallPending = false;
|
|
602
|
+
let resolveParenDepth = 0;
|
|
603
|
+
while (i < code.length) {
|
|
604
|
+
const ch = code[i];
|
|
605
|
+
const next = code[i + 1];
|
|
606
|
+
if (ch === "/" && next === "/") {
|
|
607
|
+
const end = readLineComment(code, i);
|
|
608
|
+
out += code.slice(i, end);
|
|
609
|
+
i = end;
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
if (ch === "/" && next === "*") {
|
|
613
|
+
const end = readBlockComment(code, i);
|
|
614
|
+
out += code.slice(i, end);
|
|
615
|
+
i = end;
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
if (ch === "'" || ch === '"') {
|
|
619
|
+
const end = readQuotedString(code, i, ch);
|
|
620
|
+
const token = code.slice(i, end);
|
|
621
|
+
if (processStringLiterals && resolveParenDepth === 0) {
|
|
622
|
+
const placeholders = extractCtxPlaceholders(token);
|
|
623
|
+
if (placeholders.length) {
|
|
624
|
+
needsStringifyHelper = true;
|
|
625
|
+
const expr = wrapStringTokenWithReplacements(token, placeholders, getPlaceholderVar, stringifyHelperName);
|
|
626
|
+
out += isObjectLikeKeyPosition(code, i, end) ? `[${expr}]` : expr;
|
|
627
|
+
} else {
|
|
628
|
+
out += token;
|
|
629
|
+
}
|
|
630
|
+
} else out += token;
|
|
631
|
+
i = end;
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (ch === "`") {
|
|
635
|
+
const end = readTemplateLiteral(code, i);
|
|
636
|
+
const token = code.slice(i, end);
|
|
637
|
+
if (processStringLiterals && resolveParenDepth === 0) {
|
|
638
|
+
const placeholders = extractCtxPlaceholders(token);
|
|
639
|
+
if (placeholders.length) {
|
|
640
|
+
needsStringifyHelper = true;
|
|
641
|
+
const expr = wrapStringTokenWithReplacements(token, placeholders, getPlaceholderVar, stringifyHelperName);
|
|
642
|
+
out += isObjectLikeKeyPosition(code, i, end) ? `[${expr}]` : expr;
|
|
643
|
+
} else {
|
|
644
|
+
out += token;
|
|
645
|
+
}
|
|
646
|
+
} else out += token;
|
|
647
|
+
i = end;
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
if (resolveParenDepth === 0 && !resolveCallPending && readsResolveJsonTemplateCall(code, i)) {
|
|
651
|
+
out += RESOLVE_JSON_TEMPLATE_CALL;
|
|
652
|
+
i += RESOLVE_JSON_TEMPLATE_CALL.length;
|
|
653
|
+
resolveCallPending = true;
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
if (resolveCallPending) {
|
|
657
|
+
if (/\s/.test(ch)) {
|
|
658
|
+
out += ch;
|
|
659
|
+
i += 1;
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
if (ch === "(") {
|
|
663
|
+
out += ch;
|
|
664
|
+
i += 1;
|
|
665
|
+
resolveCallPending = false;
|
|
666
|
+
resolveParenDepth = 1;
|
|
667
|
+
continue;
|
|
668
|
+
}
|
|
669
|
+
resolveCallPending = false;
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
if (resolveParenDepth > 0) {
|
|
673
|
+
if (ch === "(") resolveParenDepth += 1;
|
|
674
|
+
else if (ch === ")") resolveParenDepth -= 1;
|
|
675
|
+
out += ch;
|
|
676
|
+
i += 1;
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
if (processBarePlaceholders) {
|
|
680
|
+
if (ch === "{" && next === "{") {
|
|
681
|
+
const end = code.indexOf("}}", i + 2);
|
|
682
|
+
if (end !== -1) {
|
|
683
|
+
const placeholder = code.slice(i, end + 2);
|
|
684
|
+
if (isCtxTemplatePlaceholder(placeholder)) {
|
|
685
|
+
out += getPlaceholderVar(placeholder);
|
|
686
|
+
i = end + 2;
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
out += ch;
|
|
693
|
+
i += 1;
|
|
694
|
+
}
|
|
695
|
+
if (!preambleLines.length) return out;
|
|
696
|
+
const preamble = [];
|
|
697
|
+
if (needsStringifyHelper) {
|
|
698
|
+
preamble.push(
|
|
699
|
+
`const ${stringifyHelperName} = (value, placeholder) => {`,
|
|
700
|
+
` if (typeof value === 'undefined') return placeholder;`,
|
|
701
|
+
` if (typeof value === 'object' && value !== null) {`,
|
|
702
|
+
` try {`,
|
|
703
|
+
` return JSON.stringify(value);`,
|
|
704
|
+
` } catch (e) {`,
|
|
705
|
+
` return String(value);`,
|
|
706
|
+
` }`,
|
|
707
|
+
` }`,
|
|
708
|
+
` return String(value);`,
|
|
709
|
+
`};`
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
preamble.push(...preambleLines);
|
|
713
|
+
return `${preamble.join("\n")}
|
|
714
|
+
${out}`;
|
|
715
|
+
}
|
|
716
|
+
__name(preprocessRunJsTemplates, "preprocessRunJsTemplates");
|
|
717
|
+
async function prepareRunJsCode(code, options = {}) {
|
|
718
|
+
const src = typeof code === "string" ? code : String(code ?? "");
|
|
719
|
+
const preprocessTemplates = !!options.preprocessTemplates;
|
|
720
|
+
const cache = preprocessTemplates ? PREPARE_RUNJS_CODE_CACHE.withTemplates : PREPARE_RUNJS_CODE_CACHE.withoutTemplates;
|
|
721
|
+
const cached = lruGet(cache, src);
|
|
722
|
+
if (cached) return await cached;
|
|
723
|
+
const task = (async () => {
|
|
724
|
+
if (!preprocessTemplates) return injectEnsureLibsPreamble(await (0, import_jsxTransform.compileRunJs)(src));
|
|
725
|
+
const preBare = preprocessRunJsTemplates(src, { processStringLiterals: false });
|
|
726
|
+
const jsxCompiled = await (0, import_jsxTransform.compileRunJs)(preBare);
|
|
727
|
+
const out = preprocessRunJsTemplates(jsxCompiled, { processBarePlaceholders: false });
|
|
728
|
+
return injectEnsureLibsPreamble(out);
|
|
729
|
+
})();
|
|
730
|
+
lruSet(cache, src, task, PREPARE_RUNJS_CODE_CACHE_LIMIT);
|
|
731
|
+
try {
|
|
732
|
+
return await task;
|
|
733
|
+
} catch (e) {
|
|
734
|
+
cache.delete(src);
|
|
735
|
+
throw e;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
__name(prepareRunJsCode, "prepareRunJsCode");
|
|
739
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
740
|
+
0 && (module.exports = {
|
|
741
|
+
prepareRunJsCode,
|
|
742
|
+
preprocessRunJsTemplates
|
|
743
|
+
});
|