@lucidkiln/core 1.5.0 → 1.7.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/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as compare, $t as escapeUrl, A as consume, At as additionsIn, B as loadTemplate, Bt as fillScope, C as getAttr, Ct as contentSets, D as after, Dt as passesFrom, E as addSlotFlags, Et as passesAhead, F as fail, Ft as RenderState, G as renderTemplate, Gt as sectionCopy, H as locating, Ht as childScope, I as hold, It as Run, J as unhold, Jt as suggest, K as silence, Kt as sectionScope, L as holdData, Lt as blockScope, M as embed, Mt as closeOutlets, N as embedKeyed, Nt as recordAddition, O as all, Ot as runAhead, P as embedScope, Pt as takeOutput, Q as MISSING, Qt as escapeHtml, R as importsFor, Rt as emptyOut, S as toTime, St as atFront, T as readableNames, Tt as mayRunAhead, U as markCaller, Ut as lookup$1, V as locate, Vt as startCapture, W as passData, Wt as ownerOf, X as write, Xt as isSafe, Y as withCaller, Yt as Markup, Z as FORMATTERS, Zt as safe, _ as markupText, _t as unwrap, a as operatorFunction, an as KilnSyntaxError, at as isHashLike, b as toPieces, bt as expose, c as checkDepth, ct as mapKeys, d as functionArgument, dt as templateFunctions, en as escapeWith, et as describeType, f as insertedPiece, ft as toNumber, g as mapContents, gt as typeName, h as listOf$1, ht as truthy, i as contains, in as KilnRenderError, it as isEmpty, j as consumeAt, jt as checkLate, k as checkLength, kt as runSequence, l as entriesOf, lt as matchesType, m as isHtmlStrategy, mt as toStringArgument, n as builtinFunctions, nn as resolveTemplateName, nt as equals, o as rangeOperator, on as createDiagnostic, ot as isPlainObject, p as integerArgument, pt as toStr, q as step, qt as variableNames, r as builtinTests, rn as KilnIssue, rt as hashKeys, s as Kept, sn as issue, st as literalOptions, t as Source, tn as TEMPLATE_EXTENSION, tt as describeValue, u as fromPieces, ut as setOwn, v as readProperty, vt as valueLabel, w as getMethod, wt as isRoot, x as toPlainData, xt as lookup, y as scanHtml, yt as BLOCKED_NAMES, z as isPromise, zt as endCapture } from "./source-C2pjTx-Y.js";
1
+ import { $ as compare, $t as escapeUrl, A as consume, At as additionsIn, B as loadTemplate, Bt as fillScope, C as getAttr, Ct as contentSets, D as after, Dt as passesFrom, E as addSlotFlags, Et as passesAhead, F as fail, Ft as RenderState, G as renderTemplate, Gt as sectionCopy, H as locating, Ht as childScope, I as hold, It as Run, J as unhold, Jt as suggest, K as silence, Kt as sectionScope, L as holdData, Lt as blockScope, M as embed, Mt as closeOutlets, N as embedKeyed, Nt as recordAddition, O as all, Ot as runAhead, P as embedScope, Pt as takeOutput, Q as MISSING, Qt as escapeHtml, R as importsFor, Rt as emptyOut, S as toTime, St as atFront, T as readableNames, Tt as mayRunAhead, U as markCaller, Ut as lookup$1, V as locate, Vt as startCapture, W as passData, Wt as ownerOf, X as write, Xt as isSafe, Y as withCaller, Yt as Markup, Z as FORMATTERS, Zt as safe, _ as markupText, _t as unwrap, a as operatorFunction, an as KilnSyntaxError, at as isHashLike, b as toPieces, bt as expose, c as checkDepth, ct as mapKeys, d as functionArgument, dt as templateFunctions, en as escapeWith, et as describeType, f as insertedPiece, ft as toNumber, g as mapContents, gt as typeName, h as listOf$1, ht as truthy, i as contains, in as KilnRenderError, it as isEmpty, j as consumeAt, jt as checkLate, k as checkLength, kt as runSequence, l as entriesOf, lt as matchesType, m as isHtmlStrategy, mt as toStringArgument, n as builtinFunctions, nn as resolveTemplateName, nt as equals, o as rangeOperator, on as createDiagnostic, ot as isPlainObject, p as integerArgument, pt as toStr, q as step, qt as variableNames, r as builtinTests, rn as KilnIssue, rt as hashKeys, s as Kept, sn as issue, st as literalOptions, t as Source, tn as TEMPLATE_EXTENSION, tt as describeValue, u as fromPieces, ut as setOwn, v as readProperty, vt as valueLabel, w as getMethod, wt as isRoot, x as toPlainData, xt as lookup, y as scanHtml, yt as BLOCKED_NAMES, z as isPromise, zt as endCapture } from "./source-DxHQeBEo.js";
2
2
  import { a as ESCAPE_STRATEGIES, i as DEFAULT_SLOT, r as scanParts, t as parse } from "./parser-BZpRlGWi.js";
3
3
  //#region src/analyzer.ts
4
4
  const MAX_DEPTH$1 = 1e3;
@@ -3244,7 +3244,10 @@ var Compiler = class {
3244
3244
  });
3245
3245
  };
3246
3246
  if (!store) return after(fresh(), NOTHING$1);
3247
- const found = locating(run, span, () => store.get({ key: name }), "The cache store");
3247
+ const found = locating(run, span, () => store.get({
3248
+ key: name,
3249
+ state: state.userState
3250
+ }), "The cache store");
3248
3251
  return after(found, (value) => {
3249
3252
  const saved = typeof value === "string" ? unpackSaved(value) : null;
3250
3253
  if (saved) {
@@ -3284,7 +3287,8 @@ var Compiler = class {
3284
3287
  key: name,
3285
3288
  value,
3286
3289
  seconds: time,
3287
- tags
3290
+ tags,
3291
+ state: state.userState
3288
3292
  }), "The cache store");
3289
3293
  });
3290
3294
  });
@@ -6210,6 +6214,68 @@ var Generator = class {
6210
6214
  }
6211
6215
  };
6212
6216
  //#endregion
6217
+ //#region src/compiler/host.ts
6218
+ const hostMark = Symbol("kiln.hostComponent");
6219
+ /**
6220
+ * Wraps a function that renders a component, such as one made with a UI library, so a loader can return it for a
6221
+ * name. It should return markup made with `safe`, or a promise of it. A plain string is escaped.
6222
+ *
6223
+ * ```ts
6224
+ * load: (name) => name.endsWith(".tsx") ? hostComponent((props, slots) => safe(renderReact(name, props, slots))) : null
6225
+ * ```
6226
+ */
6227
+ const hostComponent = (render) => ({
6228
+ [hostMark]: true,
6229
+ render
6230
+ });
6231
+ const isHostComponent = (value) => typeof value === "object" && value !== null && hostMark in value;
6232
+ const SPAN = {
6233
+ start: 0,
6234
+ end: 0
6235
+ };
6236
+ /**
6237
+ * A template whose content is the host component: it renders the caller's fills, then calls the component with the
6238
+ * variables passed in and writes what it returns. Everything that renders templates renders it unchanged.
6239
+ */
6240
+ const compileHost = (env, name, component) => {
6241
+ const emit = emitter(env.autoescape);
6242
+ const label = `Component "${name}"`;
6243
+ const body = (run, scope, out) => {
6244
+ step(run, SPAN);
6245
+ const props = {};
6246
+ for (const key of run.passed ?? []) setOwn(props, key, scope[key]);
6247
+ return after(renderFills(run), (slots) => after(locating(run, SPAN, () => component.render(props, slots, run.info), label), (html) => write(run, out, emit(html), SPAN)));
6248
+ };
6249
+ return Object.assign(compileTemplate(env, name, ""), {
6250
+ body,
6251
+ host: true
6252
+ });
6253
+ };
6254
+ /**
6255
+ * Renders each fill the caller passed where the component is, as a slot would: the content outside fills first, then
6256
+ * each fill in order.
6257
+ */
6258
+ const renderFills = (run) => {
6259
+ const slots = {};
6260
+ if (run.slots === null) return slots;
6261
+ const fills = [...run.slots].sort(([a], [b]) => Number(b === DEFAULT_SLOT) - Number(a === DEFAULT_SLOT));
6262
+ const from = (index) => {
6263
+ for (let i = index; i < fills.length; i++) {
6264
+ const [name, fill] = fills[i];
6265
+ const out = emptyOut();
6266
+ const done = () => setOwn(slots, name, new Markup(out.s, "html"));
6267
+ const result = fill.body(fill.run.within(run.capture, run.blockDepth), fillScope(fill, run), out);
6268
+ if (result instanceof Promise) return result.then(() => {
6269
+ done();
6270
+ return from(i + 1);
6271
+ });
6272
+ done();
6273
+ }
6274
+ return slots;
6275
+ };
6276
+ return from(0);
6277
+ };
6278
+ //#endregion
6213
6279
  //#region src/html/data.ts
6214
6280
  /** Obsolete elements in the HTML standard, by what to use instead. */
6215
6281
  const OBSOLETE = new Map(Object.entries({
@@ -8304,7 +8370,8 @@ var Environment = class {
8304
8370
  if (source === null) return null;
8305
8371
  if (typeof source === "string") return compileTemplate(this, name, source);
8306
8372
  if (isPrecompiled(source)) return compilePrecompiled(this, name, source);
8307
- throw new TypeError(`Loader returned a ${typeName(source)} for "${name}", not a string, a precompiled template or null`);
8373
+ if (isHostComponent(source)) return compileHost(this, name, source);
8374
+ throw new TypeError(`Loader returned a ${typeName(source)} for "${name}", not a string, a precompiled template, a host component or null`);
8308
8375
  };
8309
8376
  const keep = (template) => {
8310
8377
  if (template !== null) this.#cache.set(name, template);
@@ -8618,7 +8685,7 @@ const createApi = (env) => {
8618
8685
  };
8619
8686
  /**
8620
8687
  * A loader for templates held in memory, keyed by name. Keys without an extension get `.kiln`. Each template is its
8621
- * source or a precompiled module's default export.
8688
+ * source, a precompiled module's default export or a host component.
8622
8689
  *
8623
8690
  * Works on every runtime, including Cloudflare Workers.
8624
8691
  */
@@ -8670,7 +8737,7 @@ const memoryCache = ({ max = 1e3 } = {}) => {
8670
8737
  //#region src/manifest.ts
8671
8738
  const hostNames = (registry) => [...registry].filter(([, entry]) => entry.kind === "host").map(([name]) => name).sort();
8672
8739
  /**
8673
- * Writes `kiln.json` for an environment: the names of its functions, filters, tests, globals and prop types, and its HTML check levels, so editors can check templates without running your code. `root` is the template folder, relative to where you write the file.
8740
+ * Writes `kiln.json` for an environment: the names of its functions, filters, tests, globals and prop types, and its HTML check levels, so editors can check templates without running your code. `root` is the template folder, relative to where you write the file, and `components` the files your loader returns as host components.
8674
8741
  *
8675
8742
  * ```ts
8676
8743
  * await writeFile("kiln.json", generateManifest(kiln, { root: "src/templates" }));
@@ -8686,7 +8753,8 @@ const generateManifest = (kiln, options) => {
8686
8753
  tests: hostNames(env.tests),
8687
8754
  globals: variableNames(env.globals).sort(),
8688
8755
  types: [...env.types.keys()].sort(),
8689
- htmlChecks: htmlChecksOption(env.htmlChecks)
8756
+ htmlChecks: htmlChecksOption(env.htmlChecks),
8757
+ components: options.components ?? []
8690
8758
  };
8691
8759
  return `${JSON.stringify(manifest, null, " ")}\n`;
8692
8760
  };
@@ -8829,6 +8897,6 @@ const generateTypes = async (kiln, names) => {
8829
8897
  ].join("\n");
8830
8898
  };
8831
8899
  //#endregion
8832
- export { KilnRenderError, KilnSyntaxError, Markup, builtins, createKiln, deferIslands, expose, generateManifest, generateTypes, isSafe, islandScript, lookup, memoryCache, memoryLoader, safe, streamIslands, templateText, withRender };
8900
+ export { KilnRenderError, KilnSyntaxError, Markup, builtins, createKiln, deferIslands, expose, generateManifest, generateTypes, hostComponent, isSafe, islandScript, lookup, memoryCache, memoryLoader, safe, streamIslands, templateText, withRender };
8833
8901
 
8834
8902
  //# sourceMappingURL=index.js.map