@builder.io/mitosis 0.5.2 → 0.5.3

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.
@@ -250,20 +250,23 @@ const getPropsDefinition = ({ json }) => {
250
250
  .join(',');
251
251
  return `${json.name}.defaultProps = {${defaultPropsString}};`;
252
252
  };
253
- const checkShouldAddUseClientDirective = (json, options) => {
253
+ const isRSC = (json, options) => {
254
254
  var _a, _b;
255
+ // When using RSC generator, we check `componentType` field in metadata to determine if it's a server component
256
+ const componentType = (_b = (_a = json.meta.useMetadata) === null || _a === void 0 ? void 0 : _a.rsc) === null || _b === void 0 ? void 0 : _b.componentType;
257
+ if (options.rsc && (0, nullable_1.checkIsDefined)(componentType)) {
258
+ return componentType === 'server';
259
+ }
260
+ return !(0, rsc_1.checkIfIsClientComponent)(json);
261
+ };
262
+ const checkShouldAddUseClientDirective = (json, options) => {
255
263
  if (!options.addUseClientDirectiveIfNeeded)
256
264
  return false;
257
265
  if (options.type === 'native')
258
266
  return false;
259
267
  if (options.preact)
260
268
  return false;
261
- // When using RSC generator, we check `componentType` field in metadata to determine if it's a server component
262
- const componentType = (_b = (_a = json.meta.useMetadata) === null || _a === void 0 ? void 0 : _a.rsc) === null || _b === void 0 ? void 0 : _b.componentType;
263
- if (options.rsc && (0, nullable_1.checkIsDefined)(componentType)) {
264
- return componentType === 'client';
265
- }
266
- return (0, rsc_1.checkIfIsClientComponent)(json);
269
+ return !isRSC(json, options);
267
270
  };
268
271
  const _componentToReact = (json, options, isSubComponent = false) => {
269
272
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
@@ -320,7 +323,9 @@ const _componentToReact = (json, options, isSubComponent = false) => {
320
323
  if ((0, context_1.hasContext)(json) && options.contextType !== 'prop-drill') {
321
324
  reactLibImports.add('useContext');
322
325
  }
323
- if (allRefs.length || ((_g = json.hooks.onInit) === null || _g === void 0 ? void 0 : _g.code)) {
326
+ const shouldAddUseClientDirective = checkShouldAddUseClientDirective(json, options);
327
+ const shouldInlineOnInitHook = !shouldAddUseClientDirective && options.rsc && isRSC(json, options);
328
+ if (allRefs.length || (((_g = json.hooks.onInit) === null || _g === void 0 ? void 0 : _g.code) && !shouldInlineOnInitHook)) {
324
329
  reactLibImports.add('useRef');
325
330
  }
326
331
  if (!options.preact && hasPropRef) {
@@ -382,13 +387,15 @@ const _componentToReact = (json, options, isSubComponent = false) => {
382
387
  ${contextStr || ''}
383
388
 
384
389
  ${((_o = json.hooks.onInit) === null || _o === void 0 ? void 0 : _o.code)
385
- ? `
390
+ ? shouldInlineOnInitHook
391
+ ? (0, state_2.processHookCode)({ str: json.hooks.onInit.code, options })
392
+ : `
386
393
  const hasInitialized = useRef(false);
387
394
  if (!hasInitialized.current) {
388
395
  ${(0, state_2.processHookCode)({
389
- str: json.hooks.onInit.code,
390
- options,
391
- })}
396
+ str: json.hooks.onInit.code,
397
+ options,
398
+ })}
392
399
  hasInitialized.current = true;
393
400
  }
394
401
  `
@@ -445,7 +452,6 @@ const _componentToReact = (json, options, isSubComponent = false) => {
445
452
  ${wrap ? (0, helpers_2.closeFrag)(options) : ''}
446
453
  );
447
454
  `;
448
- const shouldAddUseClientDirective = checkShouldAddUseClientDirective(json, options);
449
455
  const str = (0, dedent_1.dedent) `
450
456
  ${shouldAddUseClientDirective ? `'use client';` : ''}
451
457
  ${getDefaultImport(json, options)}
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.5.2",
25
+ "version": "0.5.3",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {