lilact 0.29.3 → 0.30.1

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.
Files changed (45) hide show
  1. package/dist/lilact.development.js +147 -66
  2. package/dist/lilact.development.js.map +3 -3
  3. package/dist/lilact.development.min.js +28 -28
  4. package/dist/lilact.development.min.js.map +3 -3
  5. package/dist/lilact.production.min.js +28 -28
  6. package/docs/assets/hierarchy.js +1 -1
  7. package/docs/assets/navigation.js +1 -1
  8. package/docs/assets/search.js +1 -1
  9. package/docs/classes/accessories.ErrorBoundary.html +8 -8
  10. package/docs/classes/accessories.Suspense.html +7 -7
  11. package/docs/classes/components.Component.html +11 -11
  12. package/docs/classes/components.HTMLComponent.html +11 -11
  13. package/docs/classes/components.RootComponent.html +11 -11
  14. package/docs/functions/components.cloneComponent.html +1 -1
  15. package/docs/functions/components.createComponent.html +1 -1
  16. package/docs/functions/components.createPortal.html +1 -1
  17. package/docs/functions/components.createRoot.html +1 -1
  18. package/docs/functions/components.hotReloadRoot.html +1 -0
  19. package/docs/functions/components.memo.html +1 -1
  20. package/docs/functions/components.render.html +1 -1
  21. package/docs/functions/components.replaceAllComponentInstances.html +1 -0
  22. package/docs/functions/components.replaceComponentEntity.html +1 -0
  23. package/docs/functions/run.lazy.html +1 -1
  24. package/docs/functions/run.run.html +1 -1
  25. package/docs/functions/run.runScripts.html +1 -1
  26. package/docs/hierarchy.html +1 -1
  27. package/docs/modules/components.html +1 -1
  28. package/docs/static/index.html +1 -1
  29. package/docs/static/lilact.development.js +147 -66
  30. package/docs/static/lilact.development.js.map +3 -3
  31. package/docs/static/lilact.development.min.js +28 -28
  32. package/docs/static/lilact.development.min.js.map +3 -3
  33. package/docs/static/lilact.production.min.js +28 -28
  34. package/docs/variables/components.cloneElement.html +1 -1
  35. package/examples/index.html +1 -1
  36. package/examples/lilact.development.js +147 -66
  37. package/examples/lilact.development.js.map +3 -3
  38. package/examples/lilact.development.min.js +28 -28
  39. package/examples/lilact.development.min.js.map +3 -3
  40. package/examples/lilact.production.min.js +28 -28
  41. package/package.json +1 -1
  42. package/src/components.jsx +200 -82
  43. package/src/jsx.js +7 -32
  44. package/src/lilact.jsx +1 -1
  45. package/src/run.jsx +86 -59
@@ -2350,6 +2350,7 @@ __export(components_exports, {
2350
2350
  current_component: () => current_component,
2351
2351
  effect_timeout: () => effect_timeout,
2352
2352
  events_set: () => events_set,
2353
+ hotReloadRoot: () => hotReloadRoot,
2353
2354
  insertion_effects: () => insertion_effects,
2354
2355
  layout_effects: () => layout_effects,
2355
2356
  length_css_attributes_set: () => length_css_attributes_set,
@@ -2357,6 +2358,8 @@ __export(components_exports, {
2357
2358
  passive_effects: () => passive_effects,
2358
2359
  processEffects: () => processEffects,
2359
2360
  render: () => render,
2361
+ replaceAllComponentInstances: () => replaceAllComponentInstances,
2362
+ replaceComponentEntity: () => replaceComponentEntity,
2360
2363
  roots: () => roots,
2361
2364
  special_attributes: () => special_attributes,
2362
2365
  update_cbs: () => update_cbs,
@@ -2368,29 +2371,91 @@ var SVG_NS = "http://www.w3.org/2000/svg";
2368
2371
  function getComponentEntity(entity) {
2369
2372
  return entity?.[MEMOIZED] ? entity.component : entity;
2370
2373
  }
2371
- function replaceComponentEntity(comp, nextEntity) {
2372
- const core = comp?.[CORE];
2373
- if (!core || typeof nextEntity !== "function") {
2374
- return;
2374
+ var componentInstances = /* @__PURE__ */ new Map();
2375
+ function registerComponentCore(core) {
2376
+ const entity = getComponentEntity(core.entity);
2377
+ if (typeof entity !== "function") return;
2378
+ let instances = componentInstances.get(entity);
2379
+ if (!instances) {
2380
+ instances = /* @__PURE__ */ new Set();
2381
+ componentInstances.set(entity, instances);
2382
+ }
2383
+ instances.add(core);
2384
+ core.registered_entity = entity;
2385
+ }
2386
+ function unregisterComponentCore(core) {
2387
+ const entity = core.registered_entity;
2388
+ if (!entity) return;
2389
+ const instances = componentInstances.get(entity);
2390
+ if (instances) {
2391
+ instances.delete(core);
2392
+ if (instances.size === 0) {
2393
+ componentInstances.delete(entity);
2394
+ }
2395
+ }
2396
+ delete core.registered_entity;
2397
+ }
2398
+ function moveComponentCore(core, oldEntity, newEntity) {
2399
+ const oldInstances = componentInstances.get(oldEntity);
2400
+ if (oldInstances) {
2401
+ oldInstances.delete(core);
2402
+ if (oldInstances.size === 0) {
2403
+ componentInstances.delete(oldEntity);
2404
+ }
2405
+ }
2406
+ let newInstances = componentInstances.get(newEntity);
2407
+ if (!newInstances) {
2408
+ newInstances = /* @__PURE__ */ new Set();
2409
+ componentInstances.set(newEntity, newInstances);
2410
+ }
2411
+ newInstances.add(core);
2412
+ core.registered_entity = newEntity;
2413
+ }
2414
+ function replaceComponentEntity(componentCore, nextEntity) {
2415
+ if (!componentCore || typeof nextEntity !== "function") {
2416
+ return false;
2375
2417
  }
2376
- const oldEntity = getComponentEntity(core.entity);
2418
+ const oldEntity = getComponentEntity(componentCore.entity);
2377
2419
  const newEntity = getComponentEntity(nextEntity);
2378
2420
  if (oldEntity === newEntity) {
2379
- return;
2421
+ return true;
2422
+ }
2423
+ const component = componentCore.component;
2424
+ if (!component) {
2425
+ return false;
2380
2426
  }
2381
- const oldIsClass = isClass(oldEntity);
2382
- const newIsClass = isClass(newEntity);
2383
- if (oldIsClass !== newIsClass) {
2427
+ if (typeof oldEntity !== "function" || typeof newEntity !== "function" || isClass(oldEntity) !== isClass(newEntity)) {
2384
2428
  return false;
2385
2429
  }
2386
- if (newIsClass) {
2387
- Object.setPrototypeOf(comp, newEntity.prototype);
2430
+ if (isClass(newEntity)) {
2431
+ Object.setPrototypeOf(component, newEntity.prototype);
2388
2432
  } else {
2389
- comp.render = newEntity.bind(comp);
2433
+ component.render = newEntity.bind(component);
2390
2434
  }
2391
- core.entity = newEntity;
2435
+ componentCore.entity = newEntity;
2436
+ moveComponentCore(componentCore, oldEntity, newEntity);
2392
2437
  return true;
2393
2438
  }
2439
+ function replaceAllComponentInstances(oldEntity, newEntity) {
2440
+ oldEntity = getComponentEntity(oldEntity);
2441
+ newEntity = getComponentEntity(newEntity);
2442
+ if (typeof oldEntity !== "function" || typeof newEntity !== "function") {
2443
+ return 0;
2444
+ }
2445
+ const instances = componentInstances.get(oldEntity);
2446
+ if (!instances) {
2447
+ return 0;
2448
+ }
2449
+ let count = 0;
2450
+ for (const core of [...instances]) {
2451
+ if (!replaceComponentEntity(core, newEntity)) {
2452
+ continue;
2453
+ }
2454
+ core.apply(core.props, core.state);
2455
+ count++;
2456
+ }
2457
+ return count;
2458
+ }
2394
2459
  var ComponentCache = class {
2395
2460
  constructor(owner) {
2396
2461
  __publicField(this, "owner");
@@ -2400,36 +2465,34 @@ var ComponentCache = class {
2400
2465
  this.owner = owner;
2401
2466
  }
2402
2467
  pick(key, construct_func, nextEntity) {
2403
- var _a;
2404
- let comp;
2468
+ let componentCore;
2405
2469
  let bucket = this.current_map.get(key);
2406
- const reusable = bucket && bucket.length > bucket[IDX];
2407
- if (reusable) {
2408
- comp = bucket[bucket[IDX]];
2470
+ if (bucket && bucket.length > bucket[IDX]) {
2471
+ componentCore = bucket[bucket[IDX]];
2409
2472
  bucket[IDX]++;
2410
- const oldEntity = getComponentEntity(comp?.[CORE]?.entity);
2473
+ const oldEntity = getComponentEntity(componentCore.entity);
2411
2474
  const newEntity = getComponentEntity(nextEntity);
2412
- const incompatible = typeof oldEntity === "function" && typeof newEntity === "function" && isClass(oldEntity) !== isClass(newEntity);
2475
+ const incompatible = typeof oldEntity !== typeof newEntity || typeof oldEntity === "function" && isClass(oldEntity) !== isClass(newEntity);
2413
2476
  if (incompatible) {
2414
- comp = construct_func();
2415
- } else {
2416
- replaceComponentEntity(comp, nextEntity);
2477
+ componentCore = construct_func();
2478
+ } else if (newEntity) {
2479
+ replaceComponentEntity(componentCore, newEntity);
2417
2480
  }
2418
2481
  } else {
2419
- comp = construct_func();
2482
+ componentCore = construct_func();
2420
2483
  }
2421
2484
  bucket = this.new_map.get(key);
2422
- if (bucket !== void 0) {
2423
- bucket.push(comp);
2485
+ if (bucket) {
2486
+ bucket.push(componentCore);
2424
2487
  } else {
2425
- bucket = [comp];
2488
+ bucket = [componentCore];
2426
2489
  this.new_map.set(key, bucket);
2427
2490
  bucket[IDX] = 0;
2428
2491
  }
2429
- if (comp[CORE]) {
2430
- (_a = comp[CORE]).parent ?? (_a.parent = this.owner);
2492
+ if (componentCore) {
2493
+ componentCore.parent ?? (componentCore.parent = this.owner);
2431
2494
  }
2432
- return comp;
2495
+ return componentCore;
2433
2496
  }
2434
2497
  commit() {
2435
2498
  this.current_map.forEach((arr) => {
@@ -2600,6 +2663,7 @@ var ComponentCore = class {
2600
2663
  }
2601
2664
  }
2602
2665
  async cleanup() {
2666
+ unregisterComponentCore(this);
2603
2667
  try {
2604
2668
  const promises = [];
2605
2669
  if (this.props?.ref) {
@@ -2884,6 +2948,7 @@ function constructFunc(core, parent) {
2884
2948
  throw new Error("Error in constructing component.");
2885
2949
  }
2886
2950
  comp[CORE].entity = entity;
2951
+ registerComponentCore(comp[CORE]);
2887
2952
  if (core.container) {
2888
2953
  comp[CORE].container = core.container;
2889
2954
  }
@@ -3170,9 +3235,26 @@ function createRoot(element) {
3170
3235
  root.cleanup();
3171
3236
  element.innerHTML = "";
3172
3237
  }
3238
+ },
3239
+ getCore() {
3240
+ console.log(root, root[CORE]);
3241
+ return root[CORE];
3173
3242
  }
3174
3243
  };
3175
3244
  }
3245
+ function hotReloadRoot(rootCore, nextEntity, props) {
3246
+ if (!rootCore || !rootCore.component) {
3247
+ throw new Error("Invalid root component.");
3248
+ }
3249
+ const replaced = replaceComponentEntity(rootCore, nextEntity);
3250
+ if (!replaced) {
3251
+ throw new Error(
3252
+ "Cannot hot-reload the root from a class component to a function component, or vice versa."
3253
+ );
3254
+ }
3255
+ rootCore.apply(props === void 0 ? rootCore.props : { ...rootCore.props, ...props });
3256
+ return rootCore;
3257
+ }
3176
3258
  function createPortal(children, element) {
3177
3259
  return createComponent2(lilact_default.Portal, { "view": element }, children);
3178
3260
  }
@@ -3812,7 +3894,10 @@ function require2(path2) {
3812
3894
  return loadModule(path2, options2);
3813
3895
  }
3814
3896
  function makeImportsObject(mod, options2 = {}) {
3815
- mod.importsObject = {};
3897
+ mod.importsObject ?? (mod.importsObject = {});
3898
+ for (const key of Object.keys(mod.importsObject)) {
3899
+ delete mod.importsObject[key];
3900
+ }
3816
3901
  for (const path2 in mod.meta.imports) {
3817
3902
  const imps = mod.meta.imports[path2];
3818
3903
  const exps = loadModule(path2, {
@@ -3832,7 +3917,7 @@ function makeImportsObject(mod, options2 = {}) {
3832
3917
  for (const i2 of imps.import_defaults) {
3833
3918
  if (!Object.prototype.hasOwnProperty.call(exps, "default")) {
3834
3919
  throw new Error(
3835
- `Imported module does not export a default.`
3920
+ "Imported module does not export a default."
3836
3921
  );
3837
3922
  }
3838
3923
  mod.importsObject[i2] = exps.default;
@@ -3841,9 +3926,26 @@ function makeImportsObject(mod, options2 = {}) {
3841
3926
  mod.importsObject[i2] = { ...exps };
3842
3927
  }
3843
3928
  }
3929
+ return mod.importsObject;
3930
+ }
3931
+ function exportEntries(value) {
3932
+ return value && typeof value === "object" ? value : { default: value };
3933
+ }
3934
+ function replaceReloadedExports(previousExports2, nextExports2) {
3935
+ if (!previousExports2) return;
3936
+ const oldExports = exportEntries(previousExports2);
3937
+ const newExports = exportEntries(nextExports2);
3938
+ for (const name of Object.keys(oldExports)) {
3939
+ const oldExport = oldExports[name];
3940
+ const newExport = newExports[name];
3941
+ if (typeof oldExport === "function" && typeof newExport === "function") {
3942
+ lilact_default.replaceAllComponentInstances(oldExport, newExport);
3943
+ }
3944
+ }
3844
3945
  }
3845
3946
  function run(jsx, path = `InlineJSX-${++lilact_default.eval_num}`, options = {}) {
3846
3947
  let module = required_scripts[path];
3948
+ const previousExports = module?.loaded ? module.exports : void 0;
3847
3949
  if (!module) {
3848
3950
  module = createModule(path, {
3849
3951
  code: jsx
@@ -3855,7 +3957,6 @@ function run(jsx, path = `InlineJSX-${++lilact_default.eval_num}`, options = {})
3855
3957
  module.exports = {};
3856
3958
  module.loaded = false;
3857
3959
  module.error = void 0;
3858
- module.importsObject = void 0;
3859
3960
  module.meta = {};
3860
3961
  }
3861
3962
  let processed;
@@ -3867,8 +3968,7 @@ function run(jsx, path = `InlineJSX-${++lilact_default.eval_num}`, options = {})
3867
3968
  factory: "createComponent",
3868
3969
  appendSourcemap: true,
3869
3970
  injectTraceLabels: true,
3870
- produceCJS: false,
3871
- referentiateImports: true,
3971
+ produceCJS: true,
3872
3972
  blocksInfo: lilact_default.blocksInfo
3873
3973
  });
3874
3974
  } catch (value) {
@@ -3887,11 +3987,11 @@ function run(jsx, path = `InlineJSX-${++lilact_default.eval_num}`, options = {})
3887
3987
  globalThis.Lilact = lilact_default;
3888
3988
  globalThis.createComponent = lilact_default.createComponent;
3889
3989
  globalThis.Fragment = lilact_default.Fragment;
3890
- makeImportsObject(module, options);
3891
- lilact_default.scriptImportsObject = module.importsObject;
3892
3990
  const result = eval(processed);
3893
3991
  module.loaded = true;
3894
- return isEmpty(module.exports) ? result : module.exports;
3992
+ const nextExports = isEmpty(module.exports) ? result : module.exports;
3993
+ replaceReloadedExports(previousExports, nextExports);
3994
+ return nextExports;
3895
3995
  } catch (value) {
3896
3996
  const error2 = report(value, path);
3897
3997
  error2.sourcePhase ?? (error2.sourcePhase = "runtime");
@@ -6829,11 +6929,6 @@ function transpileJSX(jsx2, {
6829
6929
  injectTraceLabels = false,
6830
6930
  discardComments = false,
6831
6931
  produceCJS = false,
6832
- referentiateImports = false,
6833
- importsObject = "Lilact.scriptImportsObject",
6834
- // first letter must be capital so components work as expected
6835
- importsObjectVar = "LilactImports",
6836
- // first letter must be capital so components work as expected
6837
6932
  logErrors = false,
6838
6933
  // lilact internal
6839
6934
  mappings = [],
@@ -6912,22 +7007,11 @@ function transpileJSX(jsx2, {
6912
7007
  const flattened_nodes = [];
6913
7008
  const codify = (outlen, node3, is_attr = false, is_xml = false) => {
6914
7009
  flattened_nodes.push(node3);
6915
- if (typeof node3 !== "object") {
6916
- if (referentiateImports && typeof node3 === "string") {
6917
- if (meta?.imported_names.has(node3)) {
6918
- let j = flattened_nodes.length - 2;
6919
- while (typeof flattened_nodes[j] === "string" && flattened_nodes[j].trim() === "" || typeof flattened_nodes[j] === "object" && flattened_nodes[j].type === "comment") j--;
6920
- if (typeof flattened_nodes[j] !== "string" || flattened_nodes[j] !== ".") {
6921
- return importsObjectVar + "." + node3;
6922
- }
6923
- }
6924
- }
6925
- return node3;
6926
- }
6927
- if (node3 === null) return "";
7010
+ if (typeof node3 !== "object") return node3;
7011
+ if (!node3) return "";
6928
7012
  node3.out_index = outlen;
6929
7013
  if (node3.type === "string") return jsx2.substring(node3.begin, node3.end);
6930
- if (node3.type === "import") return referentiateImports ? "" : node3.cjs;
7014
+ if (node3.type === "import") return node3.cjs;
6931
7015
  if (node3.type === "export") return node3.cjs;
6932
7016
  if (node3.type === "regex") return jsx2.substring(node3.begin, node3.end);
6933
7017
  if (node3.type === "comment") return discardComments || is_attr ? "" : jsx2.substring(node3.begin, node3.end);
@@ -6935,7 +7019,7 @@ function transpileJSX(jsx2, {
6935
7019
  if (node3.type === "parenthesis") {
6936
7020
  let out2 = "(";
6937
7021
  if (node3.out) {
6938
- if (produceCJS || referentiateImports) processImportExports(node3, jsx2, meta);
7022
+ if (produceCJS) processImportExports(node3, jsx2, meta);
6939
7023
  for (const ch2 of node3.out) {
6940
7024
  out2 += codify(outlen + out2.length - 1, ch2);
6941
7025
  }
@@ -6945,7 +7029,7 @@ function transpileJSX(jsx2, {
6945
7029
  if (node3.type === "js") {
6946
7030
  let out2 = "";
6947
7031
  if (node3.out) {
6948
- if (produceCJS || referentiateImports) processImportExports(node3, jsx2, meta);
7032
+ if (produceCJS) processImportExports(node3, jsx2, meta);
6949
7033
  for (const ch2 of node3.out) {
6950
7034
  if (is_xml && ch2.type === "comment") continue;
6951
7035
  out2 += codify(outlen + out2.length - (is_attr ? 1 : 0), ch2);
@@ -6960,9 +7044,6 @@ function transpileJSX(jsx2, {
6960
7044
  return out2;
6961
7045
  }
6962
7046
  if (node3.type === "xml") {
6963
- if (referentiateImports && meta?.imported_names?.has(node3.tag) && node3.tag[0] === node3.tag[0].toUpperCase()) {
6964
- node3.tag = importsObjectVar + "." + node3.tag;
6965
- }
6966
7047
  if (node3.tag.length === 0) {
6967
7048
  node3.tag = fragment;
6968
7049
  } else if (node3.tag[0] !== node3.tag[0].toUpperCase()) {
@@ -7000,9 +7081,6 @@ function transpileJSX(jsx2, {
7000
7081
  if (injectTraceLabels) {
7001
7082
  out = `/*LILACTBLOCK${++blocksInfo2.counter}:0,0:<EXEC>*/try{`;
7002
7083
  }
7003
- if (referentiateImports) {
7004
- out += `const ${importsObjectVar} = ${importsObject};`;
7005
- }
7006
7084
  out += codify(out.length, json);
7007
7085
  if (injectTraceLabels) {
7008
7086
  if (transpilerConfig.enableLabelStack) {
@@ -7020,7 +7098,7 @@ function transpileJSX(jsx2, {
7020
7098
 
7021
7099
  // .tmp/src/lilact.jsx
7022
7100
  var Lilact2 = {
7023
- VERSION: "RC.5",
7101
+ VERSION: "RC.6",
7024
7102
  // Configuration
7025
7103
  defaultTransitionTimeout: 300,
7026
7104
  defaultIsEqual: Object.is,
@@ -7147,6 +7225,7 @@ export {
7147
7225
  getComponentByPointer,
7148
7226
  globalErrorHandler,
7149
7227
  grabTimers,
7228
+ hotReloadRoot,
7150
7229
  id_num,
7151
7230
  insertion_effects,
7152
7231
  isAsync,
@@ -7167,6 +7246,8 @@ export {
7167
7246
  releaseSyntheticEvent,
7168
7247
  releaseTimers,
7169
7248
  render,
7249
+ replaceAllComponentInstances,
7250
+ replaceComponentEntity,
7170
7251
  require2 as require,
7171
7252
  required_scripts,
7172
7253
  resetTimers,