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

Preserve "use client" module boundaries #516

Merged
merged 6 commits into from
Mar 24, 2023
Merged

Preserve "use client" module boundaries #516

merged 6 commits into from
Mar 24, 2023

Conversation

emmatown
Copy link
Member

@emmatown emmatown commented Feb 9, 2023

No description provided.

@changeset-bot
Copy link

changeset-bot bot commented Feb 9, 2023

🦋 Changeset detected

Latest commit: 0d24b15

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

This PR includes changesets to release 1 package
Name Type
@preconstruct/cli Minor

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

@codecov
Copy link

codecov bot commented Feb 9, 2023

Codecov Report

Patch coverage: 90.47% and project coverage change: -0.08 ⚠️

Comparison is base (57576f9) 90.90% compared to head (4f3fb49) 90.82%.

❗ Current head 4f3fb49 differs from pull request most recent head 0d24b15. Consider uploading reports for the commit 0d24b15 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #516      +/-   ##
==========================================
- Coverage   90.90%   90.82%   -0.08%     
==========================================
  Files          33       34       +1     
  Lines        1495     1537      +42     
  Branches      422      425       +3     
==========================================
+ Hits         1359     1396      +37     
- Misses        130      135       +5     
  Partials        6        6              
Impacted Files Coverage Δ
packages/cli/src/build/rollup.ts 91.30% <ø> (ø)
...ckages/cli/src/rollup-plugins/server-components.ts 90.47% <90.47%> (ø)

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

if (!output.meta) {
output.meta = {};
}
output.meta.isUseClientEntry = true;
Copy link
Member

Choose a reason for hiding this comment

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

perhaps it would be a good idea to prefix this property with something like _preconstruct_?

Copy link
Member Author

Choose a reason for hiding this comment

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

We control all the Rollup plugins used so it doesn't really matter

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right - good point. I forgot that this isn't anyhow exposed.

const moduleInfo = this.getModuleInfo(chunk.facadeModuleId);
if (moduleInfo?.meta.isUseClientEntry) {
const magicString = new MagicString(code);
magicString.prepend('"use client";\n');
Copy link
Member

Choose a reason for hiding this comment

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

i guess this is because Rollup might drop the directive, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Rollup actually throws a warning if you have directives so this removes the directive and then adds it back.

Copy link
Member

Choose a reason for hiding this comment

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

Do u know if that's also the case in Rollup 3 (IIRC we are still on Rollup 2 here)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, it might be worth creating a Rollup issue about this. I suspect that more people might need to handle those directives in the near-ish future.

Copy link
Member Author

Choose a reason for hiding this comment

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

tbh, imo the current behaviour is the most sensible option. There's no correct answer to bundling directives so erroring on it seems like the ideal thing.

}
if (node.expression.value === "use client") {
let magicString = new MagicString(code);
this.emitFile({ type: "chunk", id });
Copy link
Member

Choose a reason for hiding this comment

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

I guess this is the core of the change that allows Rollup to preserve this file as a separate chunk, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

Comment on lines 842 to 854
"src/b.js": js`
export const B = "b";
`,
"src/c.js": js`
import { D } from "./d";
export function C() {
return D;
}
`,
"src/d.js": js`
"use client";
export const D = "d";
`,
Copy link
Member

Choose a reason for hiding this comment

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

those end up being unused - was this intentional?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, need to fix that, it was from debugging

@emmatown emmatown marked this pull request as ready for review March 24, 2023 04:09
@emmatown emmatown enabled auto-merge (squash) March 24, 2023 04:10
@emmatown emmatown merged commit d363c88 into main Mar 24, 2023
@emmatown emmatown deleted the server-components branch March 24, 2023 04:15
@github-actions github-actions bot mentioned this pull request Mar 24, 2023
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.

None yet

2 participants