@backmatter/core 0.0.42 → 0.0.43
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 +1 -1
- package/src/block.ts +5 -0
- package/src/node/eval-block.ts +13 -2
- package/src/node/local-modules.ts +23 -1
package/package.json
CHANGED
package/src/block.ts
CHANGED
|
@@ -106,6 +106,11 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
|
106
106
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
/** The examples in `value` that have a name and an object of props; the rest are dropped. */
|
|
110
|
+
export function validBlockExamples(value: unknown): BlockExample[] {
|
|
111
|
+
return readBlockExamples(value, false) ?? [];
|
|
112
|
+
}
|
|
113
|
+
|
|
109
114
|
/** Valid examples; null when `strict` and any entry is bad (otherwise bad ones are dropped). */
|
|
110
115
|
function readBlockExamples(value: unknown, strict: boolean): BlockExample[] | null {
|
|
111
116
|
if (!Array.isArray(value)) return strict ? null : [];
|
package/src/node/eval-block.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// SaaS never loads customer React, Next, or the host checkout.
|
|
5
5
|
import { runInNewContext } from "node:vm";
|
|
6
6
|
import { field } from "../fields";
|
|
7
|
-
import { defineBlock, type BlockDef } from "../block";
|
|
7
|
+
import { defineBlock, validBlockExamples, type BlockDef } from "../block";
|
|
8
8
|
import { compileLocalModules, pickBindings, withLocalModules, type LocalModules } from "./local-modules";
|
|
9
9
|
|
|
10
10
|
const SOURCE_EXT = /\.(ts|tsx|js|jsx|mjs)$/;
|
|
@@ -143,6 +143,17 @@ export type BlockSourceOptions = {
|
|
|
143
143
|
files?: ReadonlyMap<string, string>;
|
|
144
144
|
};
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Examples only dress the block library. When a site helper that builds them
|
|
148
|
+
* comes back stubbed or wrong in the sandbox, drop the bad ones, not the block.
|
|
149
|
+
*/
|
|
150
|
+
function defineBlockKeepingValidExamples(def: Record<string, unknown>): BlockDef {
|
|
151
|
+
if (def.examples === undefined) return defineBlock(def as Parameters<typeof defineBlock>[0]);
|
|
152
|
+
const { examples, ...rest } = def;
|
|
153
|
+
const valid = validBlockExamples(examples);
|
|
154
|
+
return defineBlock((valid.length > 0 ? { ...rest, examples: valid } : rest) as Parameters<typeof defineBlock>[0]);
|
|
155
|
+
}
|
|
156
|
+
|
|
146
157
|
function transpile(source: string): string {
|
|
147
158
|
const transpiler = new Bun.Transpiler({ loader: "tsx", target: "node" });
|
|
148
159
|
return transpiler.transformSync(source);
|
|
@@ -171,7 +182,7 @@ export function evalBlockSource(
|
|
|
171
182
|
jsxDEV: (...args: unknown[]) => null;
|
|
172
183
|
};
|
|
173
184
|
const el = () => null;
|
|
174
|
-
sandbox.defineBlock = defineBlock;
|
|
185
|
+
sandbox.defineBlock = defineBlockKeepingValidExamples as typeof defineBlock;
|
|
175
186
|
sandbox.field = field;
|
|
176
187
|
sandbox.React = { createElement: el, Fragment: "Fragment" };
|
|
177
188
|
sandbox.jsx = el;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// files (`../lib/block-fields`). The block sandbox runs those files too, under
|
|
4
4
|
// the same rules, so their exports carry real values instead of stubs. A file
|
|
5
5
|
// that is missing, breaks a rule, or throws falls back to stubs for its names.
|
|
6
|
+
// A `.json` import (content a site reuses for block examples) is plain data.
|
|
6
7
|
|
|
7
8
|
const SOURCE_EXTS = [".ts", ".tsx", ".js", ".jsx", ".mjs"];
|
|
8
9
|
const INDEX_FILES = ["/index.ts", "/index.tsx", "/index.js"];
|
|
@@ -115,7 +116,9 @@ function compileModule(repoPath: string, ctx: CompileContext): number | null {
|
|
|
115
116
|
// A cycle, or too many files: the importer gets stubs for this one.
|
|
116
117
|
if (ctx.inProgress.has(repoPath) || ctx.indexes.size >= MAX_MODULES) return null;
|
|
117
118
|
const source = ctx.files.get(repoPath);
|
|
118
|
-
if (source === undefined
|
|
119
|
+
if (source === undefined) return null;
|
|
120
|
+
if (repoPath.endsWith(".json")) return compileJson(repoPath, source, ctx);
|
|
121
|
+
if (ctx.isForbidden(source)) return null;
|
|
119
122
|
|
|
120
123
|
let js: string;
|
|
121
124
|
try {
|
|
@@ -151,6 +154,25 @@ function compileModule(repoPath: string, ctx: CompileContext): number | null {
|
|
|
151
154
|
return index;
|
|
152
155
|
}
|
|
153
156
|
|
|
157
|
+
/**
|
|
158
|
+
* A JSON file is data: its value is the default export, and an object's
|
|
159
|
+
* top-level keys are named exports, as bundlers do. Invalid JSON gets stubs.
|
|
160
|
+
*/
|
|
161
|
+
function compileJson(repoPath: string, source: string, ctx: CompileContext): number | null {
|
|
162
|
+
let value: unknown;
|
|
163
|
+
try {
|
|
164
|
+
value = JSON.parse(source);
|
|
165
|
+
} catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const index = ctx.indexes.size;
|
|
169
|
+
ctx.indexes.set(repoPath, index);
|
|
170
|
+
ctx.chunks.push(
|
|
171
|
+
`__mods[${index}] = (function (d) { var m = d && typeof d === "object" && !Array.isArray(d) ? Object.assign({}, d) : {}; m["default"] = d; return m; })(${JSON.stringify(value)});`,
|
|
172
|
+
);
|
|
173
|
+
return index;
|
|
174
|
+
}
|
|
175
|
+
|
|
154
176
|
/** Strip `export` keywords and list [exported name, local expression] pairs. */
|
|
155
177
|
function collectExports(js: string): { body: string; names: Array<[string, string]> } {
|
|
156
178
|
const names: Array<[string, string]> = [];
|