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

Bump tailwind to v4 stable + Build error will prevent a success build #721

Merged
merged 1 commit into from
Jan 30, 2025
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
12 changes: 6 additions & 6 deletions packages/brisa-tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
},
"files": ["index.ts", "index.d.ts", "libs.json"],
"devDependencies": {
"@tailwindcss/postcss": "4.0.0-alpha.33",
"postcss": "8.4.49",
"tailwindcss": "4.0.0-alpha.33"
"@tailwindcss/postcss": "4.0.1",
"postcss": "8.5.1",
"tailwindcss": "4.0.1"
},
"peerDependencies": {
"@tailwindcss/postcss": "4.0.0-alpha.33",
"postcss": "8.4.48",
"tailwindcss": "4.0.0-alpha.33"
"@tailwindcss/postcss": "^4.0.0",
"postcss": "^8.4.48",
"tailwindcss": "^4.0.1"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
186 changes: 89 additions & 97 deletions packages/brisa/src/utils/handle-css-files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,126 +2,118 @@ import path from 'node:path';
import type { Loader } from 'bun';
import fs from 'node:fs';
import { getConstants } from '@/constants';
import { logError } from '../log/log-build';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why clear the logError with the try-catch? This is to give error feedback of the build in the terminal.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, it's fine because the error is handled upper

import { gzipSync, Glob } from 'bun';
import { brotliCompressSync } from 'node:zlib';

const cssGlob = new Glob('**/*.css');

export default async function handleCSSFiles() {
try {
const {
BUILD_DIR,
CONFIG,
LOG_PREFIX,
IS_BUILD_PROCESS,
IS_PRODUCTION,
SRC_DIR,
} = getConstants();
const publicFolder = path.join(BUILD_DIR, 'public');

if (!fs.existsSync(publicFolder)) fs.mkdirSync(publicFolder);
const {
BUILD_DIR,
CONFIG,
LOG_PREFIX,
IS_BUILD_PROCESS,
IS_PRODUCTION,
SRC_DIR,
} = getConstants();
const publicFolder = path.join(BUILD_DIR, 'public');

if (!fs.existsSync(publicFolder)) fs.mkdirSync(publicFolder);

const cssFilePaths: string[] = await handleCSSInsidePublic(
BUILD_DIR,
publicFolder,
);
const integrations = (CONFIG?.integrations ?? []).filter(
(integration) => integration.transpileCSS,
);

const cssFilePaths: string[] = await handleCSSInsidePublic(
BUILD_DIR,
// Using CSS integrations
if (integrations.length > 0) {
// Use the "src" CSS files to transpile it with the integration parser
// instead of the "build" because they are not transpiled by Bun CSS Parser
const cssFilePathsFromSrc = await handleCSSInsidePublic(
SRC_DIR,
publicFolder,
);
const integrations = (CONFIG?.integrations ?? []).filter(
(integration) => integration.transpileCSS,
);
for (const file of cssFilePathsFromSrc) {
if (!cssFilePaths.includes(file)) cssFilePaths.push(file);
}

// Using CSS integrations
if (integrations.length > 0) {
// Use the "src" CSS files to transpile it with the integration parser
// instead of the "build" because they are not transpiled by Bun CSS Parser
const cssFilePathsFromSrc = await handleCSSInsidePublic(
SRC_DIR,
publicFolder,
);
for (const file of cssFilePathsFromSrc) {
if (!cssFilePaths.includes(file)) cssFilePaths.push(file);
}
for (const integration of integrations) {
const startTime = Date.now();

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

if (IS_BUILD_PROCESS) {
console.log(
LOG_PREFIX.WAIT,
`transpiling CSS with ${integration.name}...`,
);
}

let useDefault = true;

for (const file of cssFilePaths) {
const pathname = path.join(publicFolder, file);
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);
}

if (useDefault && integration.defaultCSS) {
const content =
(await integration.transpileCSS?.(
'base.css',
integration.defaultCSS.content,
)) ?? '';
const filename = `base-${Bun.hash(content)}.css`;
fs.writeFileSync(path.join(publicFolder, filename), content);
cssFilePaths.unshift(filename);
}

if (IS_BUILD_PROCESS) {
const endTime = Date.now();
const ms = ((endTime - startTime) / 1000).toFixed(2);
console.log(
LOG_PREFIX.INFO,
LOG_PREFIX.TICK,
`CSS transpiled with ${integration.name} in ${ms}ms`,
);
}
if (IS_BUILD_PROCESS) {
console.log(
LOG_PREFIX.WAIT,
`transpiling CSS with ${integration.name}...`,
);
}
}

// Compression to gzip & brotli
if (IS_PRODUCTION && CONFIG.assetCompression) {
const start = Date.now();
let useDefault = true;

for (const file of cssFilePaths) {
const buffer = fs.readFileSync(path.join(publicFolder, file));
Bun.write(
path.join(publicFolder, file + '.gz'),
gzipSync(buffer as any) as any,
);
Bun.write(
path.join(publicFolder, file + '.br'),
brotliCompressSync(buffer as any) as any,
const pathname = path.join(publicFolder, file);
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);
}

if (useDefault && integration.defaultCSS) {
const content =
(await integration.transpileCSS?.(
'base.css',
integration.defaultCSS.content,
)) ?? '';
const filename = `base-${Bun.hash(content)}.css`;
fs.writeFileSync(path.join(publicFolder, filename), content);
cssFilePaths.unshift(filename);
}

if (IS_BUILD_PROCESS) {
const endTime = Date.now();
const ms = ((endTime - startTime) / 1000).toFixed(2);
console.log(
LOG_PREFIX.INFO,
LOG_PREFIX.TICK,
`CSS transpiled with ${integration.name} in ${ms}ms`,
);
}
}
}

// Compression to gzip & brotli
if (IS_PRODUCTION && CONFIG.assetCompression) {
const start = Date.now();

const ms = ((Date.now() - start) / 1000).toFixed(2);
console.log(
LOG_PREFIX.INFO,
LOG_PREFIX.TICK,
`CSS files compressed successfully in ${ms}ms`,
for (const file of cssFilePaths) {
const buffer = fs.readFileSync(path.join(publicFolder, file));
Bun.write(
path.join(publicFolder, file + '.gz'),
gzipSync(buffer as any) as any,
);
Bun.write(
path.join(publicFolder, file + '.br'),
brotliCompressSync(buffer as any) as any,
);
}

// Write css-files.js
fs.writeFileSync(
path.join(BUILD_DIR, 'css-files.js'),
'export default ' + JSON.stringify(cssFilePaths),
const ms = ((Date.now() - start) / 1000).toFixed(2);
console.log(
LOG_PREFIX.INFO,
LOG_PREFIX.TICK,
`CSS files compressed successfully in ${ms}ms`,
);
} catch (e: any) {
logError({
messages: ['Failed to handle CSS files', e.message],
stack: e.stack,
});
}

// Write css-files.js
fs.writeFileSync(
path.join(BUILD_DIR, 'css-files.js'),
'export default ' + JSON.stringify(cssFilePaths),
);
}

async function handleCSSInsidePublic(dir: string, outDir: string) {
Expand Down
Loading