@mdsvex/render 1.0.0-next.2 → 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/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);