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

Make client reference ID unique within a module #43

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/webpack-rsc/src/webpack-rsc-server-loader.cts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ const webpackRscServerLoader: webpack.LoaderDefinitionFunction<webpackRscServerL
const exportedClientReferences: t.ExportNamedDeclaration[] = [];

for (const {exportName} of exportedFunctions) {
const id = `${path.relative(process.cwd(), resourcePath)}`;
const id = `${path.relative(
process.cwd(),
resourcePath,
)}#${exportName}`;

clientReferences.push({id, exportName});
addedRegisterReferenceCall = `Client`;

Expand Down
54 changes: 46 additions & 8 deletions packages/webpack-rsc/src/webpack-rsc-server-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe(`webpackRscServerLoader`, () => {
);

const output = await callLoader(resourcePath, new Map());
const expectedId = path.relative(process.cwd(), resourcePath);
const idPrefix = path.relative(process.cwd(), resourcePath);

expect(output).toEqual(
`
Expand All @@ -66,17 +66,55 @@ function createClientReferenceProxy(exportName) {
throw new Error(\`Attempted to call \${exportName}() from the server but \${exportName} is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.\`);
};
}
export const ComponentA = registerClientReference(createClientReferenceProxy("ComponentA"), "${expectedId}", "ComponentA");
export const ComponentB = registerClientReference(createClientReferenceProxy("ComponentB"), "${expectedId}", "ComponentB");
export const ComponentC = registerClientReference(createClientReferenceProxy("ComponentC"), "${expectedId}", "ComponentC");
export const ComponentD = registerClientReference(createClientReferenceProxy("ComponentD"), "${expectedId}", "ComponentD");
export const ComponentE = registerClientReference(createClientReferenceProxy("ComponentE"), "${expectedId}", "ComponentE");
export const ComponentF = registerClientReference(createClientReferenceProxy("ComponentF"), "${expectedId}", "ComponentF");
export const ComponentA = registerClientReference(createClientReferenceProxy("ComponentA"), "${idPrefix}#ComponentA", "ComponentA");
export const ComponentB = registerClientReference(createClientReferenceProxy("ComponentB"), "${idPrefix}#ComponentB", "ComponentB");
export const ComponentC = registerClientReference(createClientReferenceProxy("ComponentC"), "${idPrefix}#ComponentC", "ComponentC");
export const ComponentD = registerClientReference(createClientReferenceProxy("ComponentD"), "${idPrefix}#ComponentD", "ComponentD");
export const ComponentE = registerClientReference(createClientReferenceProxy("ComponentE"), "${idPrefix}#ComponentE", "ComponentE");
export const ComponentF = registerClientReference(createClientReferenceProxy("ComponentF"), "${idPrefix}#ComponentF", "ComponentF");
`.trim(),
);
});

// TODO: Add missing expectation for clientReferencesMap.
test(`populates the given client references map`, async () => {
const clientReferencesMap: ClientReferencesMap = new Map();

const resourcePath = path.resolve(
currentDirname,
`__fixtures__/client-components.js`,
);

await callLoader(resourcePath, clientReferencesMap);

expect(Object.fromEntries([...clientReferencesMap.entries()])).toEqual({
[resourcePath]: [
{
exportName: `ComponentA`,
id: `src/__fixtures__/client-components.js#ComponentA`,
},
{
exportName: `ComponentB`,
id: `src/__fixtures__/client-components.js#ComponentB`,
},
{
exportName: `ComponentC`,
id: `src/__fixtures__/client-components.js#ComponentC`,
},
{
exportName: `ComponentD`,
id: `src/__fixtures__/client-components.js#ComponentD`,
},
{
exportName: `ComponentE`,
id: `src/__fixtures__/client-components.js#ComponentE`,
},
{
exportName: `ComponentF`,
id: `src/__fixtures__/client-components.js#ComponentF`,
},
],
});
});

test(`adds 'registerServerReference' calls to all exported functions of a module with a 'use server' directive`, async () => {
const resourcePath = path.resolve(
Expand Down
Loading