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

fix(build): co-relate details with the correct output #666

Merged
merged 2 commits into from
Dec 5, 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
40 changes: 40 additions & 0 deletions packages/brisa/src/utils/client-build/pages-build/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import clientPageBuild from '.';
import { getConstants } from '@/constants';
import getWebComponentsList from '@/utils/get-web-components-list';
import type { BuildArtifact } from 'bun';
import type { WCsEntrypoints } from '../types';

const src = path.join(import.meta.dir, '..', '..', '..', '__fixtures__');
const build = path.join(src, `out-${crypto.randomUUID()}}`);
Expand Down Expand Up @@ -189,4 +190,43 @@ describe('client-build', () => {
// Brisa element usage
expect(output[0].code).toContain('._P)');
});

it('should correctly associate web components with entrypoints', async () => {
const temp = path.join(src, 'pages', '.temp-test-files');

if (fs.existsSync(temp)) {
fs.rmSync(temp, { recursive: true });
}
fs.mkdirSync(temp);

const entrypoints = [];
const webComponentsPerEntrypoint: WCsEntrypoints = {};
const allWCs = {};

for (let i = 0; i < 20; i += 1) {
const wcPath = path.join(temp, `wc-${i}-test.tsx`);

fs.writeFileSync(wcPath, `export default () => <wc-${i}-test />;`);
entrypoints.push(wcPath);
Object.assign(allWCs, { [`wc-${i}-test`]: wcPath });

Object.assign(webComponentsPerEntrypoint, {
[wcPath]: { [`wc-${i}-test`]: wcPath },
});
}

const output = await clientPageBuild(entrypoints.map(toArtifact), {
allWebComponents: allWCs,
webComponentsPerEntrypoint,
layoutWebComponents: {},
});

fs.rmSync(temp, { recursive: true });
expect(output.length).toEqual(entrypoints.length);

for (const details of output) {
const pathname = path.parse(details.pagePath).name;
expect(details.code).toContain(pathname);
}
});
});
28 changes: 17 additions & 11 deletions packages/brisa/src/utils/client-build/pages-build/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BuildArtifact } from 'bun';
import { parse } from 'node:path';
import { log, logBuildError } from '../../log/log-build';
import { removeTempEntrypoints } from '../fs-temp-entrypoint-manager';
import { getClientBuildDetails } from '../get-client-build-details';
Expand All @@ -17,11 +18,7 @@ export default async function clientPageBuild(

let clientBuildDetails = await getClientBuildDetails(pages, options);

const entrypointsData = clientBuildDetails.reduce((acc, curr, index) => {
if (curr.entrypoint) acc.push({ ...curr, index });
return acc;
}, [] as EntryPointData[]);

const entrypointsData = clientBuildDetails.filter((p) => p.entrypoint);
const entrypoints = entrypointsData.map((p) => p.entrypoint!);

if (entrypoints.length === 0) {
Expand All @@ -46,14 +43,23 @@ export default async function clientPageBuild(
return clientBuildDetails;
}

const outputsPerFilename = outputs.reduce(
(acc, artifact) => {
acc[parse(artifact.path).name] = artifact;
return acc;
},
{} as Record<string, BuildArtifact>,
);

await Promise.all(
outputs.map(async (output, i) => {
const index = entrypointsData[i].index!;
clientBuildDetails.map(async (details) => {
if (!details.entrypoint) return;

const filename = parse(details.entrypoint).name;
const code = await outputsPerFilename[filename].text();

clientBuildDetails[index] = {
...clientBuildDetails[index],
...processI18n(await output.text()),
};
// Mutate the details object to include the compiled code + i18n details
Object.assign(details, processI18n(code));
}),
);

Expand Down
4 changes: 4 additions & 0 deletions packages/brisa/src/utils/snake-to-camelcase/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ describe('utils', () => {
it('should work with upper case letters', () => {
expect(snakeToCamelCase('some-EXAMPLE-1')).toBe('someExample1');
});

it('should remove "."', () => {
expect(snakeToCamelCase('some.example')).toBe('someExample');
});
});
});
4 changes: 2 additions & 2 deletions packages/brisa/src/utils/snake-to-camelcase/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const SNAKE_TO_CAMEL_CASE_REGEX = /([-_]([a-z]|[0-9]))/g;
const SNAKE_TO_CAMEL_CASE_REGEX = /([-_\.]([a-z]|[0-9]))/g;

export default function snakeToCamelCase(str: string) {
return str
Expand All @@ -7,5 +7,5 @@ export default function snakeToCamelCase(str: string) {
}

function replaceSnakeToCamelCase(group: string) {
return group.toUpperCase().replace('-', '').replace('_', '');
return group.toUpperCase().replace('-', '').replace('_', '').replace('.', '');
}
Loading