@pstdio/canvas 0.46.0 → 0.47.0-nightly.20261101230131
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/package.json +2 -3
- package/src/project/bundle.ts +11 -1
- package/src/project/cli.ts +1 -1
- package/src/project/watch.ts +13 -4
- package/src/indexer.ts +0 -65
- package/src/native-preview-runtime.tsx +0 -41
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pstdio/canvas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.47.0-nightly.20261101230131",
|
|
4
4
|
"description": "Build and watch a project's .canvas.tsx files for UI Canvas.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,8 +16,7 @@
|
|
|
16
16
|
"src"
|
|
17
17
|
],
|
|
18
18
|
"exports": {
|
|
19
|
-
".": "./src/index.ts"
|
|
20
|
-
"./indexer": "./src/indexer.ts"
|
|
19
|
+
".": "./src/index.ts"
|
|
21
20
|
},
|
|
22
21
|
"dependencies": {
|
|
23
22
|
"typescript": "6.0.2"
|
package/src/project/bundle.ts
CHANGED
|
@@ -13,6 +13,16 @@ const projectReact = (base: string): Bun.BunPlugin => ({
|
|
|
13
13
|
},
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
+
// Project code and the frame runtime must share this package's module state, such as the loader
|
|
17
|
+
// that nested frames use, so `@pstdio/canvas` always means the package running the build.
|
|
18
|
+
const PACKAGE_INDEX = resolve(import.meta.dir, "../index.ts");
|
|
19
|
+
const ownPackage: Bun.BunPlugin = {
|
|
20
|
+
name: "pstdio-canvas-own-package",
|
|
21
|
+
setup(build) {
|
|
22
|
+
build.onResolve({ filter: /^@pstdio\/canvas$/ }, () => ({ path: PACKAGE_INDEX }));
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
16
26
|
// Vite's `?raw` (file text) and `?url` (file address) imports are common in React projects.
|
|
17
27
|
// Frames load from blob URLs, so a `?url` import becomes a data URL.
|
|
18
28
|
const queryImports: Bun.BunPlugin = {
|
|
@@ -97,7 +107,7 @@ const bunBuild = (root: string, entry: string, canvases: CanvasEntry[], outdir?:
|
|
|
97
107
|
define: { "process.env.NODE_ENV": '"production"' },
|
|
98
108
|
naming: { entry: "[name]-[hash].[ext]", chunk: "chunk-[hash].[ext]", asset: "[name]-[hash].[ext]" },
|
|
99
109
|
loader,
|
|
100
|
-
plugins: [projectReact(reactBase(root, canvases)), queryImports],
|
|
110
|
+
plugins: [projectReact(reactBase(root, canvases)), ownPackage, queryImports],
|
|
101
111
|
});
|
|
102
112
|
|
|
103
113
|
type BuildLogs = Bun.BuildOutput["logs"];
|
package/src/project/cli.ts
CHANGED
package/src/project/watch.ts
CHANGED
|
@@ -41,6 +41,7 @@ export const watchCanvases = (options: CanvasWatchOptions) => {
|
|
|
41
41
|
let failedState: string | undefined;
|
|
42
42
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
43
43
|
let child: ReturnType<typeof Bun.spawn> | undefined;
|
|
44
|
+
let checking: Promise<void>;
|
|
44
45
|
const build = async () => {
|
|
45
46
|
child = Bun.spawn([process.execPath, CLI, "build", "--root", root, "--out", out], {
|
|
46
47
|
stdout: "inherit",
|
|
@@ -53,19 +54,27 @@ export const watchCanvases = (options: CanvasWatchOptions) => {
|
|
|
53
54
|
};
|
|
54
55
|
const check = async () => {
|
|
55
56
|
const manifest = await readManifest(out);
|
|
56
|
-
if (
|
|
57
|
+
if (stopped) return;
|
|
58
|
+
const current = manifest && (await isCurrent(root, manifest));
|
|
59
|
+
if (stopped) return;
|
|
60
|
+
if (!current) {
|
|
57
61
|
const state = await inputState(root, manifest);
|
|
58
|
-
if (state !== failedState) failedState = (await build()) === 0 ? undefined : state;
|
|
62
|
+
if (!stopped && state !== failedState) failedState = (await build()) === 0 ? undefined : state;
|
|
59
63
|
}
|
|
60
|
-
if (!stopped)
|
|
64
|
+
if (!stopped)
|
|
65
|
+
timer = setTimeout(() => {
|
|
66
|
+
checking = check();
|
|
67
|
+
}, options.intervalMs ?? 500);
|
|
61
68
|
};
|
|
62
69
|
const started = check();
|
|
70
|
+
checking = started;
|
|
63
71
|
return {
|
|
64
72
|
started,
|
|
65
|
-
close: () => {
|
|
73
|
+
close: async () => {
|
|
66
74
|
stopped = true;
|
|
67
75
|
clearTimeout(timer);
|
|
68
76
|
child?.kill();
|
|
77
|
+
await checking;
|
|
69
78
|
},
|
|
70
79
|
};
|
|
71
80
|
};
|
package/src/indexer.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
const bindings = (file: ts.SourceFile) => {
|
|
4
|
-
const declarations = new Map<string, ts.Expression>();
|
|
5
|
-
const imports = new Map<string, string>();
|
|
6
|
-
for (const statement of file.statements) {
|
|
7
|
-
if (ts.isVariableStatement(statement))
|
|
8
|
-
for (const declaration of statement.declarationList.declarations)
|
|
9
|
-
if (ts.isIdentifier(declaration.name) && declaration.initializer)
|
|
10
|
-
declarations.set(declaration.name.text, declaration.initializer);
|
|
11
|
-
if (
|
|
12
|
-
ts.isImportDeclaration(statement) &&
|
|
13
|
-
statement.importClause?.namedBindings &&
|
|
14
|
-
ts.isNamedImports(statement.importClause.namedBindings)
|
|
15
|
-
)
|
|
16
|
-
for (const binding of statement.importClause.namedBindings.elements)
|
|
17
|
-
imports.set(binding.name.text, (statement.moduleSpecifier as ts.StringLiteral).text);
|
|
18
|
-
}
|
|
19
|
-
return { declarations, imports };
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const indexExamples = (path: string, source: string) => {
|
|
23
|
-
const file = ts.createSourceFile(path, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
24
|
-
const { declarations, imports } = bindings(file);
|
|
25
|
-
const object = (node?: ts.Expression): ts.ObjectLiteralExpression | undefined => {
|
|
26
|
-
if (!node) return;
|
|
27
|
-
if (ts.isObjectLiteralExpression(node)) return node;
|
|
28
|
-
if (ts.isSatisfiesExpression(node) || ts.isAsExpression(node) || ts.isParenthesizedExpression(node))
|
|
29
|
-
return object(node.expression);
|
|
30
|
-
if (ts.isIdentifier(node)) return object(declarations.get(node.text));
|
|
31
|
-
};
|
|
32
|
-
const property = (node: ts.ObjectLiteralExpression | undefined, key: string) => {
|
|
33
|
-
const value = node?.properties.find(
|
|
34
|
-
(property) => ts.isPropertyAssignment(property) && property.name.getText(file) === key,
|
|
35
|
-
);
|
|
36
|
-
return value && ts.isPropertyAssignment(value) ? value.initializer : undefined;
|
|
37
|
-
};
|
|
38
|
-
const text = (node?: ts.Expression) => (node && ts.isStringLiteral(node) ? node.text : undefined);
|
|
39
|
-
const defaults = file.statements.find(ts.isExportAssignment);
|
|
40
|
-
const meta = object(defaults?.expression);
|
|
41
|
-
const component = property(meta, "component")?.getText(file);
|
|
42
|
-
const stories: { exportName: string; id: string; name: string; defaultTheme?: string }[] = [];
|
|
43
|
-
for (const statement of file.statements) {
|
|
44
|
-
if (
|
|
45
|
-
!ts.isVariableStatement(statement) ||
|
|
46
|
-
!statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)
|
|
47
|
-
)
|
|
48
|
-
continue;
|
|
49
|
-
for (const declaration of statement.declarationList.declarations) {
|
|
50
|
-
const value = object(declaration.initializer);
|
|
51
|
-
const id = text(property(value, "id"));
|
|
52
|
-
if (!id) continue;
|
|
53
|
-
const exportName = declaration.name.getText(file);
|
|
54
|
-
const name = text(property(value, "name")) ?? exportName.replace(/([a-z\d])([A-Z])/g, "$1 $2");
|
|
55
|
-
const defaultTheme = text(property(value, "defaultTheme")) ?? text(property(meta, "defaultTheme"));
|
|
56
|
-
stories.push({ exportName, id, name, ...(defaultTheme ? { defaultTheme } : {}) });
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return {
|
|
60
|
-
title: text(property(meta, "title")) ?? path.replace(/\.canvas\.tsx?$/, ""),
|
|
61
|
-
component,
|
|
62
|
-
componentImport: component ? imports.get(component) : undefined,
|
|
63
|
-
stories,
|
|
64
|
-
};
|
|
65
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type CanvasFrame,
|
|
3
|
-
type CanvasMeta,
|
|
4
|
-
type CanvasPreview,
|
|
5
|
-
CanvasThemeContext,
|
|
6
|
-
composePreview,
|
|
7
|
-
} from "@pstdio/canvas";
|
|
8
|
-
|
|
9
|
-
interface PreviewInput {
|
|
10
|
-
kind: string;
|
|
11
|
-
suite: string;
|
|
12
|
-
source: string;
|
|
13
|
-
exportName?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const createPreviewLoader =
|
|
17
|
-
(
|
|
18
|
-
modules: Record<string, () => Promise<Record<string, unknown>>>,
|
|
19
|
-
projects: Record<string, () => Promise<{ default: Record<string, unknown> }>>,
|
|
20
|
-
) =>
|
|
21
|
-
async (input: PreviewInput, imported: () => void) => {
|
|
22
|
-
const started = performance.now();
|
|
23
|
-
const module = await modules[input.source]?.();
|
|
24
|
-
if (!module) throw new Error(`Missing preview source: ${input.source}`);
|
|
25
|
-
const project = (await projects[input.suite]()).default;
|
|
26
|
-
const exportName = input.exportName!;
|
|
27
|
-
if (!module[exportName]) throw new Error(`Missing preview: ${input.source}#${exportName}`);
|
|
28
|
-
const importedAt = performance.now();
|
|
29
|
-
imported();
|
|
30
|
-
const preview = composePreview(
|
|
31
|
-
module[exportName] as CanvasFrame,
|
|
32
|
-
module.default as CanvasMeta,
|
|
33
|
-
project as CanvasPreview,
|
|
34
|
-
);
|
|
35
|
-
// A preview with decorators themes its frames from this context; the runtime themes the others.
|
|
36
|
-
return {
|
|
37
|
-
...preview,
|
|
38
|
-
ThemeProvider: (project as CanvasPreview).decorators?.length ? CanvasThemeContext.Provider : undefined,
|
|
39
|
-
timings: { importMs: importedAt - started, setupMs: performance.now() - importedAt },
|
|
40
|
-
};
|
|
41
|
-
};
|