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

refactor(ssr): reduce duplicated text rendering logic #4991

Merged
merged 3 commits into from
Dec 3, 2024

Conversation

nolanlawson
Copy link
Collaborator

@nolanlawson nolanlawson commented Dec 2, 2024

Details

I got bugged by seeing so much repeated code in the generated JS. So I extracted some of it into a shared runtime helper.

Before:

didBufferTextContent = true;
{
  const value = instance.foo;
  textContentBuffer += value == null ? '' : String(value);
}
didBufferTextContent = true;
{
  const value = instance.bar;
  textContentBuffer += value == null ? '' : String(value);
}
didBufferTextContent = true;
{
  const value = instance.baz;
  textContentBuffer += value == null ? '' : String(value);
}

After:

didBufferTextContent = true;
textContentBuffer += renderTextContent(instance.foo);
didBufferTextContent = true;
textContentBuffer += renderTextContent(instance.bar);
didBufferTextContent = true;
textContentBuffer += renderTextContent(instance.baz);

No we just need to have optimizeAdjacentYieldStmts also optimize for adjacent text content boilerplate, since Terser doesn't seem smart enough to collapse this.

Does this pull request introduce a breaking change?

  • 😮‍💨 No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • 🤞 No, it does not introduce an observable change.

@nolanlawson nolanlawson requested a review from a team as a code owner December 2, 2024 23:51
packages/@lwc/ssr-runtime/src/render-text-content.ts Outdated Show resolved Hide resolved
@@ -21,20 +24,15 @@ import type { Transformer } from '../types';

const bBufferTextContent = esTemplateWithYield`
didBufferTextContent = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 Could we make the buffer state part of the context object that we pass around? Might help address this repetition.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't be fully at compile time, because directives like lwc:if are unknown until runtime. But maybe it could mitigate it?

@nolanlawson nolanlawson enabled auto-merge (squash) December 3, 2024 20:12
@nolanlawson nolanlawson merged commit b3e5ca4 into master Dec 3, 2024
11 checks passed
@nolanlawson nolanlawson deleted the nolan/extract-render-text branch December 3, 2024 20:16
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