Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for inline localized values for Builder #1659

Merged
merged 13 commits into from
Jan 22, 2025

Conversation

mrkoreye
Copy link
Collaborator

@mrkoreye mrkoreye commented Jan 14, 2025

Description

This is a WIP PR to support inline localization for the builder parser. Next step is to add support to the generator. The idea is that when we recognize a property value as a localized value, we set the mitosis value to the Default and store the original localized value as a string on a $ prefixed property. Then, (not completed yet) we will take the $ prefixed values to create a localized value when converting back to Builder.

Make sure to follow the PR preparation steps in CONTRIBUTING.md before submitting your PR:

  • format the codebase: from the root, run yarn fmt:prettier.
  • update all snapshots (in core & CLI): from the root, run yarn test:update
  • add Changeset entry: from the root, run yarn g:changeset and follow the CLI instructions. Alternatively, use the Changeset Github Bot to create the file.

Copy link

changeset-bot bot commented Jan 14, 2025

🦋 Changeset detected

Latest commit: 862d587

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@builder.io/mitosis Patch
@builder.io/mitosis-cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

nx-cloud bot commented Jan 14, 2025

View your CI Pipeline Execution ↗ for commit 862d587.

Command Status Duration Result
nx run-many --target test ✅ Succeeded 4m 35s View ↗
nx e2e @builder.io/e2e-app ✅ Succeeded 1m 7s View ↗
nx run-many --target build --exclude @builder.i... ✅ Succeeded 1s View ↗
nx build @builder.io/mitosis-site ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2025-01-22 16:56:46 UTC

Copy link

cloudflare-workers-and-pages bot commented Jan 16, 2025

Deploying mitosis with  Cloudflare Pages  Cloudflare Pages

Latest commit: 862d587
Status: ✅  Deploy successful!
Preview URL: https://03d6e70f.mitosis-9uh.pages.dev
Branch Preview URL: https://korey-support-localization.mitosis-9uh.pages.dev

View logs

@mrkoreye mrkoreye marked this pull request as ready for review January 16, 2025 22:55
@mrkoreye mrkoreye requested a review from samijaber as a code owner January 16, 2025 22:55
@mrkoreye mrkoreye changed the title WIP: support inline localized values for Builder Add support for inline localized values for Builder Jan 16, 2025
Copy link
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(mitosisJsx).toMatchSnapshot();

const backToBuilder = componentToBuilder()({ component });
expect(backToBuilder).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I visually compared originalBuilder and backToBuilder using https://www.diffchecker.com, and found some discrepancies in the JSON, specially the structure of the children blocks.

Just wanted to give you a heads-up so you can confirm that the output is indeed what you expect it to be and that it doesn't need to be exactly equal to the original Builder jSON.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for looking closely! I saw that too and assumed that was just part of the mitosis to builder and back process because no other previously written tests were breaking, but maybe I should recheck the changes I made 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked more closely and it seems like my changes are not the cause of this, and that putting the text node in a child when converting back to builder is the current mitosis behavior.

Comment on lines 333 to 344
if (path.startsWith(propertiesPrefix)) {
const key = path.replace(propertiesPrefix, '');
element.properties = element.properties || {};
(element as any).properties[key] = value;
} else if (path === 'linkUrl') {
(element as any).linkUrl = value;
} else if (path.startsWith(componentOptionsPrefix)) {
const key = path.replace(componentOptionsPrefix, '');
if (element.component) {
element.component.options[key] = value;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably use a set helper to do this (I suggest copy/pasting https://github.com/BuilderIO/builder/blob/main/packages/sdks/src/functions/set.ts instead of using the lodash version to keep bundle size low)

Suggested change
if (path.startsWith(propertiesPrefix)) {
const key = path.replace(propertiesPrefix, '');
element.properties = element.properties || {};
(element as any).properties[key] = value;
} else if (path === 'linkUrl') {
(element as any).linkUrl = value;
} else if (path.startsWith(componentOptionsPrefix)) {
const key = path.replace(componentOptionsPrefix, '');
if (element.component) {
element.component.options[key] = value;
}
}
set(element, path, value)

it would be more future proof: you won't need to add more cases here as time goes on

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice, will look at changing that

@mrkoreye
Copy link
Collaborator Author

@samijaber updated

Copy link
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@mrkoreye mrkoreye enabled auto-merge (squash) January 22, 2025 16:50
@mrkoreye mrkoreye merged commit 1d74164 into main Jan 22, 2025
15 checks passed
@mrkoreye mrkoreye deleted the korey/support-localization branch January 22, 2025 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants