@lucidkiln/core 1.4.0 → 1.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.5.0
4
+
5
+ - New `globals` render option, which adds globals for one render on top of the environment's. ([8560145](https://github.com/buildlucid/kiln/commit/8560145ff40508c0fff2390677bfb75d0d51e306))
6
+ - Deferred island manifest entries now record the render's `locale` and `timeZone`, and both are part of the island's id, so ids from earlier versions change. ([8560145](https://github.com/buildlucid/kiln/commit/8560145ff40508c0fff2390677bfb75d0d51e306))
7
+
3
8
  ## 1.4.0
4
9
 
5
10
  - New `builtins` export, which lists Kiln's own function, filter and test names. ([8d99639](https://github.com/buildlucid/kiln/commit/8d99639916ce3dfb920d567ed4de9069ec739cd7))
@@ -53,10 +53,16 @@ declare class KilnRenderError extends Error {
53
53
  type JsonValue = null | boolean | number | string | JsonValue[] | {
54
54
  [key: string]: JsonValue;
55
55
  };
56
- /** One deferred island: the template to render and the props to render it with. */
56
+ /**
57
+ * One deferred island: the template to render, the props to render it with, and the locale and time zone of the render
58
+ * that deferred it. Hosts render it with `render(entry.template, entry.props, { locale: entry.locale, timeZone:
59
+ * entry.timeZone })`, plus any other render options it needs, such as `state` or `globals`.
60
+ */
57
61
  type IslandEntry = {
58
62
  template: string;
59
63
  props: Record<string, JsonValue>;
64
+ locale: string;
65
+ timeZone: string;
60
66
  };
61
67
  /** Every island deferred in a build, by id. Hosts save it as JSON and look ids up in it. */
62
68
  type IslandManifest = Record<string, IslandEntry>;
@@ -365,6 +371,12 @@ type KilnOptions<State = undefined, Schema extends KilnSchema = DefaultSchema> =
365
371
  types: TypeChecks<Schema["types"]>;
366
372
  });
367
373
  type BaseRenderOptions = {
374
+ /**
375
+ * Globals for this render, layered over the environment's. `check`, generated types and exports only know the
376
+ * environment's globals, so give each one a default there. `cache` keys don't include them, so add any that change a
377
+ * cached section's output to its key.
378
+ */
379
+ globals?: Record<string, unknown>;
368
380
  locale?: string;
369
381
  timeZone?: string;
370
382
  /** Stops the render when aborted. A real `AbortSignal` works. */
@@ -471,4 +483,4 @@ declare const memoryCache: ({ max }?: {
471
483
  };
472
484
  //#endregion
473
485
  export { CallArgument as A, IslandMode as B, withRender as C, PrecompiledTemplate as D, HtmlChecks as E, KilnCache as F, streamIslands as G, StreamedIslands as H, RenderInfo as I, KilnSyntaxError as J, Diagnostic as K, DeferredIslands as L, TemplateExport as M, TemplateReference as N, AnalysedProp as O, AbortSignalLike as P, IslandEntry as R, memoryLoader as S, HtmlCheckLevel as T, deferIslands as U, JsonValue as V, islandScript as W, TemplateLocation as Y, TemplateName as _, KilnGlobals as a, createKiln as b, KilnTemplates as c, Limits as d, Loader as f, TemplateContext as g, TemplateBlock as h, KilnFunctions as i, FunctionCall as j, Analysis as k, KilnTests as l, RenderOptions as m, KilnCallable as n, KilnOptions as o, RenderAware as p, KilnRenderError as q, KilnFilters as r, KilnSchema as s, Kiln as t, KilnTypes as u, TypeChecks as v, HtmlCheck as w, memoryCache as x, builtins as y, IslandManifest as z };
474
- //# sourceMappingURL=environment-CFAjojgY.d.ts.map
486
+ //# sourceMappingURL=environment-jAgaYony.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environment-jAgaYony.d.ts","names":[],"sources":["../src/errors.ts","../src/runtime/islands.ts","../src/runtime/types.ts","../src/analyzer.ts","../src/compiler/precompiled.ts","../src/html/options.ts","../src/environment.ts"],"mappings":";;KAEY;;EAEX;EACA;EACA;;EAEA;EACA,OAAO;EACP,KAAK;;KAGM;EACX;EACA;EACA;;;;;cAyBY,wBAAwB;EAC3B;WACA,aAAa;EAEtB,YAAY,aAAa,cAAc,QAAQ;;;;;cAqBnC,wBAAwB;;EAC3B;WACA;WACA;WACA;WACA;;WAEA;;WAEA,eAAe;;;;EAMxB,YACC,cACA,gBACA,QAAQ,iBACR,OAAM,MACN;IAAW;IAAe;;;M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,SACnE,QAAQ,eAAe;;EAE1B,MAAM,cAAc,kBAAkB,QAAQ;;EAE9C,QAAQ,cAAc,kBAAkB,QAAQ;;;;;EAKhD,SACC,MAAM,aAAa,sBACnB,uBACG,2BAA2B,SAC1B,UAAU,cAAc,WACxB,SAAS,cAAc,UACzB;;;;;EAKH,WAAW,cAAc,kBAAkB;;EAE3C,WAAW;;;;;;;;;cAUC,aAAc,aAAa,YAAU,IAC7C,SACF,YAAY;;cA2DF;WACH;WACA;WACA;;;;;;;;;;cA0hBG,aACZ,mBACA,eAAe,aAAa,eAAa,SAEhC,YAAY,OAAO,YAC1B,KAAK,OAAO;;;;;;;cAoHF,eAAY,WAErB,wBAAwB,uBACxB,qBAAqB,yBACtB,KAAK;EAAoB;;;;;;;;;;cAkBf,gBAAW;EAAsB;;;;;;;;;;;;EA0B1B,UAAA;IAAE"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { c as PropType, l as ShapeKey, m as Position, p as TypeName, r as EscapeStrategy } from "./ast-DzzSn9tS.js";
2
- import { A as CallArgument, B as IslandMode, C as withRender, D as PrecompiledTemplate, E as HtmlChecks, F as KilnCache, G as streamIslands, H as StreamedIslands, I as RenderInfo, J as KilnSyntaxError, K as Diagnostic, L as DeferredIslands, M as TemplateExport, N as TemplateReference, O as AnalysedProp, P as AbortSignalLike, R as IslandEntry, S as memoryLoader, T as HtmlCheckLevel, U as deferIslands, V as JsonValue, W as islandScript, Y as TemplateLocation, _ as TemplateName, a as KilnGlobals, b as createKiln, c as KilnTemplates, d as Limits, f as Loader, g as TemplateContext, h as TemplateBlock, i as KilnFunctions, j as FunctionCall, k as Analysis, l as KilnTests, m as RenderOptions, n as KilnCallable, o as KilnOptions, p as RenderAware, q as KilnRenderError, r as KilnFilters, s as KilnSchema, t as Kiln, u as KilnTypes, v as TypeChecks, w as HtmlCheck, x as memoryCache, y as builtins, z as IslandManifest } from "./environment-CFAjojgY.js";
2
+ import { A as CallArgument, B as IslandMode, C as withRender, D as PrecompiledTemplate, E as HtmlChecks, F as KilnCache, G as streamIslands, H as StreamedIslands, I as RenderInfo, J as KilnSyntaxError, K as Diagnostic, L as DeferredIslands, M as TemplateExport, N as TemplateReference, O as AnalysedProp, P as AbortSignalLike, R as IslandEntry, S as memoryLoader, T as HtmlCheckLevel, U as deferIslands, V as JsonValue, W as islandScript, Y as TemplateLocation, _ as TemplateName, a as KilnGlobals, b as createKiln, c as KilnTemplates, d as Limits, f as Loader, g as TemplateContext, h as TemplateBlock, i as KilnFunctions, j as FunctionCall, k as Analysis, l as KilnTests, m as RenderOptions, n as KilnCallable, o as KilnOptions, p as RenderAware, q as KilnRenderError, r as KilnFilters, s as KilnSchema, t as Kiln, u as KilnTypes, v as TypeChecks, w as HtmlCheck, x as memoryCache, y as builtins, z as IslandManifest } from "./environment-jAgaYony.js";
3
3
  //#region src/runtime/markup.d.ts
4
4
  /**
5
5
  * A string that is already safe to output with one escaping strategy.
package/dist/index.js CHANGED
@@ -1755,19 +1755,33 @@ const hashId = (text) => {
1755
1755
  return (a >>> 0).toString(16).padStart(8, "0") + (b >>> 0).toString(16).padStart(8, "0");
1756
1756
  };
1757
1757
  /**
1758
- * Records an island in the manifest and returns its id. The id comes from the template and props, so it is the same
1759
- * on every page and in every build, and a page cached from an earlier build never asks for a different island.
1758
+ * Records an island in the manifest and returns its id. The id comes from the template, props, locale and time zone,
1759
+ * so it is the same on every page and in every build, and a page cached from an earlier build never asks for a
1760
+ * different island.
1760
1761
  */
1761
1762
  const recordIsland = (run, span, islands, template, props) => {
1762
- const key = JSON.stringify([template, props]);
1763
+ const { locale, timeZone } = run.state;
1764
+ const key = JSON.stringify([
1765
+ template,
1766
+ props,
1767
+ locale,
1768
+ timeZone
1769
+ ]);
1763
1770
  const id = hashId(key);
1764
1771
  const { manifest } = islands;
1765
1772
  const existing = Object.hasOwn(manifest, id) ? manifest[id] : void 0;
1766
1773
  if (existing === void 0) manifest[id] = {
1767
1774
  template,
1768
- props
1775
+ props,
1776
+ locale,
1777
+ timeZone
1769
1778
  };
1770
- else if (JSON.stringify([existing.template, existing.props]) !== key) throw fail(run, span, "island-collision", `Two different islands have the id "${id}". Change one island's props slightly`);
1779
+ else if (JSON.stringify([
1780
+ existing.template,
1781
+ existing.props,
1782
+ existing.locale,
1783
+ existing.timeZone
1784
+ ]) !== key) throw fail(run, span, "island-collision", `Two different islands have the id "${id}". Change one island's props slightly`);
1771
1785
  run.capture?.section?.deferred.push(id);
1772
1786
  return id;
1773
1787
  };
@@ -3989,7 +4003,7 @@ const readingLabel = (key) => `Reading "${key}"`;
3989
4003
  /** `set`: assigns where the variable already exists, or declares it in the current scope. */
3990
4004
  const assign = (run, scope, name, value, span) => {
3991
4005
  let owner = ownerOf(scope, name);
3992
- if (owner === run.state.globals) throw fail(run, span, "assign-global", `Cannot assign to global "${name}"`);
4006
+ if (owner === run.state.globals || owner === run.state.env.globals) throw fail(run, span, "assign-global", `Cannot assign to global "${name}"`);
3993
4007
  if (owner !== null && run.capture?.section) owner = sectionCopy(scope, owner);
3994
4008
  (owner ?? scope)[name] = value ?? null;
3995
4009
  };
@@ -8280,11 +8294,7 @@ var Environment = class {
8280
8294
  if (typeof check !== "function") throw new TypeError(`type "${name}" must be a function`);
8281
8295
  this.types.set(name, check);
8282
8296
  }
8283
- const globals = childScope(null);
8284
- const provided = options.globals ?? {};
8285
- if (!isPlainObject(provided)) throw new TypeError("globals must be a plain object");
8286
- for (const key of hashKeys(provided)) globals[key] = provided[key] ?? null;
8287
- this.globals = globals;
8297
+ this.globals = globalScope(null, options.globals ?? {});
8288
8298
  }
8289
8299
  load(name) {
8290
8300
  const cached = this.#cache.get(name);
@@ -8446,7 +8456,7 @@ var Environment = class {
8446
8456
  const run = new Run(state, template, 0, null, null);
8447
8457
  const imports = importsFor(run, template);
8448
8458
  run.imports = isPromise(imports) ? await imports : imports;
8449
- const value = compiled.ev(run, childScope(this.globals));
8459
+ const value = compiled.ev(run, childScope(state.globals));
8450
8460
  if (!isPromise(value)) return value;
8451
8461
  const result = unhold(await untilAborted(value, state.signal, resolved));
8452
8462
  if (state.signal?.aborted) throw aborted(resolved);
@@ -8478,9 +8488,10 @@ var Environment = class {
8478
8488
  if (!template) throw new KilnRenderError("missing-template", `Template "${resolved}" was not found`, resolved);
8479
8489
  if (template.hasErrors) throw template.syntaxError();
8480
8490
  if (!isPlainObject(context)) throw new TypeError("render context must be a plain object");
8481
- const state = new RenderState(this, this.globals, this.limits.maxSteps, this.limits.maxOutputLength, this.limits.maxDepth, options.locale ?? this.locale, options.timeZone ?? this.timeZone, options.state, options.signal);
8491
+ const globals = options.globals === void 0 ? this.globals : globalScope(this.globals, options.globals);
8492
+ const state = new RenderState(this, globals, this.limits.maxSteps, this.limits.maxOutputLength, this.limits.maxDepth, options.locale ?? this.locale, options.timeZone ?? this.timeZone, options.state, options.signal);
8482
8493
  if (options.signal?.aborted) throw aborted(resolved);
8483
- const scope = childScope(this.globals);
8494
+ const scope = childScope(globals);
8484
8495
  const passed = hashKeys(context);
8485
8496
  for (const key of passed) scope[key] = context[key] ?? null;
8486
8497
  addSlotFlags(scope, template, null);
@@ -8501,6 +8512,13 @@ var Environment = class {
8501
8512
  return renderTemplate(render.state, template, render.scope, render.out, 0, null, render.passed, null, null, template.parentSpan, block);
8502
8513
  }
8503
8514
  };
8515
+ /** A scope inside `parent` holding `values`: the environment's globals, or one render's over them. */
8516
+ const globalScope = (parent, values) => {
8517
+ if (!isPlainObject(values)) throw new TypeError("globals must be a plain object");
8518
+ const scope = childScope(parent);
8519
+ for (const key of hashKeys(values)) scope[key] = values[key] ?? null;
8520
+ return scope;
8521
+ };
8504
8522
  const aborted = (resolved) => new KilnRenderError("aborted", "Render was aborted", resolved);
8505
8523
  /**
8506
8524
  * Waits for `work`, or rejects as soon as `signal` aborts, when the signal can say so, as a real `AbortSignal` can.