Skip to content

Conversation

@Imran-imtiaz48
Copy link

I converted the script from a callback-based approach to one that uses fs/promises with async/await, which makes the code cleaner and easier to read. Instead of appending CSS files one by one using fs.appendFile—which can cause race conditions because file reads happen asynchronously—I updated the logic so that all CSS files are read first, combined into a single string using join("\n"), and then written to bundle.css in one go. This ensures the output is always consistent and avoids partial or out-of-order writes. I also added a step to automatically create the output directory with fs.mkdir(..., { recursive: true }) so the script will not fail if project-dist does not already exist. The file order is preserved based on how readdir returns them, but the code can easily be extended to sort alphabetically if needed. Finally, I wrapped the entire process in a try/catch block with a clear log message on success, which makes the script more robust and user-friendly.

I converted the script from a callback-based approach to one that uses fs/promises with async/await, which makes the code cleaner and easier to read. Instead of appending CSS files one by one using fs.appendFile—which can cause race conditions because file reads happen asynchronously—I updated the logic so that all CSS files are read first, combined into a single string using join("\n"), and then written to bundle.css in one go. This ensures the output is always consistent and avoids partial or out-of-order writes. I also added a step to automatically create the output directory with fs.mkdir(..., { recursive: true }) so the script will not fail if project-dist does not already exist. The file order is preserved based on how readdir returns them, but the code can easily be extended to sort alphabetically if needed. Finally, I wrapped the entire process in a try/catch block with a clear log message on success, which makes the script more robust and user-friendly.
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.

1 participant