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

MINOR: Introduce new proxy implementation version #32

Merged
merged 13 commits into from
Dec 5, 2024
Merged

Conversation

tkvlnk
Copy link
Collaborator

@tkvlnk tkvlnk commented Nov 29, 2024

Motivation:

Current generated files are huge and in fact mix into the bundle duplication of the messages file
So I got an idea to use a trick with typed Proxy object to be used in runtime
and the huge part of the file would be a typescript interface which would be stripped out on bundling

return ${useTranslateFn ? `t(prevKeys, ...argArray)` : 'prevKeys'};
},
});`;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

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 actually stole this approach from this package 😅

https://github.com/wix-private/data-hooks

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same 😄 .. I just wanted to make it support nesting, and fit our dataHooks approach

ownValueAlias: string;
useTranslateFn: boolean;
}) {
return `const ${creatorFnName} = <R extends string>(t: (...args: unknown[]) => R = (k) => k as R, prevKeys: string = ''): unknown =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

are you adding the return type of localeKeys and also the translations in english?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

im not sure im getting ur question
moreover I did refactor implementation and covered it with tests

could u plz take a look at this piece again? or we can catchap directly

@tkvlnk tkvlnk changed the base branch from master to migrate-to-jest-snapshots December 3, 2024 10:23
@tkvlnk tkvlnk requested a review from varzager December 3, 2024 10:26
});

export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) {
return createProxyImpl(t) as ILocaleKeys;
Copy link
Collaborator

Choose a reason for hiding this comment

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

that is what i was wondering.. perfect!

src/bin.ts Outdated
@@ -72,6 +72,12 @@ const cliDefinition = yargs(hideBin(process.argv)).command(
type: 'boolean',
describe: 'Generate React bindings (Provider and hook)',
})
.option('experimental_proxyImpl', {
Copy link
Collaborator

Choose a reason for hiding this comment

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

is the plan to add it as an option, or eventually just replaced the approach from big object to the proxy approach?
I think it won't be breaking changes, and you should make it the only option.
can you think of any reason it will break for consumers?

Copy link
Collaborator Author

@tkvlnk tkvlnk Dec 3, 2024

Choose a reason for hiding this comment

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

plan was to eventually replace to proxy as default
I played safe with the flag
but in general I'm also good with replacing it and having it enabled by default (bundlu reducing boost right away on new version!)

I will remove flag and enable this by default 👌

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@varzager done ✅

Base automatically changed from migrate-to-jest-snapshots to master December 3, 2024 12:41
@tkvlnk tkvlnk requested a review from varzager December 4, 2024 07:55
@tkvlnk tkvlnk marked this pull request as ready for review December 4, 2024 07:55
@tkvlnk tkvlnk changed the title Proxy version MINOR: Introduce new proxy implementation version Dec 4, 2024
@tkvlnk
Copy link
Collaborator Author

tkvlnk commented Dec 4, 2024

@varzager also updated docs and changelog

order: {
shippingLabel: {
customerDetailsCard: {
address: (data: Record<'firstName' | 'lastName' | 'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => t('order.shippingLabel.customerDetailsCard.address', data), /* {{firstName}} {{lastName}}, {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, concat ', '}}{{state, suffix ' '}}{{zipCode, suffix ', '}}{{country}} */
address: (data: Record<'firstName' | 'lastName' | 'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => string, /* {{firstName}} {{lastName}}, {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, concat ', '}}{{state, suffix ' '}}{{zipCode, suffix ', '}}{{country}} */
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have one concern!
and maybe it's only me doing it this way...
many times I search the content in localeKeys file and easily copy the their path from the actual function which makes it really easy while developing:
t('order.shippingLabel.customerDetailsCard.address', data)
now with the new approach this won't be possible.
option 1: It's not really important
option 2: we can add it as a comment before the value

It is just something i thought about, wanted to hear from you if you can relate, or we can just live without it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

option 2: we can add it as a comment before the value

sounds good to me
will do

@tkvlnk tkvlnk merged commit cdc61b7 into master Dec 5, 2024
1 check passed
@tkvlnk tkvlnk deleted the proxy-version branch December 5, 2024 14:48
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