@mdsvex/render 1.0.0-next.3 → 1.0.0-next.4

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/scope.d.ts CHANGED
@@ -9,7 +9,31 @@ export interface ComponentImport {
9
9
  readonly specifier: string;
10
10
  /** generated local binding, internal and never api */
11
11
  readonly local: string;
12
+ /** a directive whose component renders at build time when a shell is given */
13
+ readonly static?: boolean;
12
14
  }
15
+ /**
16
+ * a static directive rendered once, its html with a hole where each snippet
17
+ * goes, html has one more entry than holes, braces escaped and comments gone
18
+ */
19
+ export interface Shell {
20
+ readonly html: readonly string[];
21
+ readonly holes: readonly ('label' | 'children')[];
22
+ /**
23
+ * import statements a document that uses the shell adds, the styles of
24
+ * the component and the files its html names, left out for none
25
+ */
26
+ readonly imports?: readonly string[];
27
+ }
28
+ /** the shells a render may use, by shell_key */
29
+ export interface ShellTable {
30
+ get(key: string): Shell | undefined;
31
+ }
32
+ /**
33
+ * the key of the shell for one use of a static directive, the component,
34
+ * its args and which snippets it is given, the same for every use alike
35
+ */
36
+ export declare function shell_key(specifier: string, name: string, args: Readonly<Record<string, string>> | undefined, label: boolean, children: boolean): string;
13
37
  /** an element component_mode all keeps because a component can not take its directive */
14
38
  export interface ReplaceWarning {
15
39
  readonly tag: string;
@@ -23,11 +47,19 @@ export interface ComponentSource {
23
47
  specifier: string;
24
48
  /** the export names of the module */
25
49
  names: string[];
50
+ /**
51
+ * the names among names whose component renders at build time, only for
52
+ * directives, left out for none, a renderer with no shell for one renders
53
+ * it live
54
+ */
55
+ static?: readonly string[];
26
56
  }
27
57
  export declare class ComponentScope {
28
58
  readonly parent: ComponentScope | null;
29
59
  /** names added here and in every parent */
30
60
  readonly size: number;
61
+ /** true when this scope or a parent serves a directive statically */
62
+ readonly has_static: boolean;
31
63
  private readonly own;
32
64
  private readonly locals;
33
65
  /**
@@ -39,6 +71,8 @@ export declare class ComponentScope {
39
71
  get(name: string): ComponentImport | undefined;
40
72
  private local_for;
41
73
  }
74
+ /** an import that only runs the module at specifier */
75
+ export declare function side_effect_import(specifier: string): string;
42
76
  /** a default import, a template the document is wrapped in */
43
77
  export interface DefaultImport {
44
78
  readonly specifier: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdsvex/render",
3
- "version": "1.0.0-next.3",
3
+ "version": "1.0.0-next.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/pngwn/MDsveX.git",
@@ -44,14 +44,24 @@ import {
44
44
  trace_take,
45
45
  } from './sourcemap.js';
46
46
  import type { MapTrace, SourceMapV3 } from './sourcemap.js';
47
- import { ComponentScope, component_imports } from './scope.js';
48
- import type { ComponentImport, ReplaceWarning } from './scope.js';
47
+ import {
48
+ ComponentScope,
49
+ component_imports,
50
+ shell_key,
51
+ side_effect_import,
52
+ } from './scope.js';
53
+ import type {
54
+ ComponentImport,
55
+ ReplaceWarning,
56
+ Shell,
57
+ ShellTable,
58
+ } from './scope.js';
49
59
  import { is_entity_name } from './entities.js';
50
60
  import { top_block } from './component.js';
51
61
 
52
62
  export type { Mapping, CodeInformation, MappingData } from './mappings.js';
53
63
  export { MapSink } from './mappings.js';
54
- export { ComponentScope, component_imports } from './scope.js';
64
+ export { ComponentScope, component_imports, shell_key } from './scope.js';
55
65
  /** @internal a pass over the tree reads references as the renderer does */
56
66
  export { is_entity_name } from './entities.js';
57
67
  export type {
@@ -59,6 +69,8 @@ export type {
59
69
  ComponentSource,
60
70
  DefaultImport,
61
71
  ReplaceWarning,
72
+ Shell,
73
+ ShellTable,
62
74
  } from './scope.js';
63
75
 
64
76
  /**
@@ -1013,6 +1025,15 @@ let comp_scope: ComponentScope | null = null;
1013
1025
  let dir_scope: ComponentScope | null = null;
1014
1026
  /** a directive no scope replaces throws, otherwise it renders as its children */
1015
1027
  let dir_strict = false;
1028
+ /** the shells of static directives, null renders every directive live */
1029
+ let dir_shells: ShellTable | null = null;
1030
+ /** comp_scan met a directive written as its shell, which imports nothing */
1031
+ let dir_shelled = false;
1032
+ /** the import lines of the shells comp_scan met, after those of the replacements */
1033
+ let shell_lines = '';
1034
+ /** each import line once, the specifiers of the modules a shelled directive comes from */
1035
+ const shell_seen = new Set<string>();
1036
+ const shell_specs: string[] = [];
1016
1037
  let comp_mode: number = CM.FOLD;
1017
1038
  /** component_mode all, typed lowercase elements are replaced too */
1018
1039
  let comp_all = false;
@@ -1060,7 +1081,8 @@ function hoist_reset(): void {
1060
1081
  */
1061
1082
  function hoist_begin(c: Cursor, buf: NodeBuffer, extra: boolean): void {
1062
1083
  const child = hoist_imports(buf);
1063
- if (hoist_nodes.length === 0 && !has_components && !extra) return;
1084
+ // comp_lines is empty for a document whose only replacements are shells
1085
+ if (hoist_nodes.length === 0 && comp_lines === '' && !extra) return;
1064
1086
  if (extra) comp_lines += extra_lines;
1065
1087
  hoist_script_from(c, buf, child);
1066
1088
  // no script and no import to start one, the render starts with one
@@ -1142,7 +1164,9 @@ function wrap_hoist_begin(c: Cursor, buf: NodeBuffer): void {
1142
1164
  component_imports(comp_used, {
1143
1165
  specifier: wrap_spec,
1144
1166
  local: WRAP_LOCAL,
1145
- }) + extra_lines;
1167
+ }) +
1168
+ shell_lines +
1169
+ extra_lines;
1146
1170
  if (declare) comp_lines += 'let ' + WRAP_PROPS + ' = $props();\n';
1147
1171
  wrap_open =
1148
1172
  '<' +
@@ -4319,6 +4343,23 @@ function comp_use_html(c: Cursor, name: string): void {
4319
4343
  else comp_warnings.push({ tag: name, directive, start: c.start });
4320
4344
  }
4321
4345
 
4346
+ /** a shelled directive imports what its shell names and runs its module for its styles */
4347
+ function note_shell(ref: ComponentImport, shell: Shell): void {
4348
+ dir_shelled = true;
4349
+ if (!shell_seen.has(ref.specifier)) {
4350
+ shell_seen.add(ref.specifier);
4351
+ shell_specs.push(ref.specifier);
4352
+ }
4353
+ const imports = shell.imports;
4354
+ if (imports === undefined) return;
4355
+ for (let i = 0; i < imports.length; i++) {
4356
+ const line = imports[i];
4357
+ if (shell_seen.has(line)) continue;
4358
+ shell_seen.add(line);
4359
+ shell_lines += line;
4360
+ }
4361
+ }
4362
+
4322
4363
  /** walk what the render walks below c and note each replacement it will use */
4323
4364
  function comp_scan(c: Cursor): void {
4324
4365
  if (!c.goto_first_child()) return;
@@ -4327,7 +4368,12 @@ function comp_scan(c: Cursor): void {
4327
4368
  if (k === K.TEXT || k === K.LINE_BREAK) continue;
4328
4369
  if (k >= K.DIRECTIVE_INLINE && k <= K.DIRECTIVE_CONTAINER) {
4329
4370
  const ref = dir_scope!.get(dir_name(c));
4330
- if (ref !== undefined) note_use(ref);
4371
+ // a directive written as its shell imports no component
4372
+ if (ref !== undefined) {
4373
+ const shell = ref.static === true ? dir_shell_of(c, ref) : undefined;
4374
+ if (shell === undefined) note_use(ref);
4375
+ else note_shell(ref, shell);
4376
+ }
4331
4377
  } else if (k === K.FOOTNOTE_DEFINITION) {
4332
4378
  // a definition is in the tree where it was written, the section holds it
4333
4379
  comp_use('footnotes');
@@ -4399,30 +4445,60 @@ function comp_begin(
4399
4445
  scope: ComponentScope | null,
4400
4446
  directives: ComponentScope | null,
4401
4447
  mode: number,
4402
- all: boolean
4448
+ all: boolean,
4449
+ shells: ShellTable | undefined
4403
4450
  ): readonly ReplaceWarning[] {
4404
4451
  comp_scope = scope ?? NO_SCOPE;
4405
4452
  dir_scope = directives ?? NO_SCOPE;
4453
+ if (shells !== undefined && dir_scope.has_static) dir_shells = shells;
4406
4454
  comp_mode = mode;
4407
4455
  comp_all = all;
4408
4456
  comp_scan(c);
4409
4457
  const warnings =
4410
4458
  comp_warnings.length === 0 ? NO_WARNINGS : comp_warnings.splice(0);
4411
- if (comp_used.length === 0) return warnings;
4459
+ // the walks route a shell through the replacement code too
4460
+ if (comp_used.length === 0 && !dir_shelled) return warnings;
4412
4461
  has_components = true;
4413
4462
  for (let i = 0; i < TABLE_PARTS.length; i++) {
4414
4463
  const ref = comp_scope.get(TABLE_PARTS[i]);
4415
4464
  if (ref !== undefined && comp_seen.has(ref)) comp_table = true;
4416
4465
  }
4417
- comp_lines = component_imports(comp_used);
4466
+ if (dir_shelled) shell_lines = shell_module_lines() + shell_lines;
4467
+ comp_lines = component_imports(comp_used) + shell_lines;
4418
4468
  return warnings;
4419
4469
  }
4420
4470
 
4471
+ /**
4472
+ * the modules of the shelled directives that no replacement already
4473
+ * imports from, run for their side effects, the styles a barrel imports
4474
+ */
4475
+ function shell_module_lines(): string {
4476
+ let s = '';
4477
+ for (let i = 0; i < shell_specs.length; i++) {
4478
+ const spec = shell_specs[i];
4479
+ let imported = false;
4480
+ for (let j = 0; j < comp_used.length; j++)
4481
+ if (comp_used[j].specifier === spec) {
4482
+ imported = true;
4483
+ break;
4484
+ }
4485
+ if (!imported) s += side_effect_import(spec);
4486
+ }
4487
+ return s;
4488
+ }
4489
+
4421
4490
  function comp_end(): void {
4422
4491
  has_components = false;
4423
4492
  comp_table = false;
4424
4493
  comp_scope = null;
4425
4494
  dir_scope = null;
4495
+ dir_shells = null;
4496
+ dir_shelled = false;
4497
+ if (shell_specs.length !== 0) {
4498
+ shell_specs.length = 0;
4499
+ shell_seen.clear();
4500
+ shell_lines = '';
4501
+ }
4426
4502
  comp_mode = CM.FOLD;
4427
4503
  comp_all = false;
4428
4504
  comp_lines = '';
@@ -5042,6 +5118,10 @@ function dir_node(
5042
5118
  p: number,
5043
5119
  ref: ComponentImport
5044
5120
  ): number {
5121
+ if (ref.static === true && dir_shells !== null) {
5122
+ const shell = dir_shell_of(c, ref);
5123
+ if (shell !== undefined) return dir_shell(c, sink, p, shell);
5124
+ }
5045
5125
  const pre = mo.length;
5046
5126
  const kind = c.kind as number;
5047
5127
  const local = ref.local;
@@ -5081,6 +5161,148 @@ function dir_node(
5081
5161
  return p;
5082
5162
  }
5083
5163
 
5164
+ /** which snippets the component of the directive at c is given */
5165
+ function dir_snippets(c: Cursor): { label: number; children: boolean } {
5166
+ const kind = c.kind as number;
5167
+ if (kind === K.DIRECTIVE_INLINE)
5168
+ return {
5169
+ label: Slot.NONE,
5170
+ children: c.words[c.index * W.stride + W.first_child] !== Slot.NONE,
5171
+ };
5172
+ return {
5173
+ label: dir_label_of(c),
5174
+ children: kind === K.DIRECTIVE_CONTAINER && dir_has_body(c),
5175
+ };
5176
+ }
5177
+
5178
+ /** the key of the shell the static directive at c renders with */
5179
+ function dir_shell_key(c: Cursor, ref: ComponentImport): string {
5180
+ const kind = c.kind as number;
5181
+ // throws for an arg the component takes as a snippet, as a live render does
5182
+ dir_props(c, kind);
5183
+ const { label, children } = dir_snippets(c);
5184
+ return shell_key(
5185
+ ref.specifier,
5186
+ ref.name,
5187
+ c.meta()?.args as Record<string, string> | undefined,
5188
+ label !== Slot.NONE,
5189
+ children
5190
+ );
5191
+ }
5192
+
5193
+ function dir_shell_of(c: Cursor, ref: ComponentImport): Shell | undefined {
5194
+ return dir_shells === null
5195
+ ? undefined
5196
+ : dir_shells.get(dir_shell_key(c, ref));
5197
+ }
5198
+
5199
+ /**
5200
+ * a static directive as the html of its shell, the label and the body
5201
+ * rendered into its holes as the walks render them
5202
+ */
5203
+ function dir_shell(
5204
+ c: Cursor,
5205
+ sink: MapSink | undefined,
5206
+ p: number,
5207
+ shell: Shell
5208
+ ): number {
5209
+ const pre = mo.length;
5210
+ const kind = c.kind as number;
5211
+ const { html, holes } = shell;
5212
+ p = cm_put(p, html[0]);
5213
+ if (holes.length === 0) {
5214
+ cm_void(c, sink, pre, Preset.STRUCTURE);
5215
+ return p;
5216
+ }
5217
+ const ao = mo.length;
5218
+ for (let i = 0; i < holes.length; i++) {
5219
+ if (holes[i] === 'label') {
5220
+ const at = c.index;
5221
+ c.move_to(dir_label_of(c));
5222
+ p = cm_children(c, sink, p);
5223
+ c.move_to(at);
5224
+ } else {
5225
+ // no line breaks around the body, outside a snippet svelte keeps them as a space
5226
+ p = cm_children(c, sink, p);
5227
+ }
5228
+ if (i === holes.length - 1) {
5229
+ const bc = mo.length;
5230
+ p = cm_put(p, html[i + 1]);
5231
+ cm_spans(
5232
+ c,
5233
+ sink,
5234
+ pre,
5235
+ ao,
5236
+ bc,
5237
+ mo.length,
5238
+ kind === K.DIRECTIVE_INLINE ? Preset.TEXT : Preset.STRUCTURE
5239
+ );
5240
+ } else p = cm_put(p, html[i + 1]);
5241
+ }
5242
+ return p;
5243
+ }
5244
+
5245
+ /** one use of a static directive with no shell yet */
5246
+ export interface ShellRequest {
5247
+ key: string;
5248
+ /** the module of the component, emitted verbatim as an import specifier */
5249
+ specifier: string;
5250
+ /** its export name, the directive name */
5251
+ name: string;
5252
+ args: Record<string, string>;
5253
+ label: boolean;
5254
+ children: boolean;
5255
+ /** the offset of the directive in the source */
5256
+ start: number;
5257
+ }
5258
+
5259
+ /**
5260
+ * every static directive of the document in directives that shells has no
5261
+ * shell for, one request per key in document order, empty when every use
5262
+ * has one
5263
+ */
5264
+ export function shell_requests(
5265
+ buf: NodeBuffer,
5266
+ source: string,
5267
+ directives: ComponentScope,
5268
+ shells: ShellTable | undefined
5269
+ ): ShellRequest[] {
5270
+ const out: ShellRequest[] = [];
5271
+ if (!directives.has_static) return out;
5272
+ const c = new Cursor(buf, source);
5273
+ const seen = new Set<string>();
5274
+ const walk = (): void => {
5275
+ if (!c.goto_first_child()) return;
5276
+ do {
5277
+ const k = c.kind as number;
5278
+ if (k >= K.DIRECTIVE_INLINE && k <= K.DIRECTIVE_CONTAINER) {
5279
+ const ref = directives.get(dir_name(c));
5280
+ if (ref !== undefined && ref.static === true) {
5281
+ const key = dir_shell_key(c, ref);
5282
+ if (!seen.has(key) && shells?.get(key) === undefined) {
5283
+ seen.add(key);
5284
+ const { label, children } = dir_snippets(c);
5285
+ const args = c.meta()?.args as Record<string, string> | undefined;
5286
+ out.push({
5287
+ key,
5288
+ specifier: ref.specifier,
5289
+ name: ref.name,
5290
+ args: args === undefined ? {} : { ...args },
5291
+ label: label !== Slot.NONE,
5292
+ children,
5293
+ start: c.start,
5294
+ });
5295
+ }
5296
+ }
5297
+ }
5298
+ if (k !== K.CODE_SPAN && k !== K.CODE_FENCE) walk();
5299
+ } while (c.goto_next_sibling());
5300
+ c.goto_parent();
5301
+ };
5302
+ walk();
5303
+ return out;
5304
+ }
5305
+
5084
5306
  /** table content where a part may be replaced */
5085
5307
  function cm_table(c: Cursor, sink: MapSink | undefined, p: number): number {
5086
5308
  const meta = c.meta();
@@ -5661,6 +5883,11 @@ export class CursorHTMLRenderer {
5661
5883
  * contents div, the cached render of update ignores it
5662
5884
  */
5663
5885
  declare wrap_static?: boolean;
5886
+ /**
5887
+ * the shells of the static directives of the next renders, a static
5888
+ * directive with none renders live, the cached render of update ignores it
5889
+ */
5890
+ declare shells?: ShellTable;
5664
5891
  /** the cached render met a footnote, so each update numbers them again */
5665
5892
  private noted = false;
5666
5893
 
@@ -5715,7 +5942,8 @@ export class CursorHTMLRenderer {
5715
5942
  scope,
5716
5943
  directives,
5717
5944
  CM.FOLD,
5718
- this.replace_typed
5945
+ this.replace_typed,
5946
+ this.shells
5719
5947
  );
5720
5948
  if (extra) extra_begin(imports);
5721
5949
  if (this.strict_directives) dir_strict = true;
@@ -5852,7 +6080,8 @@ export class CursorHTMLRenderer {
5852
6080
  scope,
5853
6081
  directives,
5854
6082
  trace ? CM.TRACE : CM.MAPPED,
5855
- this.replace_typed
6083
+ this.replace_typed,
6084
+ this.shells
5856
6085
  );
5857
6086
  if (extra) extra_begin(imports);
5858
6087
  if (this.strict_directives) dir_strict = true;
@@ -5915,7 +6144,14 @@ export class CursorHTMLRenderer {
5915
6144
  (scope !== null && scope.size !== 0) ||
5916
6145
  (directives !== null && directives.size !== 0)
5917
6146
  )
5918
- comp_last = comp_begin(c, scope, directives, mode, this.replace_typed);
6147
+ comp_last = comp_begin(
6148
+ c,
6149
+ scope,
6150
+ directives,
6151
+ mode,
6152
+ this.replace_typed,
6153
+ this.shells
6154
+ );
5919
6155
  if (extra) extra_begin(imports);
5920
6156
  if (this.strict_directives) dir_strict = true;
5921
6157
  if (this.wrap_static === true) ws_on = true;
@@ -6096,6 +6332,7 @@ export class CursorHTMLRenderer {
6096
6332
  this.smart_punctuation = undefined;
6097
6333
  if (this.footnotes !== undefined) this.footnotes = undefined;
6098
6334
  if (this.wrap_static !== undefined) this.wrap_static = undefined;
6335
+ if (this.shells !== undefined) this.shells = undefined;
6099
6336
  this.noted = false;
6100
6337
  // the escape index is module state and would keep the source alive,
6101
6338
  // every render resets it, a zero length clamps any text of '' to empty
package/src/scope.ts CHANGED
@@ -10,6 +10,46 @@ export interface ComponentImport {
10
10
  readonly specifier: string;
11
11
  /** generated local binding, internal and never api */
12
12
  readonly local: string;
13
+ /** a directive whose component renders at build time when a shell is given */
14
+ readonly static?: boolean;
15
+ }
16
+
17
+ /**
18
+ * a static directive rendered once, its html with a hole where each snippet
19
+ * goes, html has one more entry than holes, braces escaped and comments gone
20
+ */
21
+ export interface Shell {
22
+ readonly html: readonly string[];
23
+ readonly holes: readonly ('label' | 'children')[];
24
+ /**
25
+ * import statements a document that uses the shell adds, the styles of
26
+ * the component and the files its html names, left out for none
27
+ */
28
+ readonly imports?: readonly string[];
29
+ }
30
+
31
+ /** the shells a render may use, by shell_key */
32
+ export interface ShellTable {
33
+ get(key: string): Shell | undefined;
34
+ }
35
+
36
+ /**
37
+ * the key of the shell for one use of a static directive, the component,
38
+ * its args and which snippets it is given, the same for every use alike
39
+ */
40
+ export function shell_key(
41
+ specifier: string,
42
+ name: string,
43
+ args: Readonly<Record<string, string>> | undefined,
44
+ label: boolean,
45
+ children: boolean
46
+ ): string {
47
+ const pairs: string[] = [];
48
+ if (args !== undefined) {
49
+ const keys = Object.keys(args).sort();
50
+ for (let i = 0; i < keys.length; i++) pairs.push(keys[i], args[keys[i]]);
51
+ }
52
+ return JSON.stringify([specifier, name, pairs, label, children]);
13
53
  }
14
54
 
15
55
  /** an element component_mode all keeps because a component can not take its directive */
@@ -26,6 +66,12 @@ export interface ComponentSource {
26
66
  specifier: string;
27
67
  /** the export names of the module */
28
68
  names: string[];
69
+ /**
70
+ * the names among names whose component renders at build time, only for
71
+ * directives, left out for none, a renderer with no shell for one renders
72
+ * it live
73
+ */
74
+ static?: readonly string[];
29
75
  }
30
76
 
31
77
  const IDENT = /^[A-Za-z_$][\w$]*$/;
@@ -34,6 +80,8 @@ export class ComponentScope {
34
80
  readonly parent: ComponentScope | null;
35
81
  /** names added here and in every parent */
36
82
  readonly size: number;
83
+ /** true when this scope or a parent serves a directive statically */
84
+ readonly has_static: boolean;
37
85
  private readonly own = new Map<string, ComponentImport>();
38
86
  private readonly locals = new Set<string>();
39
87
 
@@ -47,17 +95,23 @@ export class ComponentScope {
47
95
  parent: ComponentScope | null = null
48
96
  ) {
49
97
  this.parent = parent;
98
+ let has_static = parent !== null && parent.has_static;
50
99
  for (let i = 0; i < sources.length; i++) {
51
100
  const { specifier, names } = sources[i];
101
+ const statics = sources[i].static;
52
102
  for (let j = 0; j < names.length; j++) {
53
103
  const name = names[j];
54
104
  const prior = this.own.get(name);
55
105
  // a replaced name keeps its local, which only this scope used
56
106
  const local = prior ? prior.local : this.local_for(name, suffix);
57
- this.own.set(name, { name, specifier, local });
107
+ if (statics !== undefined && statics.includes(name)) {
108
+ this.own.set(name, { name, specifier, local, static: true });
109
+ has_static = true;
110
+ } else this.own.set(name, { name, specifier, local });
58
111
  }
59
112
  }
60
113
  this.size = this.own.size + (parent === null ? 0 : parent.size);
114
+ this.has_static = has_static;
61
115
  }
62
116
 
63
117
  /** the replacement for an element name, closest scope first */
@@ -85,6 +139,11 @@ export class ComponentScope {
85
139
  }
86
140
  }
87
141
 
142
+ /** an import that only runs the module at specifier */
143
+ export function side_effect_import(specifier: string): string {
144
+ return 'import ' + js_string(specifier) + ';\n';
145
+ }
146
+
88
147
  function js_string(s: string): string {
89
148
  // specifiers are paths and ids, anything else is escaped as json
90
149
  return /^[\w$@#:./~+%-]*$/.test(s) ? "'" + s + "'" : JSON.stringify(s);