Skip to content

feat: format js and css with prettier if prettier is on the $PATH#1230

Merged
a-h merged 18 commits intomainfrom
format_js_and_css_with_prettier
Aug 11, 2025
Merged

feat: format js and css with prettier if prettier is on the $PATH#1230
a-h merged 18 commits intomainfrom
format_js_and_css_with_prettier

Conversation

@a-h
Copy link
Copy Markdown
Owner

@a-h a-h commented Jul 27, 2025

Closes #481

@a-h a-h requested a review from Copilot July 27, 2025 18:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements automatic formatting of JavaScript and CSS code within templ files using Prettier when it's available on the system PATH. The feature addresses issue #481 by integrating Prettier formatting for script elements and CSS during the templ formatting process.

Key changes:

  • Added a new internal/prettier package to interface with the Prettier CLI tool
  • Created a new internal/format package that handles templ file formatting with script element processing
  • Refactored the format command to use the new formatting infrastructure

Reviewed Changes

Copilot reviewed 17 out of 82 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/prettier/prettier.go New package providing interface to Prettier CLI for formatting JS/CSS
internal/prettier/prettier_test.go Tests for the Prettier integration functionality
internal/format/templ.go New main formatting logic for templ files with script element processing
internal/format/scriptelement.go Specialized formatting for script elements using Prettier
parser/v2/types.go Added WriteStart and WriteEnd methods to ScriptElement for flexible output
cmd/templ/fmtcmd/main.go Refactored to use new formatting package instead of inline logic
flake.nix Added Prettier as a development dependency
go.mod Moved natefinch/atomic from direct to indirect dependency

return nil
}

func showWhitespace(s string) string {
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

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

The showWhitespace function appears to be a debug utility that should be removed or moved to test code since it's only used by the debug print statement.

Copilot uses AI. Check for mistakes.
@a-h
Copy link
Copy Markdown
Owner Author

a-h commented Jul 27, 2025

@joerdav - this is currently a bit rough around the edges, but it works. I think this might be a good way to get CSS and JS formatting into templ.

@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Jul 28, 2025

Can take a look this week! Just back off from a week of house move shenanigans

@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Jul 28, 2025

Cool yeah this looks good so far!

@a-h a-h marked this pull request as ready for review August 2, 2025 14:47
@a-h
Copy link
Copy Markdown
Owner Author

a-h commented Aug 2, 2025

Added CSS formatting, and refactored all the tests to use prettier instead of htmldiff - might as well reduce dependencies.

@therealparmesh
Copy link
Copy Markdown
Contributor

therealparmesh commented Aug 2, 2025

Looks good!

Hopefully this replaces my little hack: https://github.com/therealparmesh/prettier-plugin-templ-script

Will this be compatible with Tailwind’s prettier plugin for sorting classes?

@a-h
Copy link
Copy Markdown
Owner Author

a-h commented Aug 2, 2025

@therealparmesh - not at the moment, because it's only formatting the contents of <script> and <style> elements, but the approach could be extended to apply prettier rules to constant attribute values to sort class names.

@a-h
Copy link
Copy Markdown
Owner Author

a-h commented Aug 11, 2025

Thanks for reviewing that beast of a PR!

@a-h a-h merged commit 273e0ed into main Aug 11, 2025
9 checks passed
@a-h a-h deleted the format_js_and_css_with_prettier branch August 11, 2025 13:55
dgrundel pushed a commit to dgrundel/templ that referenced this pull request Aug 16, 2025
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.

question: Does/Should javascript blocks be formatted with templ fmt command?

4 participants