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

wip: bundle css #642

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/brisa/src/utils/compile-all/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export default async function compileAll() {
await compileAssets();

try {
const { success, logs, pagesSize } = await compileFiles();
const { success, logs, pagesSize, outputs } = await compileFiles();

if (!success) {
logBuildError('Failed to compile pages', logs);
}

await handleCSSFiles();
await handleCSSFiles(outputs);

return { success, logs, pagesSize };
} catch (e: any) {
Expand Down
8 changes: 5 additions & 3 deletions packages/brisa/src/utils/compile-files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default async function compileFiles() {
const { success, logs, outputs } = await Bun.build({
entrypoints,
outdir: BUILD_DIR,
experimentalCss: true,
sourcemap: IS_PRODUCTION ? undefined : 'inline',
root: SRC_DIR,
target: isNode ? 'node' : 'bun',
Expand Down Expand Up @@ -145,7 +146,7 @@ export default async function compileFiles() {
),
});

if (!success) return { success, logs, pagesSize: {} };
if (!success) return { success, logs, pagesSize: {}, outputs };

if (actionsEntrypoints.length) {
const actionResult = await buildActions({ actionsEntrypoints, define });
Expand Down Expand Up @@ -173,11 +174,12 @@ export default async function compileFiles() {
| ResolveMessage,
],
pagesSize,
outputs,
};
}

if (!IS_PRODUCTION || IS_STATIC_EXPORT) {
return { success, logs, pagesSize };
return { success, logs, pagesSize, outputs };
}

const [generated] = (await generateStaticExport()) ?? [new Map()];
Expand Down Expand Up @@ -255,7 +257,7 @@ export default async function compileFiles() {
console.log(LOG_PREFIX.INFO, 'Φ JS shared by all');
console.log(LOG_PREFIX.INFO);

return { success, logs, pagesSize: pagesSize };
return { success, logs, pagesSize: pagesSize, outputs };
}

async function compileClientCodePage(
Expand Down
34 changes: 18 additions & 16 deletions packages/brisa/src/utils/handle-css-files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import path from 'node:path';
import fs from 'node:fs';
import { getConstants } from '@/constants';
import { logError } from '../log/log-build';
import { gzipSync } from 'bun';
import { gzipSync, type BuildArtifact } from 'bun';
import { brotliCompressSync } from 'node:zlib';

export default async function handleCSSFiles() {
export default async function handleCSSFiles(outputs: BuildArtifact[]) {
try {
const { BUILD_DIR, CONFIG, LOG_PREFIX, IS_BUILD_PROCESS, IS_PRODUCTION } =
getConstants();
const publicFolder = path.join(BUILD_DIR, 'public');
const allFiles = fs.readdirSync(BUILD_DIR);
const cssFiles = outputs.filter(
(o) => o.kind === 'asset' && o.path.endsWith('.css'),
);
const cssFilePaths: string[] = [];
const integrations = (CONFIG?.integrations ?? []).filter(
(integration) => integration.transpileCSS,
Expand All @@ -20,8 +22,6 @@ export default async function handleCSSFiles() {

// Using CSS integrations
if (integrations.length > 0) {
const cssFiles = allFiles.filter((file) => file.endsWith('.css'));

for (const integration of integrations) {
const startTime = Date.now();

Expand All @@ -34,15 +34,16 @@ export default async function handleCSSFiles() {

let useDefault = true;

for (const file of cssFiles) {
const pathname = path.join(BUILD_DIR, file);
for (const output of cssFiles) {
const fileName = outputToFilename(output);
const pathname = output.path;
const rawContent = fs.readFileSync(pathname, 'utf-8');
const content =
(await integration.transpileCSS?.(pathname, rawContent)) ?? '';
useDefault &&=
integration.defaultCSS?.applyDefaultWhenEvery?.(rawContent) ?? true;
fs.writeFileSync(path.join(publicFolder, file), content);
cssFilePaths.push(file);
fs.writeFileSync(path.join(publicFolder, fileName), content);
cssFilePaths.push(fileName);
}

if (useDefault && integration.defaultCSS) {
Expand Down Expand Up @@ -70,13 +71,10 @@ export default async function handleCSSFiles() {

// Without integrations
else {
for (const file of allFiles) {
if (!file.endsWith('.css')) continue;
fs.renameSync(
path.join(BUILD_DIR, file),
path.join(publicFolder, file),
);
cssFilePaths.push(file);
for (const cssOutput of cssFiles) {
const fileName = outputToFilename(cssOutput);
fs.renameSync(cssOutput.path, path.join(publicFolder, fileName));
cssFilePaths.push(fileName);
}
}

Expand Down Expand Up @@ -113,3 +111,7 @@ export default async function handleCSSFiles() {
});
}
}

function outputToFilename(output: BuildArtifact) {
return path.basename(output.path).replace('.css', `-${output.hash}.css`);
}
Loading