Skip to content

Commit 3a86ec3

Browse files
authored
Misc: fixes (#867)
* remove templates * refactor svelte code processing * remove endless loop in babel-transform * make svelte context reactive * update vue code processing * fix this binding * generate reactive svelte context * add reactive type to context * refactor svelte code processing * remove endless loop in babel-transform * make svelte context reactive * update vue code processing * fix this binding * generate reactive svelte context * add reactive type to context * Revert "remove templates" This reverts commit d2523aa. * bring back reactive, add reactive-proxy contrexts * update context example and snapshots * fix svelte reactive setter * fix vue binding processor * add example * improve svg types * remove onCreate * undo reactive context changes * update snapshots * fix vue getter logic * undo context snapshot changes * undo * remove new snapshot * remove import
1 parent 67066a4 commit 3a86ec3

File tree

21 files changed

+188
-149
lines changed

21 files changed

+188
-149
lines changed

packages/core/jsx-runtime.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ export declare namespace JSX {
10061006

10071007
interface ContainerElementSVGAttributes<T>
10081008
extends CoreSVGAttributes<T>,
1009+
ShapeElementSVGAttributes<T>,
10091010
Pick<
10101011
PresentationSVGAttributes,
10111012
| 'clip-path'
@@ -1078,6 +1079,7 @@ export declare namespace JSX {
10781079

10791080
interface ShapeElementSVGAttributes<T>
10801081
extends CoreSVGAttributes<T>,
1082+
ShapeElementSVGAttributes<T>,
10811083
Pick<
10821084
PresentationSVGAttributes,
10831085
| 'color'

packages/core/src/__tests__/__snapshots__/vue-composition.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,7 @@ const foo = inject(Context1);
17531753
provide(Context1, {
17541754
foo: \\"bar\\",
17551755
content() {
1756-
return content;
1756+
return props.content;
17571757
},
17581758
});
17591759
provide(Context2, { bar: \\"baz\\" });
@@ -4270,7 +4270,7 @@ const foo = inject(Context1);
42704270
provide(Context1, {
42714271
foo: \\"bar\\",
42724272
content() {
4273-
return content;
4273+
return props.content;
42744274
},
42754275
});
42764276
provide(Context2, { bar: \\"baz\\" });
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { setContext, onMount, onUpdate } from '@builder.io/mitosis';
2+
import {
3+
sendComponentsToVisualEditor,
4+
dispatchNewContentToVisualEditor,
5+
trackClick,
6+
} from '@dummy/injection-js';
7+
import BuilderContext from '@dummy/context.lite';
8+
import RenderBlocks from '@dummy/RenderBlocks.lite.tsx';
9+
10+
type Props = {
11+
customComponents: string[];
12+
content: { blocks: any[]; id: string };
13+
};
14+
15+
export default function RenderContent(props: Props) {
16+
onMount(() => {
17+
sendComponentsToVisualEditor(props.customComponents);
18+
});
19+
20+
onUpdate(() => {
21+
dispatchNewContentToVisualEditor(props.content);
22+
}, [props.content]);
23+
24+
setContext(BuilderContext, {
25+
get content() {
26+
return 3;
27+
},
28+
get registeredComponents() {
29+
return 4;
30+
},
31+
});
32+
33+
return (
34+
<div
35+
css={{ display: 'flex', flexDirection: 'columns' }}
36+
onClick={() => trackClick(props.content.id)}
37+
>
38+
<RenderBlocks blocks={props.content.blocks} />
39+
</div>
40+
);
41+
}

packages/core/src/__tests__/shared.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const spreadProps = getRawFile('./data/spread/spread-props.raw');
8989
const builderRenderContent = getRawFile('./data/blocks/builder-render-content.raw');
9090

9191
const rootFragmentMultiNode = getRawFile('./data/blocks/root-fragment-multi-node.raw');
92+
const renderContentExample = getRawFile('./data/render-content.raw');
9293

9394
const path = 'test-path';
9495

@@ -154,6 +155,7 @@ const BASIC_TESTS: Tests = {
154155
spreadAttrs,
155156
spreadNestedProps,
156157
spreadProps,
158+
// renderContentExample,
157159
};
158160

159161
const SLOTS_TESTS: Tests = {

packages/core/src/generators/angular.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const blockToAngular = (
124124
return mappers[json.name](json, options, blockOptions);
125125
}
126126

127-
if (isChildren(json)) {
127+
if (isChildren({ node: json })) {
128128
return `<ng-content></ng-content>`;
129129
}
130130

packages/core/src/generators/helpers/functions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { prefixWithFunction } from '../../helpers/patterns';
12
import { Plugin } from '../../modules/plugins';
23

34
export const FUNCTION_HACK_PLUGIN: Plugin = () => ({
@@ -7,7 +8,7 @@ export const FUNCTION_HACK_PLUGIN: Plugin = () => ({
78
const value = json.state[key]?.code;
89
const type = json.state[key]?.type;
910
if (typeof value === 'string' && type === 'method') {
10-
const newValue = `function ${value}`;
11+
const newValue = prefixWithFunction(value);
1112
json.state[key] = {
1213
code: newValue,
1314
type: 'method',

packages/core/src/generators/html.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ const blockToHtml = (
268268
return mappers[json.name](json, options, blockOptions);
269269
}
270270

271-
if (isChildren(json)) {
271+
if (isChildren({ node: json })) {
272272
return `<slot></slot>`;
273273
}
274274

packages/core/src/generators/svelte/blocks.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { stripStateAndPropsRefs } from '../../helpers/strip-state-and-props-refs';
21
import { selfClosingTags } from '../../parsers/jsx';
32
import { MitosisComponent } from '../../types/mitosis-component';
43
import { BaseNode, Binding, ForNode, MitosisNode } from '../../types/mitosis-node';
@@ -9,8 +8,8 @@ import { isSlotProperty, stripSlotPrefix } from '../../helpers/slots';
98
import { VALID_HTML_TAGS } from '../../constants/html_tags';
109
import { isUpperCase } from '../../helpers/is-upper-case';
1110
import { getForArguments } from '../../helpers/nodes/for';
12-
import { stripStateAndProps } from './helpers';
1311
import { ToSvelteOptions } from './types';
12+
import { stripStateAndProps } from './helpers';
1413

1514
const mappers: {
1615
For: BlockToSvelte<ForNode>;
@@ -42,16 +41,14 @@ const mappers: {
4241
const args = getForArguments(json, { excludeCollectionName: true }).join(', ');
4342

4443
return `
45-
{#each ${stripStateAndProps(json.bindings.each?.code, options)} as ${args} ${
46-
keyValue ? `(${keyValue})` : ''
47-
}}
44+
{#each ${json.bindings.each?.code} as ${args} ${keyValue ? `(${keyValue})` : ''}}
4845
${json.children.map((item) => blockToSvelte({ json: item, options, parentComponent })).join('\n')}
4946
{/each}
5047
`;
5148
},
5249
Show: ({ json, options, parentComponent }) => {
5350
return `
54-
{#if ${stripStateAndProps(json.bindings.when?.code, options)} }
51+
{#if ${json.bindings.when?.code} }
5552
${json.children.map((item) => blockToSvelte({ json: item, options, parentComponent })).join('\n')}
5653
5754
${
@@ -75,21 +72,20 @@ ${json.children.map((item) => blockToSvelte({ json: item, options, parentCompone
7572

7673
return `
7774
<span #${key}>
78-
${stripStateAndPropsRefs(json.bindings[key]?.code)}
75+
${json.bindings[key]?.code}
7976
</span>
8077
`;
8178
}
82-
const strippedTextCode = stripStateAndPropsRefs(json.bindings.name.code);
8379

84-
return `<slot name="${stripSlotPrefix(strippedTextCode).toLowerCase()}">${json.children
80+
return `<slot name="${stripSlotPrefix(json.bindings.name.code).toLowerCase()}">${json.children
8581
?.map((item) => blockToSvelte({ json: item, options, parentComponent }))
8682
.join('\n')}</slot>`;
8783
},
8884
};
8985

9086
const BINDINGS_MAPPER = {
9187
innerHTML: (json: MitosisNode, options: ToSvelteOptions) =>
92-
`{@html ${stripStateAndPropsRefs(json.bindings.innerHTML?.code)}}`,
88+
`{@html ${json.bindings.innerHTML?.code}}`,
9389
};
9490

9591
const SVELTE_SPECIAL_TAGS = {
@@ -101,9 +97,11 @@ const SVELTE_SPECIAL_TAGS = {
10197
const getTagName = ({
10298
json,
10399
parentComponent,
100+
options,
104101
}: {
105102
json: MitosisNode;
106103
parentComponent: MitosisComponent;
104+
options: ToSvelteOptions;
107105
}) => {
108106
if (parentComponent && json.name === parentComponent.name) {
109107
return SVELTE_SPECIAL_TAGS.SELF;
@@ -115,10 +113,13 @@ const getTagName = ({
115113
const hasMatchingImport = parentComponent.imports.some(({ imports }) =>
116114
Object.keys(imports).some((name) => name === json.name),
117115
);
116+
118117
// TO-DO: no way to decide between <svelte:component> and <svelte:element>...need to do that through metadata
119118
// overrides for now
120119
if (!isValidHtmlTag && !isSpecialSvelteTag && !hasMatchingImport) {
121-
json.bindings.this = { code: json.name };
120+
json.bindings.this = {
121+
code: stripStateAndProps({ json: parentComponent, options })(json.name),
122+
};
122123
return SVELTE_SPECIAL_TAGS.COMPONENT;
123124
}
124125

@@ -139,26 +140,25 @@ const stringifyBinding =
139140
}
140141

141142
const { code, arguments: cusArgs = ['event'], type } = binding;
142-
const useValue = stripStateAndProps(code, options);
143143

144144
if (type === 'spread') {
145-
const spreadValue = key === 'props' ? '$$props' : useValue;
145+
const spreadValue = key === 'props' ? '$$props' : code;
146146
return ` {...${spreadValue}} `;
147147
} else if (key.startsWith('on')) {
148148
const event = key.replace('on', '').toLowerCase();
149149
// TODO: handle quotes in event handler values
150150

151-
const valueWithoutBlock = removeSurroundingBlock(useValue);
151+
const valueWithoutBlock = removeSurroundingBlock(code);
152152

153153
if (valueWithoutBlock === key) {
154154
return ` on:${event}={${valueWithoutBlock}} `;
155155
} else {
156156
return ` on:${event}="{${cusArgs.join(',')} => {${valueWithoutBlock}}}" `;
157157
}
158158
} else if (key === 'ref') {
159-
return ` bind:this={${useValue}} `;
159+
return ` bind:this={${code}} `;
160160
} else {
161-
return ` ${key}={${useValue}} `;
161+
return ` ${key}={${code}} `;
162162
}
163163
};
164164

@@ -171,9 +171,9 @@ export const blockToSvelte: BlockToSvelte = ({ json, options, parentComponent })
171171
});
172172
}
173173

174-
const tagName = getTagName({ json, parentComponent });
174+
const tagName = getTagName({ json, parentComponent, options });
175175

176-
if (isChildren(json)) {
176+
if (isChildren({ node: json, extraMatches: ['$$slots.default'] })) {
177177
return `<slot></slot>`;
178178
}
179179

@@ -184,11 +184,10 @@ export const blockToSvelte: BlockToSvelte = ({ json, options, parentComponent })
184184
const textCode = json.bindings._text?.code;
185185

186186
if (textCode) {
187-
const strippedTextCode = stripStateAndProps(textCode, options);
188-
if (isSlotProperty(strippedTextCode)) {
189-
return `<slot name="${stripSlotPrefix(strippedTextCode).toLowerCase()}"/>`;
187+
if (isSlotProperty(textCode)) {
188+
return `<slot name="${stripSlotPrefix(textCode).toLowerCase()}"/>`;
190189
}
191-
return `{${strippedTextCode}}`;
190+
return `{${textCode}}`;
192191
}
193192

194193
let str = '';
@@ -197,10 +196,7 @@ export const blockToSvelte: BlockToSvelte = ({ json, options, parentComponent })
197196

198197
const isComponent = Boolean(tagName[0] && isUpperCase(tagName[0]));
199198
if ((json.bindings.style?.code || json.properties.style) && !isComponent) {
200-
const useValue = stripStateAndProps(
201-
json.bindings.style?.code || json.properties.style,
202-
options,
203-
);
199+
const useValue = json.bindings.style?.code || json.properties.style;
204200

205201
str += `use:mitosis_styling={${useValue}}`;
206202
delete json.bindings.style;
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { MitosisComponent } from '../../types/mitosis-component';
12
import { stripStateAndPropsRefs } from '../../helpers/strip-state-and-props-refs';
23
import { ToSvelteOptions } from './types';
34

4-
export const stripStateAndProps = (code: string | undefined, options: ToSvelteOptions) =>
5-
stripStateAndPropsRefs(code, {
6-
includeState: options.stateType === 'variables',
7-
replaceWith: (name) => (name === 'children' ? '$$slots.default' : name),
8-
});
5+
export const stripStateAndProps =
6+
({ options, json }: { options: ToSvelteOptions; json: MitosisComponent }) =>
7+
(code: string) =>
8+
stripStateAndPropsRefs(code, {
9+
includeState: options.stateType === 'variables',
10+
replaceWith: (name) => (name === 'children' ? '$$slots.default' : name),
11+
});

0 commit comments

Comments
 (0)