marko 5.37.48 → 5.37.49

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.
@@ -10,43 +10,55 @@ const defaultCreateOut = require("../../createOut");
10
10
  const dynamicTag5 = require("../dynamic-tag");
11
11
 
12
12
  exports.p = function (htmlCompat) {
13
- const outsByGlobal = new WeakMap();
13
+ const writersByGlobal = new WeakMap();
14
14
  const isMarko6 = (fn) => htmlCompat.isTagsAPI(fn);
15
15
  const isMarko5 = (fn) => !isMarko6(fn);
16
- const writeHTML = (result) => {
17
- const { out } = result;
18
- const $global = out.global;
19
- const outs = outsByGlobal.get($global);
20
- const writer = out._state.writer;
16
+ const writeClassAPIResultToTagsAPI = (result) => {
17
+ const { writer } = result.out._state;
21
18
  htmlCompat.write(writer._content);
22
- writer._content = "";
23
- if (outs) {
24
- outs.push(out);
25
- } else {
26
- outsByGlobal.set($global, [out]);
27
- }
19
+ htmlCompat.writeScript(writer._script);
20
+ writer._content = writer._scripts = "";
28
21
  };
22
+ const flushScripts = ($global, flushDefs) => {
23
+ const writers = writersByGlobal.get($global);
24
+ if (!writers) return "";
29
25
 
30
- htmlCompat.onFlush((chunk) => {
31
- const { $global } = chunk.boundary.state;
32
- const outs = outsByGlobal.get($global);
33
- if (outs) {
34
- chunk.render(() => {
35
- const defs = [];
36
- outsByGlobal.delete($global);
37
- for (const out of outs) {
38
- if (out.b_) {
39
- __(out.b_, defs);
40
- }
26
+ const { classAPI, tagsAPI } = writers;
27
+ let scripts = "";
28
+ let componentDefs = flushDefs;
41
29
 
42
- out._state.events.emit("c_", out._state.writer);
43
- chunk.writeScript(out._state.writer._content);
44
- chunk.writeScript(out._state.writer._scripts);
45
- }
30
+ if (classAPI.length) {
31
+ componentDefs = flushDefs ? flushDefs.concat(classAPI) : classAPI;
32
+ writers.classAPI = [];
33
+ }
46
34
 
47
- chunk.writeScript(_g_($global, defs));
48
- });
35
+ if (componentDefs) {
36
+ scripts = _g_($global, componentDefs);
37
+ }
38
+
39
+ if (tagsAPI.length) {
40
+ const [chunk] = tagsAPI;
41
+ for (let i = 1; i < tagsAPI.length; i++) {
42
+ chunk.append(tagsAPI[i]);
43
+ }
44
+
45
+ if (!chunk.boundary.done) {
46
+ throw new Error(
47
+ "Cannot serialize promise across tags/class compat layer."
48
+ );
49
+ }
50
+
51
+ scripts = concatScripts(chunk.flushScript().scripts, scripts);
52
+ writers.tagsAPI = [];
49
53
  }
54
+
55
+ return scripts;
56
+ };
57
+
58
+ htmlCompat.onFlush((chunk) => {
59
+ chunk.render(() => {
60
+ chunk.writeScript(flushScripts(chunk.boundary.state.$global));
61
+ });
50
62
  });
51
63
 
52
64
  dynamicTag5.bo_ = function tagsToVdom(
@@ -74,6 +86,21 @@ exports.p = function (htmlCompat) {
74
86
  const TagsCompatId = "tags-compat";
75
87
  const TagsCompat = createRenderer(
76
88
  function (_, out, componentDef, component) {
89
+ // class to tags
90
+ const $global = out.global;
91
+ let writers = writersByGlobal.get($global);
92
+ if (!writers) {
93
+ writersByGlobal.set($global, writers = { classAPI: [], tagsAPI: [] });
94
+ out.prependListener("c_", (writer) => {
95
+ const defs = writer._data?.componentDefs;
96
+ const scripts = flushScripts($global, defs);
97
+ if (scripts) {
98
+ if (defs) writer._data.componentDefs = undefined;
99
+ writer.script(scripts);
100
+ }
101
+ });
102
+ }
103
+
77
104
  const input = _.i;
78
105
  const tagsRenderer = _.r;
79
106
  const willRerender = componentDef._wrr || htmlCompat.isInResumedBranch();
@@ -83,8 +110,8 @@ exports.p = function (htmlCompat) {
83
110
  willRerender,
84
111
  out,
85
112
  component,
86
- input, "c_"
87
-
113
+ input,
114
+ writers.tagsAPI
88
115
  );
89
116
  out.ef();
90
117
  },
@@ -118,7 +145,15 @@ exports.p = function (htmlCompat) {
118
145
  htmlCompat.registerRenderBody(renderBody5);
119
146
  }
120
147
  return (input, ...args) => {
121
- const out = defaultCreateOut(htmlCompat.$global());
148
+ // tags to class
149
+ const $global = htmlCompat.$global();
150
+ htmlCompat.ensureState($global);
151
+ let writers = writersByGlobal.get($global);
152
+ if (!writers) {
153
+ writersByGlobal.set($global, writers = { classAPI: [], tagsAPI: [] });
154
+ }
155
+
156
+ const out = defaultCreateOut($global);
122
157
  const branchId = htmlCompat.nextScopeId();
123
158
  let customEvents;
124
159
 
@@ -159,9 +194,15 @@ exports.p = function (htmlCompat) {
159
194
 
160
195
  let async;
161
196
  out.once("finish", (result) => {
197
+ if (result.out.b_) {
198
+ __(
199
+ result.out.b_,
200
+ writers.classAPI
201
+ );
202
+ }
162
203
  if (!async) {
163
204
  async = false;
164
- writeHTML(result);
205
+ writeClassAPIResultToTagsAPI(result);
165
206
  }
166
207
  });
167
208
 
@@ -169,10 +210,14 @@ exports.p = function (htmlCompat) {
169
210
 
170
211
  if (async !== false) {
171
212
  async = true;
172
- htmlCompat.fork(scopeId, accessor, out, writeHTML);
213
+ htmlCompat.fork(scopeId, accessor, out, writeClassAPIResultToTagsAPI);
173
214
  }
174
215
  };
175
216
  });
176
217
 
177
218
  return htmlCompat.registerRenderer;
178
- };
219
+ };
220
+
221
+ function concatScripts(a, b) {
222
+ return a ? b ? a + ";" + b : a : b;
223
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marko",
3
- "version": "5.37.48",
3
+ "version": "5.37.49",
4
4
  "description": "UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.",
5
5
  "keywords": [
6
6
  "front-end",
@@ -10,43 +10,55 @@ const defaultCreateOut = require("../../createOut");
10
10
  const dynamicTag5 = require("../dynamic-tag");
11
11
 
12
12
  exports.p = function (htmlCompat) {
13
- const outsByGlobal = new WeakMap();
13
+ const writersByGlobal = new WeakMap();
14
14
  const isMarko6 = (fn) => htmlCompat.isTagsAPI(fn);
15
15
  const isMarko5 = (fn) => !isMarko6(fn);
16
- const writeHTML = (result) => {
17
- const { out } = result;
18
- const $global = out.global;
19
- const outs = outsByGlobal.get($global);
20
- const writer = out._state.writer;
16
+ const writeClassAPIResultToTagsAPI = (result) => {
17
+ const { writer } = result.out._state;
21
18
  htmlCompat.write(writer._content);
22
- writer._content = "";
23
- if (outs) {
24
- outs.push(out);
25
- } else {
26
- outsByGlobal.set($global, [out]);
27
- }
19
+ htmlCompat.writeScript(writer._script);
20
+ writer._content = writer._scripts = "";
28
21
  };
22
+ const flushScripts = ($global, flushDefs) => {
23
+ const writers = writersByGlobal.get($global);
24
+ if (!writers) return "";
29
25
 
30
- htmlCompat.onFlush((chunk) => {
31
- const { $global } = chunk.boundary.state;
32
- const outs = outsByGlobal.get($global);
33
- if (outs) {
34
- chunk.render(() => {
35
- const defs = [];
36
- outsByGlobal.delete($global);
37
- for (const out of outs) {
38
- if (out.___components) {
39
- ___addComponentsFromContext(out.___components, defs);
40
- }
26
+ const { classAPI, tagsAPI } = writers;
27
+ let scripts = "";
28
+ let componentDefs = flushDefs;
41
29
 
42
- out._state.events.emit("___toString", out._state.writer);
43
- chunk.writeScript(out._state.writer._content);
44
- chunk.writeScript(out._state.writer._scripts);
45
- }
30
+ if (classAPI.length) {
31
+ componentDefs = flushDefs ? flushDefs.concat(classAPI) : classAPI;
32
+ writers.classAPI = [];
33
+ }
46
34
 
47
- chunk.writeScript(___getInitComponentsCodeForDefs($global, defs));
48
- });
35
+ if (componentDefs) {
36
+ scripts = ___getInitComponentsCodeForDefs($global, componentDefs);
37
+ }
38
+
39
+ if (tagsAPI.length) {
40
+ const [chunk] = tagsAPI;
41
+ for (let i = 1; i < tagsAPI.length; i++) {
42
+ chunk.append(tagsAPI[i]);
43
+ }
44
+
45
+ if (!chunk.boundary.done) {
46
+ throw new Error(
47
+ "Cannot serialize promise across tags/class compat layer.",
48
+ );
49
+ }
50
+
51
+ scripts = concatScripts(chunk.flushScript().scripts, scripts);
52
+ writers.tagsAPI = [];
49
53
  }
54
+
55
+ return scripts;
56
+ };
57
+
58
+ htmlCompat.onFlush((chunk) => {
59
+ chunk.render(() => {
60
+ chunk.writeScript(flushScripts(chunk.boundary.state.$global));
61
+ });
50
62
  });
51
63
 
52
64
  dynamicTag5.___runtimeCompat = function tagsToVdom(
@@ -74,6 +86,21 @@ exports.p = function (htmlCompat) {
74
86
  const TagsCompatId = "tags-compat";
75
87
  const TagsCompat = createRenderer(
76
88
  function (_, out, componentDef, component) {
89
+ // class to tags
90
+ const $global = out.global;
91
+ let writers = writersByGlobal.get($global);
92
+ if (!writers) {
93
+ writersByGlobal.set($global, (writers = { classAPI: [], tagsAPI: [] }));
94
+ out.prependListener("___toString", (writer) => {
95
+ const defs = writer._data?.componentDefs;
96
+ const scripts = flushScripts($global, defs);
97
+ if (scripts) {
98
+ if (defs) writer._data.componentDefs = undefined;
99
+ writer.script(scripts);
100
+ }
101
+ });
102
+ }
103
+
77
104
  const input = _.i;
78
105
  const tagsRenderer = _.r;
79
106
  const willRerender = componentDef._wrr || htmlCompat.isInResumedBranch();
@@ -84,7 +111,7 @@ exports.p = function (htmlCompat) {
84
111
  out,
85
112
  component,
86
113
  input,
87
- "___toString",
114
+ writers.tagsAPI,
88
115
  );
89
116
  out.ef();
90
117
  },
@@ -118,7 +145,15 @@ exports.p = function (htmlCompat) {
118
145
  htmlCompat.registerRenderBody(renderBody5);
119
146
  }
120
147
  return (input, ...args) => {
121
- const out = defaultCreateOut(htmlCompat.$global());
148
+ // tags to class
149
+ const $global = htmlCompat.$global();
150
+ htmlCompat.ensureState($global);
151
+ let writers = writersByGlobal.get($global);
152
+ if (!writers) {
153
+ writersByGlobal.set($global, (writers = { classAPI: [], tagsAPI: [] }));
154
+ }
155
+
156
+ const out = defaultCreateOut($global);
122
157
  const branchId = htmlCompat.nextScopeId();
123
158
  let customEvents;
124
159
 
@@ -159,9 +194,15 @@ exports.p = function (htmlCompat) {
159
194
 
160
195
  let async;
161
196
  out.once("finish", (result) => {
197
+ if (result.out.___components) {
198
+ ___addComponentsFromContext(
199
+ result.out.___components,
200
+ writers.classAPI,
201
+ );
202
+ }
162
203
  if (!async) {
163
204
  async = false;
164
- writeHTML(result);
205
+ writeClassAPIResultToTagsAPI(result);
165
206
  }
166
207
  });
167
208
 
@@ -169,10 +210,14 @@ exports.p = function (htmlCompat) {
169
210
 
170
211
  if (async !== false) {
171
212
  async = true;
172
- htmlCompat.fork(scopeId, accessor, out, writeHTML);
213
+ htmlCompat.fork(scopeId, accessor, out, writeClassAPIResultToTagsAPI);
173
214
  }
174
215
  };
175
216
  });
176
217
 
177
218
  return htmlCompat.registerRenderer;
178
219
  };
220
+
221
+ function concatScripts(a, b) {
222
+ return a ? (b ? a + ";" + b : a) : b;
223
+ }