@reckona/mreact-compiler 0.0.66 → 0.0.68

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 (71) hide show
  1. package/dist/compiler-module-context.js.map +1 -1
  2. package/dist/diagnostics.js.map +1 -1
  3. package/dist/emit-client.js.map +1 -1
  4. package/dist/emit-compat.js.map +1 -1
  5. package/dist/emit-escape-helper.js.map +1 -1
  6. package/dist/emit-server-shared.js.map +1 -1
  7. package/dist/emit-server-stream.js.map +1 -1
  8. package/dist/emit-server.js.map +1 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/internal.js.map +1 -1
  11. package/dist/ir.js.map +1 -1
  12. package/dist/oxc-analysis-types.js.map +1 -1
  13. package/dist/oxc-await-analysis.js.map +1 -1
  14. package/dist/oxc-await-ids.js.map +1 -1
  15. package/dist/oxc-await-validation.js.map +1 -1
  16. package/dist/oxc-bindings.js.map +1 -1
  17. package/dist/oxc-body-lowering.js.map +1 -1
  18. package/dist/oxc-child-analysis.js.map +1 -1
  19. package/dist/oxc-code-utils.js.map +1 -1
  20. package/dist/oxc-component-detection.js.map +1 -1
  21. package/dist/oxc-component-props.js.map +1 -1
  22. package/dist/oxc-component-references.js.map +1 -1
  23. package/dist/oxc-dom-lowering.js.map +1 -1
  24. package/dist/oxc-expression-utils.js.map +1 -1
  25. package/dist/oxc-jsx-attributes.js.map +1 -1
  26. package/dist/oxc-jsx-text.js.map +1 -1
  27. package/dist/oxc-nested-lowering.js.map +1 -1
  28. package/dist/oxc-node-utils.js.map +1 -1
  29. package/dist/oxc-raw-jsx.js.map +1 -1
  30. package/dist/oxc-render-values.js.map +1 -1
  31. package/dist/oxc-runtime-emit.js.map +1 -1
  32. package/dist/oxc-transform.js.map +1 -1
  33. package/dist/oxc.js.map +1 -1
  34. package/dist/transform.js.map +1 -1
  35. package/dist/types.js.map +1 -1
  36. package/package.json +4 -3
  37. package/src/compiler-module-context.ts +31 -0
  38. package/src/diagnostics.ts +184 -0
  39. package/src/emit-client.ts +837 -0
  40. package/src/emit-compat.ts +567 -0
  41. package/src/emit-escape-helper.ts +45 -0
  42. package/src/emit-server-shared.ts +384 -0
  43. package/src/emit-server-stream.ts +2558 -0
  44. package/src/emit-server.ts +1827 -0
  45. package/src/index.ts +44 -0
  46. package/src/internal.ts +1905 -0
  47. package/src/ir.ts +151 -0
  48. package/src/oxc-analysis-types.ts +5 -0
  49. package/src/oxc-await-analysis.ts +165 -0
  50. package/src/oxc-await-ids.ts +62 -0
  51. package/src/oxc-await-validation.ts +117 -0
  52. package/src/oxc-bindings.ts +70 -0
  53. package/src/oxc-body-lowering.ts +430 -0
  54. package/src/oxc-child-analysis.ts +791 -0
  55. package/src/oxc-code-utils.ts +19 -0
  56. package/src/oxc-component-detection.ts +459 -0
  57. package/src/oxc-component-props.ts +170 -0
  58. package/src/oxc-component-references.ts +613 -0
  59. package/src/oxc-dom-lowering.ts +127 -0
  60. package/src/oxc-expression-utils.ts +42 -0
  61. package/src/oxc-jsx-attributes.ts +110 -0
  62. package/src/oxc-jsx-text.ts +84 -0
  63. package/src/oxc-nested-lowering.ts +319 -0
  64. package/src/oxc-node-utils.ts +65 -0
  65. package/src/oxc-raw-jsx.ts +239 -0
  66. package/src/oxc-render-values.ts +620 -0
  67. package/src/oxc-runtime-emit.ts +212 -0
  68. package/src/oxc-transform.ts +77 -0
  69. package/src/oxc.ts +932 -0
  70. package/src/transform.ts +634 -0
  71. package/src/types.ts +117 -0
@@ -0,0 +1,212 @@
1
+ import type { AttributeIr, ComponentPropIr, JsxNodeIr } from "./ir.js";
2
+ import { escapeHtmlAttribute } from "@reckona/mreact-shared/html-escape";
3
+
4
+ export const oxcServerStringReactNodeRenderHelperPlaceholder =
5
+ "__mreactRenderReactNodeToString";
6
+
7
+ export function emitOxcServerStringChildren(children: readonly JsxNodeIr[]): string {
8
+ if (children.length === 0) {
9
+ return '""';
10
+ }
11
+
12
+ return children.map(emitOxcServerStringNode).join(" + ");
13
+ }
14
+
15
+ function emitOxcServerStringNode(node: JsxNodeIr): string {
16
+ if (node.kind === "text") {
17
+ return JSON.stringify(node.value);
18
+ }
19
+
20
+ if (node.kind === "expr") {
21
+ return `_escapeHtml(${node.code})`;
22
+ }
23
+
24
+ if (node.kind === "conditional") {
25
+ return `((${node.conditionCode}) ? ${emitOxcServerStringChildren(node.whenTrue)} : ${emitOxcServerStringChildren(node.whenFalse)})`;
26
+ }
27
+
28
+ if (node.kind === "list") {
29
+ const parameters =
30
+ node.indexName === undefined ? node.itemName : `${node.itemName}, ${node.indexName}`;
31
+ return `(${node.itemsCode}).map((${parameters}) => ${emitOxcServerStringChildren(node.children)}).join("")`;
32
+ }
33
+
34
+ if (node.kind === "fragment") {
35
+ return emitOxcServerStringChildren(node.children);
36
+ }
37
+
38
+ if (node.kind === "component") {
39
+ const props = emitOxcServerComponentProps(node.props, node.children);
40
+ if (node.runtime === "compat") {
41
+ return `${oxcServerStringReactNodeRenderHelperPlaceholder}(${node.name}, ${props})`;
42
+ }
43
+ return `${node.name}(${props})`;
44
+ }
45
+
46
+ if (node.kind === "async-boundary") {
47
+ return '""';
48
+ }
49
+
50
+ const attrs = node.attributes.map(emitOxcServerAttribute).join(" + ");
51
+ const open =
52
+ attrs === ""
53
+ ? JSON.stringify(`<${node.tagName}>`)
54
+ : `${JSON.stringify(`<${node.tagName}`)} + ${attrs} + ">"`;
55
+ return `${open} + ${emitOxcServerStringChildren(node.children)} + ${JSON.stringify(`</${node.tagName}>`)}`;
56
+ }
57
+
58
+ function emitOxcServerComponentProps(
59
+ props: readonly ComponentPropIr[],
60
+ children: readonly JsxNodeIr[],
61
+ ): string {
62
+ const entries = props.map((prop) => {
63
+ if (prop.kind === "spread-prop") {
64
+ return `...(${prop.code})`;
65
+ }
66
+
67
+ if (prop.kind === "render-prop") {
68
+ return `${emitOxcCompatObjectPropName(prop.name)}: ${emitOxcServerStringChildren(prop.children)}`;
69
+ }
70
+
71
+ return `${emitOxcCompatObjectPropName(prop.name)}: (${prop.code})`;
72
+ });
73
+
74
+ if (children.length > 0) {
75
+ entries.push(`children: ${emitOxcServerStringChildren(children)}`);
76
+ }
77
+
78
+ return `{ ${entries.join(", ")} }`;
79
+ }
80
+
81
+ function emitOxcServerAttribute(attr: AttributeIr): string {
82
+ if (attr.kind === "static-attr") {
83
+ return JSON.stringify(` ${attr.name}="${escapeHtmlAttribute(attr.value)}"`);
84
+ }
85
+
86
+ if (attr.kind === "dynamic-attr") {
87
+ return `${JSON.stringify(` ${attr.name}="`)} + _escapeHtml(${attr.code}) + ${JSON.stringify('"')}`;
88
+ }
89
+
90
+ return '""';
91
+ }
92
+
93
+ function emitOxcCompatObjectNode(node: JsxNodeIr): string {
94
+ if (node.kind === "text") {
95
+ return JSON.stringify(node.value);
96
+ }
97
+
98
+ if (node.kind === "expr") {
99
+ return `(${node.code})`;
100
+ }
101
+
102
+ if (node.kind === "conditional") {
103
+ return `(${node.conditionCode}) ? ${emitOxcCompatObjectChildren(node.whenTrue)} : ${emitOxcCompatObjectChildren(node.whenFalse)}`;
104
+ }
105
+
106
+ if (node.kind === "list") {
107
+ const parameters =
108
+ node.indexName === undefined ? node.itemName : `${node.itemName}, ${node.indexName}`;
109
+ return `(${node.itemsCode}).map((${parameters}) => ${emitOxcCompatObjectChildren(node.children)})`;
110
+ }
111
+
112
+ if (node.kind === "fragment") {
113
+ return emitOxcCompatObjectElement('Symbol.for("modular.react.fragment")', [], node.children);
114
+ }
115
+
116
+ if (node.kind === "component") {
117
+ return emitOxcCompatObjectElement(
118
+ node.name,
119
+ node.props.map(emitOxcCompatObjectComponentProp),
120
+ node.children,
121
+ node.keyCode,
122
+ );
123
+ }
124
+
125
+ if (node.kind === "async-boundary") {
126
+ return "null";
127
+ }
128
+
129
+ return emitOxcCompatObjectElement(
130
+ JSON.stringify(node.tagName),
131
+ node.attributes.map(emitOxcCompatObjectAttribute),
132
+ node.children,
133
+ node.keyCode,
134
+ );
135
+ }
136
+
137
+ export function emitOxcCompatObjectChildren(children: readonly JsxNodeIr[]): string {
138
+ if (children.length === 0) {
139
+ return "null";
140
+ }
141
+
142
+ if (children.length === 1) {
143
+ return emitOxcCompatObjectNode(children[0] as JsxNodeIr);
144
+ }
145
+
146
+ return `[${children.map(emitOxcCompatObjectNode).join(", ")}]`;
147
+ }
148
+
149
+ function emitOxcCompatObjectElement(
150
+ typeCode: string,
151
+ propEntries: readonly string[],
152
+ children: readonly JsxNodeIr[],
153
+ explicitKeyCode?: string,
154
+ ): string {
155
+ const entries = [...propEntries];
156
+
157
+ if (children.length > 0) {
158
+ entries.push(`children: ${emitOxcCompatObjectChildren(children)}`);
159
+ }
160
+
161
+ const keyExpression =
162
+ explicitKeyCode === undefined
163
+ ? "_props.key === undefined ? null : String(_props.key)"
164
+ : `String(${explicitKeyCode})`;
165
+
166
+ return [
167
+ "(() => {",
168
+ ` const _props = { ${entries.join(", ")} };`,
169
+ ` const _key = ${keyExpression};`,
170
+ " const _ref = _props.ref ?? null;",
171
+ " delete _props.key;",
172
+ " delete _props.ref;",
173
+ ' return { $$typeof: Symbol.for("modular.react.element"),',
174
+ ` type: ${typeCode},`,
175
+ " key: _key,",
176
+ " ref: _ref,",
177
+ " props: _props };",
178
+ "})()",
179
+ ].join("\n");
180
+ }
181
+
182
+ function emitOxcCompatObjectAttribute(attr: AttributeIr): string {
183
+ if (attr.kind === "spread-attr") {
184
+ return `...(${attr.code})`;
185
+ }
186
+
187
+ if (attr.kind === "static-attr") {
188
+ return `${emitOxcCompatObjectPropName(attr.name)}: ${JSON.stringify(attr.value)}`;
189
+ }
190
+
191
+ if (attr.kind === "dynamic-attr") {
192
+ return `${emitOxcCompatObjectPropName(attr.name)}: (${attr.code})`;
193
+ }
194
+
195
+ return `${emitOxcCompatObjectPropName(attr.name)}: ${attr.code}`;
196
+ }
197
+
198
+ function emitOxcCompatObjectComponentProp(prop: ComponentPropIr): string {
199
+ if (prop.kind === "spread-prop") {
200
+ return `...(${prop.code})`;
201
+ }
202
+
203
+ if (prop.kind === "render-prop") {
204
+ return `${emitOxcCompatObjectPropName(prop.name)}: ${emitOxcCompatObjectChildren(prop.children)}`;
205
+ }
206
+
207
+ return `${emitOxcCompatObjectPropName(prop.name)}: (${prop.code})`;
208
+ }
209
+
210
+ function emitOxcCompatObjectPropName(name: string): string {
211
+ return /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name);
212
+ }
@@ -0,0 +1,77 @@
1
+ import { transformSync } from "oxc-transform";
2
+
3
+ export function stripTypeScriptWithOxc(source: string): string {
4
+ if (!needsTypeScriptStripping(source)) {
5
+ return source.trimEnd();
6
+ }
7
+
8
+ const result = transformSync("snippet.tsx", source, {
9
+ lang: "tsx",
10
+ sourceType: "module",
11
+ target: "es2022",
12
+ jsx: "preserve",
13
+ typescript: {
14
+ onlyRemoveTypeImports: true,
15
+ },
16
+ });
17
+
18
+ if (result.errors.length > 0) {
19
+ return source.trimEnd();
20
+ }
21
+
22
+ return result.code.trimEnd();
23
+ }
24
+
25
+ function needsTypeScriptStripping(source: string): boolean {
26
+ return (
27
+ /\bimport\s+type\b/.test(source) ||
28
+ /\btype\s+[A-Za-z_$][\w$]*\b/.test(source) ||
29
+ /\binterface\s+[A-Za-z_$][\w$]*\b/.test(source) ||
30
+ /\b[A-Za-z_$][\w$.]*\s*<[^>\n]+>\s*\(/.test(source) ||
31
+ /\bas\s+(?:const|[A-Za-z_$][\w$]*)\b/.test(source) ||
32
+ /:\s*[A-Za-z_$][\w$<>,\s|&.[\]?]*(?=[,)=;{])/.test(source)
33
+ );
34
+ }
35
+
36
+ export function transformJsxWithOxc(source: string): string {
37
+ const result = transformSync("snippet.tsx", source, {
38
+ lang: "tsx",
39
+ sourceType: "module",
40
+ target: "es2022",
41
+ jsx: {
42
+ runtime: "automatic",
43
+ importSource: "@reckona/mreact-compat",
44
+ },
45
+ typescript: {
46
+ onlyRemoveTypeImports: true,
47
+ },
48
+ });
49
+
50
+ if (result.errors.length > 0 && result.code === "") {
51
+ return stripTypeScriptWithOxc(source);
52
+ }
53
+
54
+ return result.code.trimEnd();
55
+ }
56
+
57
+ export function transformJsxToCreateElementWithOxc(source: string): string {
58
+ const result = transformSync("snippet.tsx", source, {
59
+ lang: "tsx",
60
+ sourceType: "module",
61
+ target: "es2022",
62
+ jsx: {
63
+ runtime: "classic",
64
+ pragma: "createElement",
65
+ pragmaFrag: "Fragment",
66
+ },
67
+ typescript: {
68
+ onlyRemoveTypeImports: true,
69
+ },
70
+ });
71
+
72
+ if (result.errors.length > 0 && result.code === "") {
73
+ return stripTypeScriptWithOxc(source);
74
+ }
75
+
76
+ return result.code.trimEnd();
77
+ }