tempo-sdk 0.0.18 → 0.0.20
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 +32 -2
- package/dist/assets/editor.d.ts +79 -0
- package/dist/assets/editor.js +571 -0
- package/dist/assets/editor.js.map +1 -0
- package/dist/assets/index.d.ts +83 -0
- package/dist/assets/index.js +26 -0
- package/dist/assets/index.js.map +1 -0
- package/dist/assets/parser.d.ts +78 -0
- package/dist/assets/parser.js +29 -0
- package/dist/assets/parser.js.map +1 -0
- package/dist/canvas/editor.js +1 -1
- package/dist/canvas/index.d.ts +99 -16
- package/dist/canvas/index.js +1427 -661
- package/dist/canvas/index.js.map +1 -1
- package/dist/canvas/interact-projection.d.ts +8 -0
- package/dist/canvas/interact-projection.js +2848 -0
- package/dist/canvas/interact-projection.js.map +1 -0
- package/dist/canvas/parser.d.ts +25 -1
- package/dist/canvas/parser.js +3 -1
- package/dist/{chunk-6Q4I3RFN.js → chunk-GBKYTBEE.js} +242 -33
- package/dist/chunk-GBKYTBEE.js.map +1 -0
- package/dist/chunk-IWULHYPW.js +650 -0
- package/dist/chunk-IWULHYPW.js.map +1 -0
- package/dist/chunk-QXB32HG4.js +11 -0
- package/dist/chunk-QXB32HG4.js.map +1 -0
- package/dist/index.d.ts +8 -3
- package/dist/index.js +601 -20
- package/dist/index.js.map +1 -1
- package/dist/types-B-dRoF2T.d.ts +102 -0
- package/package.json +19 -2
- package/dist/chunk-6Q4I3RFN.js.map +0 -1
package/README.md
CHANGED
|
@@ -189,8 +189,38 @@ export default defineConfig({
|
|
|
189
189
|
|
|
190
190
|
Same `TEMPO` env-var gating, same `data-tempo-*` output as the Next.js plugin.
|
|
191
191
|
The Vite plugin additionally exposes an `/__tempo/errors` SSE endpoint for
|
|
192
|
-
streaming compile errors to the design surface
|
|
193
|
-
|
|
192
|
+
streaming concrete compile errors to the design surface. It does not publish a
|
|
193
|
+
source-update stream or supervise source-to-visible HMR transactions; normal
|
|
194
|
+
updates belong to Vite and the official React plugin.
|
|
195
|
+
|
|
196
|
+
### Canvas runtime surface
|
|
197
|
+
|
|
198
|
+
`tempo-sdk/canvas` exposes the bundler-agnostic runtime used by the generated
|
|
199
|
+
Canvas host:
|
|
200
|
+
|
|
201
|
+
- `CanvasHost` accepts either legacy `children` or literal `canvasModules`
|
|
202
|
+
loaders. Passing both throws `CanvasHostContractError` instead of silently
|
|
203
|
+
rendering an ambiguous host.
|
|
204
|
+
- `CanvasImportBoundary` owns lazy import, Suspense, canvas-scoped error
|
|
205
|
+
reporting, and current-owner mount success. It does not replay accepted
|
|
206
|
+
modules or inspect Vite transactions.
|
|
207
|
+
- `getCanvasHostRecoveryFeed()` returns the document-local
|
|
208
|
+
`host-recovery-feed`. It carries exactly one product fact
|
|
209
|
+
(`module-load-failure`: a canvas import rejection or an HMR
|
|
210
|
+
module-evaluation failure) to the generated recovery adapter; no hot
|
|
211
|
+
context, cycle number, owner success, or reload command crosses the SDK
|
|
212
|
+
boundary.
|
|
213
|
+
- `canvasHostCapability` declares protocol version 1 with
|
|
214
|
+
`supportsModules: true`. It is defense-in-depth, not the primary
|
|
215
|
+
compatibility gate.
|
|
216
|
+
|
|
217
|
+
Before generating `.tempo/tempo-host.tsx`, `tempo-devserver` runs the primary
|
|
218
|
+
installed-package check. An incompatible SDK fails before browser evaluation
|
|
219
|
+
with `canvas_host_sdk_incompatible` and an actionable install command. Tempo's
|
|
220
|
+
managed sidecar rollout first runs `ensureTempoSdkLatestSpec`; whether that
|
|
221
|
+
upgrade was attempted is included in the typed compatibility error. The rollout
|
|
222
|
+
contract is publish the compatible SDK first, verify the packed artifact, then
|
|
223
|
+
ship the devserver that requires it.
|
|
194
224
|
|
|
195
225
|
---
|
|
196
226
|
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { A as AssetEditFailureCode, S as StaticValue } from '../types-B-dRoF2T.js';
|
|
2
|
+
|
|
3
|
+
type AssetEditFailure = {
|
|
4
|
+
ok: false;
|
|
5
|
+
code: AssetEditFailureCode;
|
|
6
|
+
message: string;
|
|
7
|
+
};
|
|
8
|
+
type AssetEditSuccess = {
|
|
9
|
+
ok: true;
|
|
10
|
+
source: string;
|
|
11
|
+
/** False when the operation was a no-op (already in the desired state). */
|
|
12
|
+
changed: boolean;
|
|
13
|
+
};
|
|
14
|
+
type AssetEditResult = AssetEditSuccess | AssetEditFailure;
|
|
15
|
+
type EnsureNamedDefaultExportResult = (AssetEditSuccess & {
|
|
16
|
+
bindingName: string;
|
|
17
|
+
}) | AssetEditFailure;
|
|
18
|
+
/** Fully-static config for `ensureAssetDeclaration` codegen. Values must not
|
|
19
|
+
* contain UNRESOLVED markers — this is codegen input, not parse output. */
|
|
20
|
+
type AssetInitialConfig = {
|
|
21
|
+
usageInstructions?: string;
|
|
22
|
+
libraries?: string[];
|
|
23
|
+
variants?: Record<string, {
|
|
24
|
+
props: Record<string, StaticValue>;
|
|
25
|
+
}>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Name an anonymous `export default` so a statement-form `defineAsset` can
|
|
29
|
+
* reference it (§4.2). No-op when the default export already is (or
|
|
30
|
+
* references) a named binding. The transform keeps the default export a plain
|
|
31
|
+
* identifier reference, so React Fast Refresh's component-only export
|
|
32
|
+
* analysis still holds:
|
|
33
|
+
*
|
|
34
|
+
* export default <expr> => const <name> = <expr>;
|
|
35
|
+
* export default <name>;
|
|
36
|
+
* export default function () {} => function <name>() {}
|
|
37
|
+
* export default <name>;
|
|
38
|
+
*/
|
|
39
|
+
declare function ensureNamedDefaultExport(options: {
|
|
40
|
+
filePath: string;
|
|
41
|
+
source: string;
|
|
42
|
+
}): EnsureNamedDefaultExportResult;
|
|
43
|
+
/**
|
|
44
|
+
* Add the tempo-sdk/assets import (if absent) and insert a
|
|
45
|
+
* `defineAsset(<bindingName>, <initialConfig>)` statement directly after the
|
|
46
|
+
* top-level statement declaring the binding, separated by one blank line
|
|
47
|
+
* (§4.3). No-op when a clean declaration for the binding already exists.
|
|
48
|
+
*/
|
|
49
|
+
declare function ensureAssetDeclaration(options: {
|
|
50
|
+
filePath: string;
|
|
51
|
+
source: string;
|
|
52
|
+
bindingName: string;
|
|
53
|
+
initialConfig?: AssetInitialConfig;
|
|
54
|
+
}): AssetEditResult;
|
|
55
|
+
/**
|
|
56
|
+
* Insert missing names into the declaration's libraries array, creating the
|
|
57
|
+
* property when absent. Existing entry order is preserved; already-present
|
|
58
|
+
* names are skipped.
|
|
59
|
+
*/
|
|
60
|
+
declare function addLibraries(options: {
|
|
61
|
+
filePath: string;
|
|
62
|
+
source: string;
|
|
63
|
+
bindingName: string;
|
|
64
|
+
libraries: string[];
|
|
65
|
+
}): AssetEditResult;
|
|
66
|
+
/**
|
|
67
|
+
* Remove matching string entries from the declaration's libraries array.
|
|
68
|
+
* Names not present are ignored. When this operation empties the array, the
|
|
69
|
+
* whole `libraries` property is removed — a library-less asset carries no
|
|
70
|
+
* empty-array residue.
|
|
71
|
+
*/
|
|
72
|
+
declare function removeLibraries(options: {
|
|
73
|
+
filePath: string;
|
|
74
|
+
source: string;
|
|
75
|
+
bindingName: string;
|
|
76
|
+
libraries: string[];
|
|
77
|
+
}): AssetEditResult;
|
|
78
|
+
|
|
79
|
+
export { type AssetEditFailure, AssetEditFailureCode, type AssetEditResult, type AssetEditSuccess, type AssetInitialConfig, type EnsureNamedDefaultExportResult, addLibraries, ensureAssetDeclaration, ensureNamedDefaultExport, removeLibraries };
|
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ASSET_SDK_IMPORT_SOURCE,
|
|
3
|
+
collectIdentifierNames,
|
|
4
|
+
collectTopLevelScope,
|
|
5
|
+
hasTopLevelDefineAssetCallFor,
|
|
6
|
+
isUnresolvedValue,
|
|
7
|
+
parseAssetFile,
|
|
8
|
+
parseAssetProgram,
|
|
9
|
+
readAssetImports,
|
|
10
|
+
toSourceSpan,
|
|
11
|
+
unwrapTsExpression
|
|
12
|
+
} from "../chunk-IWULHYPW.js";
|
|
13
|
+
|
|
14
|
+
// assets/editor.ts
|
|
15
|
+
function ensureNamedDefaultExport(options) {
|
|
16
|
+
const { program, errors } = parseAssetProgram(options.filePath, options.source);
|
|
17
|
+
if (!program) {
|
|
18
|
+
return failure(
|
|
19
|
+
"not_definable",
|
|
20
|
+
errors[0]?.message ?? "asset source has syntax errors"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const statement = (program.body ?? []).find(
|
|
24
|
+
(node) => node?.type === "ExportDefaultDeclaration"
|
|
25
|
+
);
|
|
26
|
+
if (!statement) {
|
|
27
|
+
return failure("export_not_found", `no default export in ${options.filePath}`);
|
|
28
|
+
}
|
|
29
|
+
const declaration = statement.declaration;
|
|
30
|
+
const isDeclarationForm = declaration?.type === "FunctionDeclaration" || declaration?.type === "ClassDeclaration";
|
|
31
|
+
if (isDeclarationForm && typeof declaration.id?.name === "string") {
|
|
32
|
+
return {
|
|
33
|
+
ok: true,
|
|
34
|
+
source: options.source,
|
|
35
|
+
changed: false,
|
|
36
|
+
bindingName: declaration.id.name
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (!isDeclarationForm) {
|
|
40
|
+
const expression = unwrapTsExpression(declaration);
|
|
41
|
+
if (expression?.type === "Identifier") {
|
|
42
|
+
return {
|
|
43
|
+
ok: true,
|
|
44
|
+
source: options.source,
|
|
45
|
+
changed: false,
|
|
46
|
+
bindingName: expression.name
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const statementSpan = toSourceSpan(statement);
|
|
51
|
+
const declarationSpan = toSourceSpan(declaration);
|
|
52
|
+
if (!statementSpan || !declarationSpan) {
|
|
53
|
+
return failure("not_definable", "default export has no source span");
|
|
54
|
+
}
|
|
55
|
+
const bindingName = pickAvailableName(
|
|
56
|
+
deriveDefaultExportName(options.filePath),
|
|
57
|
+
collectIdentifierNames(program)
|
|
58
|
+
);
|
|
59
|
+
if (bindingName === void 0) {
|
|
60
|
+
return failure(
|
|
61
|
+
"not_definable",
|
|
62
|
+
"could not derive a free identifier for the default export"
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
const newline = detectNewline(options.source);
|
|
66
|
+
let edits;
|
|
67
|
+
if (isDeclarationForm) {
|
|
68
|
+
const head = options.source.slice(
|
|
69
|
+
declarationSpan.start,
|
|
70
|
+
declarationSpan.start + 64
|
|
71
|
+
);
|
|
72
|
+
const keyword = declaration.type === "FunctionDeclaration" ? head.match(/^(?:async\s+)?function(?:\s*\*)?/) : head.match(/^class\b/);
|
|
73
|
+
if (!keyword) {
|
|
74
|
+
return failure("not_definable", "unsupported default export shape");
|
|
75
|
+
}
|
|
76
|
+
const nameOffset = declarationSpan.start + keyword[0].length;
|
|
77
|
+
let nameEnd = nameOffset;
|
|
78
|
+
while (nameEnd < options.source.length && /\s/.test(options.source[nameEnd] ?? "")) {
|
|
79
|
+
nameEnd++;
|
|
80
|
+
}
|
|
81
|
+
const nameValue = options.source[nameEnd] === "(" ? ` ${bindingName}` : ` ${bindingName} `;
|
|
82
|
+
edits = [
|
|
83
|
+
{ start: statementSpan.start, end: declarationSpan.start, value: "" },
|
|
84
|
+
{ start: nameOffset, end: nameEnd, value: nameValue },
|
|
85
|
+
{
|
|
86
|
+
start: statementSpan.end,
|
|
87
|
+
end: statementSpan.end,
|
|
88
|
+
value: `${newline}export default ${bindingName};`
|
|
89
|
+
}
|
|
90
|
+
];
|
|
91
|
+
} else {
|
|
92
|
+
edits = [
|
|
93
|
+
{
|
|
94
|
+
start: statementSpan.start,
|
|
95
|
+
end: declarationSpan.start,
|
|
96
|
+
value: `const ${bindingName} = `
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
start: declarationSpan.end,
|
|
100
|
+
end: statementSpan.end,
|
|
101
|
+
value: `;${newline}export default ${bindingName};`
|
|
102
|
+
}
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
return { ...applyEdits(options.source, edits), bindingName };
|
|
106
|
+
}
|
|
107
|
+
function ensureAssetDeclaration(options) {
|
|
108
|
+
const { program, errors } = parseAssetProgram(options.filePath, options.source);
|
|
109
|
+
if (!program) {
|
|
110
|
+
return failure(
|
|
111
|
+
"not_definable",
|
|
112
|
+
errors[0]?.message ?? "asset source has syntax errors"
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const parsed = parseAssetFile(options.filePath, options.source);
|
|
116
|
+
if (parsed.declarations.some(
|
|
117
|
+
(declaration) => declaration.bindingName === options.bindingName
|
|
118
|
+
)) {
|
|
119
|
+
return { ok: true, source: options.source, changed: false };
|
|
120
|
+
}
|
|
121
|
+
const imports = readAssetImports(program);
|
|
122
|
+
if (hasTopLevelDefineAssetCallFor(
|
|
123
|
+
program,
|
|
124
|
+
imports.trustedDefineAssetNames,
|
|
125
|
+
options.bindingName
|
|
126
|
+
)) {
|
|
127
|
+
return failure(
|
|
128
|
+
"config_not_editable",
|
|
129
|
+
`an existing defineAsset declaration for "${options.bindingName}" has structural errors \u2014 fix it instead of adding another`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
const binding = collectTopLevelScope(program).bindings.get(
|
|
133
|
+
options.bindingName
|
|
134
|
+
);
|
|
135
|
+
if (!binding) {
|
|
136
|
+
const importSource = imports.importedBindings.get(options.bindingName);
|
|
137
|
+
return failure(
|
|
138
|
+
"not_definable",
|
|
139
|
+
importSource !== void 0 ? `"${options.bindingName}" is imported from "${importSource}" \u2014 defineAsset must live in the file that defines the component` : `no top-level binding "${options.bindingName}" in ${options.filePath}`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
const style = detectCodeStyle(options.source);
|
|
143
|
+
const edits = [];
|
|
144
|
+
let callName;
|
|
145
|
+
if (imports.trustedDefineAssetNames.size > 0) {
|
|
146
|
+
callName = imports.trustedDefineAssetNames.values().next().value;
|
|
147
|
+
} else {
|
|
148
|
+
const identifiers = collectIdentifierNames(program);
|
|
149
|
+
if (identifiers.has("defineAsset")) {
|
|
150
|
+
const picked = pickAvailableName("defineAsset", identifiers);
|
|
151
|
+
if (picked === void 0) {
|
|
152
|
+
return failure(
|
|
153
|
+
"not_definable",
|
|
154
|
+
"could not derive a free identifier for the defineAsset import"
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
callName = picked;
|
|
158
|
+
} else {
|
|
159
|
+
callName = "defineAsset";
|
|
160
|
+
}
|
|
161
|
+
const specifier = callName === "defineAsset" ? "defineAsset" : `defineAsset as ${callName}`;
|
|
162
|
+
const importLine = `import { ${specifier} } from ${quoteString(
|
|
163
|
+
ASSET_SDK_IMPORT_SOURCE,
|
|
164
|
+
style.quote
|
|
165
|
+
)};`;
|
|
166
|
+
edits.push(
|
|
167
|
+
buildImportInsertionEdit(
|
|
168
|
+
options.source,
|
|
169
|
+
imports.importDeclarationSpans,
|
|
170
|
+
importLine,
|
|
171
|
+
style.newline
|
|
172
|
+
)
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
const callText = serializeDefineAssetCall(
|
|
176
|
+
callName,
|
|
177
|
+
options.bindingName,
|
|
178
|
+
options.initialConfig ?? {},
|
|
179
|
+
style
|
|
180
|
+
);
|
|
181
|
+
const insertionOffset = statementInsertionOffset(
|
|
182
|
+
options.source,
|
|
183
|
+
binding.statementSpan.end
|
|
184
|
+
);
|
|
185
|
+
edits.push({
|
|
186
|
+
start: insertionOffset,
|
|
187
|
+
end: insertionOffset,
|
|
188
|
+
value: `${style.newline}${style.newline}${callText}`
|
|
189
|
+
});
|
|
190
|
+
return applyEdits(options.source, edits);
|
|
191
|
+
}
|
|
192
|
+
function addLibraries(options) {
|
|
193
|
+
const located = locateDeclaration(options);
|
|
194
|
+
if (!located.ok) return located;
|
|
195
|
+
const { declaration } = located;
|
|
196
|
+
const names = [];
|
|
197
|
+
for (const name of options.libraries) {
|
|
198
|
+
if (name === "") continue;
|
|
199
|
+
if (declaration.libraries.includes(name)) continue;
|
|
200
|
+
if (names.includes(name)) continue;
|
|
201
|
+
names.push(name);
|
|
202
|
+
}
|
|
203
|
+
if (names.length === 0) {
|
|
204
|
+
return { ok: true, source: options.source, changed: false };
|
|
205
|
+
}
|
|
206
|
+
const style = detectCodeStyle(options.source);
|
|
207
|
+
const serialized = names.map((name) => quoteString(name, style.quote)).join(", ");
|
|
208
|
+
const spans = declaration.spans;
|
|
209
|
+
if (spans.libraries) {
|
|
210
|
+
const lastItem = spans.libraryItems[spans.libraryItems.length - 1];
|
|
211
|
+
if (lastItem) {
|
|
212
|
+
return applyEdits(options.source, [
|
|
213
|
+
{ start: lastItem.end, end: lastItem.end, value: `, ${serialized}` }
|
|
214
|
+
]);
|
|
215
|
+
}
|
|
216
|
+
const inner = options.source.slice(
|
|
217
|
+
spans.libraries.start + 1,
|
|
218
|
+
spans.libraries.end - 1
|
|
219
|
+
);
|
|
220
|
+
const offset = spans.libraries.start + 1;
|
|
221
|
+
const value = inner.trim() === "" ? serialized : `${serialized}, `;
|
|
222
|
+
return applyEdits(options.source, [{ start: offset, end: offset, value }]);
|
|
223
|
+
}
|
|
224
|
+
if (spans.librariesProperty) {
|
|
225
|
+
return failure(
|
|
226
|
+
"config_not_editable",
|
|
227
|
+
`the libraries property of "${options.bindingName}" is not an editable array literal`
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return applyEdits(options.source, [
|
|
231
|
+
insertConfigProperty(
|
|
232
|
+
options.source,
|
|
233
|
+
spans.config,
|
|
234
|
+
`libraries: [${serialized}]`,
|
|
235
|
+
style
|
|
236
|
+
)
|
|
237
|
+
]);
|
|
238
|
+
}
|
|
239
|
+
function removeLibraries(options) {
|
|
240
|
+
const targets = new Set(options.libraries.filter((name) => name !== ""));
|
|
241
|
+
let located = locateDeclaration(options);
|
|
242
|
+
if (!located.ok) return located;
|
|
243
|
+
if (targets.size > 0 && located.declaration.spans.librariesProperty && !located.declaration.spans.libraries) {
|
|
244
|
+
return failure(
|
|
245
|
+
"config_not_editable",
|
|
246
|
+
`the libraries property of "${options.bindingName}" is not an editable array literal`
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
let source = options.source;
|
|
250
|
+
let changed = false;
|
|
251
|
+
for (let iteration = 0; iteration < 32; iteration++) {
|
|
252
|
+
const declaration = located.declaration;
|
|
253
|
+
const removals = [];
|
|
254
|
+
declaration.libraries.forEach((name, index) => {
|
|
255
|
+
const span = declaration.spans.libraryItems[index];
|
|
256
|
+
if (targets.has(name) && span) {
|
|
257
|
+
removals.push(expandListItemRemoval(source, span));
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
if (removals.length === 0) break;
|
|
261
|
+
source = spliceRemovals(source, removals);
|
|
262
|
+
changed = true;
|
|
263
|
+
const relocated = locateDeclaration({ ...options, source });
|
|
264
|
+
if (!relocated.ok) return relocated;
|
|
265
|
+
located = relocated;
|
|
266
|
+
}
|
|
267
|
+
if (changed) {
|
|
268
|
+
const spans = located.declaration.spans;
|
|
269
|
+
if (spans.libraries && spans.librariesProperty && /^\s*$/.test(
|
|
270
|
+
source.slice(spans.libraries.start + 1, spans.libraries.end - 1)
|
|
271
|
+
)) {
|
|
272
|
+
source = spliceRemovals(source, [
|
|
273
|
+
expandListItemRemoval(source, spans.librariesProperty)
|
|
274
|
+
]);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return { ok: true, source, changed };
|
|
278
|
+
}
|
|
279
|
+
function locateDeclaration(options) {
|
|
280
|
+
const { program, errors } = parseAssetProgram(options.filePath, options.source);
|
|
281
|
+
if (!program) {
|
|
282
|
+
return failure(
|
|
283
|
+
"config_not_editable",
|
|
284
|
+
errors[0]?.message ?? "asset source has syntax errors"
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
const parsed = parseAssetFile(options.filePath, options.source);
|
|
288
|
+
const declaration = parsed.declarations.find(
|
|
289
|
+
(candidate) => candidate.bindingName === options.bindingName
|
|
290
|
+
);
|
|
291
|
+
if (declaration) return { ok: true, declaration };
|
|
292
|
+
const imports = readAssetImports(program);
|
|
293
|
+
if (hasTopLevelDefineAssetCallFor(
|
|
294
|
+
program,
|
|
295
|
+
imports.trustedDefineAssetNames,
|
|
296
|
+
options.bindingName
|
|
297
|
+
)) {
|
|
298
|
+
return failure(
|
|
299
|
+
"config_not_editable",
|
|
300
|
+
`the defineAsset declaration for "${options.bindingName}" has structural errors and cannot be edited`
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return failure(
|
|
304
|
+
"asset_not_found",
|
|
305
|
+
`no defineAsset declaration for "${options.bindingName}" in ${options.filePath}`
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
function failure(code, message) {
|
|
309
|
+
return { ok: false, code, message };
|
|
310
|
+
}
|
|
311
|
+
function applyEdits(source, edits) {
|
|
312
|
+
const sorted = [...edits].sort((left, right) => right.start - left.start);
|
|
313
|
+
let previousStart = source.length + 1;
|
|
314
|
+
let next = source;
|
|
315
|
+
for (const edit of sorted) {
|
|
316
|
+
if (edit.start < 0 || edit.end < edit.start || edit.end > previousStart) {
|
|
317
|
+
throw new Error(
|
|
318
|
+
`Invalid overlapping asset source edit ${edit.start}:${edit.end}`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
next = next.slice(0, edit.start) + edit.value + next.slice(edit.end);
|
|
322
|
+
previousStart = edit.start;
|
|
323
|
+
}
|
|
324
|
+
return { ok: true, source: next, changed: next !== source };
|
|
325
|
+
}
|
|
326
|
+
function spliceRemovals(source, spans) {
|
|
327
|
+
const sorted = [...spans].sort((left, right) => left.start - right.start);
|
|
328
|
+
const merged = [];
|
|
329
|
+
for (const span of sorted) {
|
|
330
|
+
const last = merged[merged.length - 1];
|
|
331
|
+
if (last && span.start <= last.end) {
|
|
332
|
+
last.end = Math.max(last.end, span.end);
|
|
333
|
+
} else {
|
|
334
|
+
merged.push({ ...span });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
let result = source;
|
|
338
|
+
for (let index = merged.length - 1; index >= 0; index--) {
|
|
339
|
+
const span = merged[index];
|
|
340
|
+
result = result.slice(0, span.start) + result.slice(span.end);
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
function expandListItemRemoval(source, span) {
|
|
345
|
+
let { start, end } = span;
|
|
346
|
+
let forward = end;
|
|
347
|
+
while (forward < source.length && /\s/.test(source[forward] ?? "")) forward++;
|
|
348
|
+
if (source[forward] === ",") {
|
|
349
|
+
end = forward + 1;
|
|
350
|
+
while (end < source.length && (source[end] === " " || source[end] === " ")) {
|
|
351
|
+
end++;
|
|
352
|
+
}
|
|
353
|
+
return expandBlankLine(source, { start, end });
|
|
354
|
+
}
|
|
355
|
+
let backward = start;
|
|
356
|
+
while (backward > 0 && /\s/.test(source[backward - 1] ?? "")) backward--;
|
|
357
|
+
if (source[backward - 1] === ",") {
|
|
358
|
+
return { start: backward - 1, end };
|
|
359
|
+
}
|
|
360
|
+
while (start > 0 && /\s/.test(source[start - 1] ?? "")) start--;
|
|
361
|
+
while (end < source.length && /\s/.test(source[end] ?? "")) end++;
|
|
362
|
+
return { start, end };
|
|
363
|
+
}
|
|
364
|
+
function expandBlankLine(source, span) {
|
|
365
|
+
const lineStart = source.lastIndexOf("\n", span.start - 1) + 1;
|
|
366
|
+
if (!/^[ \t]*$/.test(source.slice(lineStart, span.start))) return span;
|
|
367
|
+
if (source.startsWith("\r\n", span.end)) {
|
|
368
|
+
return { start: lineStart, end: span.end + 2 };
|
|
369
|
+
}
|
|
370
|
+
if (source[span.end] === "\n") {
|
|
371
|
+
return { start: lineStart, end: span.end + 1 };
|
|
372
|
+
}
|
|
373
|
+
return span;
|
|
374
|
+
}
|
|
375
|
+
function insertConfigProperty(source, configSpan, propertyText, style) {
|
|
376
|
+
const closeOffset = configSpan.end - 1;
|
|
377
|
+
let offset = closeOffset;
|
|
378
|
+
while (offset > configSpan.start + 1 && /\s/.test(source[offset - 1] ?? "")) {
|
|
379
|
+
offset--;
|
|
380
|
+
}
|
|
381
|
+
const previous = source[offset - 1] ?? "";
|
|
382
|
+
const multiline = source.slice(configSpan.start, configSpan.end).includes("\n");
|
|
383
|
+
if (!multiline) {
|
|
384
|
+
if (previous === "{") {
|
|
385
|
+
return { start: offset, end: closeOffset, value: ` ${propertyText} ` };
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
start: offset,
|
|
389
|
+
end: offset,
|
|
390
|
+
value: previous === "," ? ` ${propertyText}` : `, ${propertyText}`
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
if (previous === "{") {
|
|
394
|
+
const baseIndent = lineIndentAt(source, configSpan.start);
|
|
395
|
+
return {
|
|
396
|
+
start: offset,
|
|
397
|
+
end: offset,
|
|
398
|
+
value: `${style.newline}${baseIndent}${style.indent}${propertyText},`
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
const propertyIndent = lineIndentAt(source, offset);
|
|
402
|
+
return {
|
|
403
|
+
start: offset,
|
|
404
|
+
end: offset,
|
|
405
|
+
value: `${previous === "," ? "" : ","}${style.newline}${propertyIndent}${propertyText},`
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
function buildImportInsertionEdit(source, importSpans, importLine, newline) {
|
|
409
|
+
const last = importSpans[importSpans.length - 1];
|
|
410
|
+
if (last) {
|
|
411
|
+
return { start: last.end, end: last.end, value: `${newline}${importLine}` };
|
|
412
|
+
}
|
|
413
|
+
const offset = topLevelInsertionOffset(source);
|
|
414
|
+
const nextChar = source[offset] ?? "";
|
|
415
|
+
const prefix = offset > 0 ? newline : "";
|
|
416
|
+
const suffix = nextChar === "\n" || nextChar === "\r" || nextChar === "" ? newline : `${newline}${newline}`;
|
|
417
|
+
return {
|
|
418
|
+
start: offset,
|
|
419
|
+
end: offset,
|
|
420
|
+
value: `${prefix}${importLine}${suffix}`
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function topLevelInsertionOffset(source) {
|
|
424
|
+
let offset = 0;
|
|
425
|
+
if (source.startsWith("#!")) {
|
|
426
|
+
const newline = source.indexOf("\n");
|
|
427
|
+
offset = newline >= 0 ? newline + 1 : source.length;
|
|
428
|
+
}
|
|
429
|
+
while (true) {
|
|
430
|
+
const nextLineEnd = source.indexOf("\n", offset);
|
|
431
|
+
const end = nextLineEnd >= 0 ? nextLineEnd + 1 : source.length;
|
|
432
|
+
const line = source.slice(offset, end).trim();
|
|
433
|
+
if (!/^["']use [^"']+["'];?$/.test(line)) break;
|
|
434
|
+
offset = end;
|
|
435
|
+
}
|
|
436
|
+
return offset;
|
|
437
|
+
}
|
|
438
|
+
function statementInsertionOffset(source, statementEnd) {
|
|
439
|
+
let offset = statementEnd;
|
|
440
|
+
while (offset < source.length && (source[offset] === " " || source[offset] === " ")) {
|
|
441
|
+
offset++;
|
|
442
|
+
}
|
|
443
|
+
if (source[offset] === "/" && source[offset + 1] === "/") {
|
|
444
|
+
while (offset < source.length && source[offset] !== "\n" && source[offset] !== "\r") {
|
|
445
|
+
offset++;
|
|
446
|
+
}
|
|
447
|
+
return offset;
|
|
448
|
+
}
|
|
449
|
+
return statementEnd;
|
|
450
|
+
}
|
|
451
|
+
function serializeDefineAssetCall(callName, bindingName, config, style) {
|
|
452
|
+
const entries = [];
|
|
453
|
+
if (config.usageInstructions !== void 0) {
|
|
454
|
+
entries.push(["usageInstructions", config.usageInstructions]);
|
|
455
|
+
}
|
|
456
|
+
if (config.libraries !== void 0) {
|
|
457
|
+
entries.push(["libraries", config.libraries]);
|
|
458
|
+
}
|
|
459
|
+
if (config.variants !== void 0) {
|
|
460
|
+
entries.push(["variants", config.variants]);
|
|
461
|
+
}
|
|
462
|
+
if (entries.length === 0) return `${callName}(${bindingName}, {});`;
|
|
463
|
+
const body = entries.map(
|
|
464
|
+
([key, value]) => `${style.indent}${key}: ${serializeStaticValue(value, style, 1)},`
|
|
465
|
+
).join(style.newline);
|
|
466
|
+
return `${callName}(${bindingName}, {${style.newline}${body}${style.newline}});`;
|
|
467
|
+
}
|
|
468
|
+
function serializeStaticValue(value, style, depth) {
|
|
469
|
+
if (isUnresolvedValue(value)) {
|
|
470
|
+
throw new Error(
|
|
471
|
+
"ensureAssetDeclaration initialConfig values must be fully static"
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
if (value === null) return "null";
|
|
475
|
+
if (typeof value === "string") return quoteString(value, style.quote);
|
|
476
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
477
|
+
return String(value);
|
|
478
|
+
}
|
|
479
|
+
if (Array.isArray(value)) {
|
|
480
|
+
if (value.length === 0) return "[]";
|
|
481
|
+
if (value.every(isPrimitiveStaticValue)) {
|
|
482
|
+
return `[${value.map((item) => serializeStaticValue(item, style, depth)).join(", ")}]`;
|
|
483
|
+
}
|
|
484
|
+
const inner2 = value.map(
|
|
485
|
+
(item) => `${style.indent.repeat(depth + 1)}${serializeStaticValue(item, style, depth + 1)},`
|
|
486
|
+
).join(style.newline);
|
|
487
|
+
return `[${style.newline}${inner2}${style.newline}${style.indent.repeat(depth)}]`;
|
|
488
|
+
}
|
|
489
|
+
const entries = Object.entries(value);
|
|
490
|
+
if (entries.length === 0) return "{}";
|
|
491
|
+
if (entries.every(([, item]) => isPrimitiveStaticValue(item))) {
|
|
492
|
+
return `{ ${entries.map(
|
|
493
|
+
([key, item]) => `${serializeObjectKey(key, style)}: ${serializeStaticValue(item, style, depth)}`
|
|
494
|
+
).join(", ")} }`;
|
|
495
|
+
}
|
|
496
|
+
const inner = entries.map(
|
|
497
|
+
([key, item]) => `${style.indent.repeat(depth + 1)}${serializeObjectKey(key, style)}: ${serializeStaticValue(item, style, depth + 1)},`
|
|
498
|
+
).join(style.newline);
|
|
499
|
+
return `{${style.newline}${inner}${style.newline}${style.indent.repeat(depth)}}`;
|
|
500
|
+
}
|
|
501
|
+
function isPrimitiveStaticValue(value) {
|
|
502
|
+
return value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
503
|
+
}
|
|
504
|
+
function serializeObjectKey(key, style) {
|
|
505
|
+
return /^[A-Za-z_$][\w$]*$/.test(key) ? key : quoteString(key, style.quote);
|
|
506
|
+
}
|
|
507
|
+
function quoteString(value, quote) {
|
|
508
|
+
const json = JSON.stringify(value);
|
|
509
|
+
if (quote === '"') return json;
|
|
510
|
+
const inner = json.slice(1, -1).replace(/\\"/g, '"').replace(/'/g, "\\'");
|
|
511
|
+
return `'${inner}'`;
|
|
512
|
+
}
|
|
513
|
+
function detectCodeStyle(source) {
|
|
514
|
+
const doubles = (source.match(/"/g) ?? []).length;
|
|
515
|
+
const singles = (source.match(/'/g) ?? []).length;
|
|
516
|
+
let sawTab = false;
|
|
517
|
+
let minSpaces = Number.POSITIVE_INFINITY;
|
|
518
|
+
for (const line of source.split("\n")) {
|
|
519
|
+
const match = line.match(/^([ \t]+)\S/);
|
|
520
|
+
if (!match) continue;
|
|
521
|
+
if (match[1].includes(" ")) {
|
|
522
|
+
sawTab = true;
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
minSpaces = Math.min(minSpaces, match[1].length);
|
|
526
|
+
}
|
|
527
|
+
return {
|
|
528
|
+
quote: singles > doubles ? "'" : '"',
|
|
529
|
+
indent: sawTab ? " " : Number.isFinite(minSpaces) ? " ".repeat(minSpaces) : " ",
|
|
530
|
+
newline: detectNewline(source)
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
function detectNewline(source) {
|
|
534
|
+
return source.includes("\r\n") ? "\r\n" : "\n";
|
|
535
|
+
}
|
|
536
|
+
function lineIndentAt(source, offset) {
|
|
537
|
+
const lineStart = Math.max(source.lastIndexOf("\n", offset - 1) + 1, 0);
|
|
538
|
+
const linePrefix = source.slice(lineStart, offset);
|
|
539
|
+
return linePrefix.match(/^[ \t]*/)?.[0] ?? "";
|
|
540
|
+
}
|
|
541
|
+
function pickAvailableName(base, taken) {
|
|
542
|
+
if (!taken.has(base)) return base;
|
|
543
|
+
for (let index = 2; index < 1e4; index++) {
|
|
544
|
+
const candidate = `${base}${index}`;
|
|
545
|
+
if (!taken.has(candidate)) return candidate;
|
|
546
|
+
}
|
|
547
|
+
return void 0;
|
|
548
|
+
}
|
|
549
|
+
function deriveDefaultExportName(filePath) {
|
|
550
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
551
|
+
const segments = normalized.split("/").filter(Boolean);
|
|
552
|
+
const fileName = segments[segments.length - 1] ?? "";
|
|
553
|
+
if (/^index\.canvas\.[tj]sx?$/.test(fileName)) {
|
|
554
|
+
const parent = segments[segments.length - 2] ?? "";
|
|
555
|
+
const base = pascalCase(parent);
|
|
556
|
+
return base === "" ? "Canvas" : `${base}Canvas`;
|
|
557
|
+
}
|
|
558
|
+
const name = pascalCase(fileName.replace(/\.[tj]sx?$/, ""));
|
|
559
|
+
if (name === "") return "DefaultExport";
|
|
560
|
+
return /^[0-9]/.test(name) ? `Component${name}` : name;
|
|
561
|
+
}
|
|
562
|
+
function pascalCase(value) {
|
|
563
|
+
return value.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
564
|
+
}
|
|
565
|
+
export {
|
|
566
|
+
addLibraries,
|
|
567
|
+
ensureAssetDeclaration,
|
|
568
|
+
ensureNamedDefaultExport,
|
|
569
|
+
removeLibraries
|
|
570
|
+
};
|
|
571
|
+
//# sourceMappingURL=editor.js.map
|