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

Format instead of minify #458

Open
verdverm opened this issue Jan 19, 2022 · 10 comments
Open

Format instead of minify #458

verdverm opened this issue Jan 19, 2022 · 10 comments

Comments

@verdverm
Copy link

how realistic or feasible would it be to leverage minify for code formatting rather than minifying?

@tdewolff
Copy link
Owner

tdewolff commented Jan 20, 2022

That is a little easier than minifying, unless you'd like to infer meaningful variable names (I'm thinking of training a neural net for that). I mean, the parser is already there!

The truth is I have limited time, and apart from SiteGround's support for maintaining this library, nobody really appreciates the immense work that goes into these projects. I'd say 80% of the interactions I have with people is because something doesn't work or they want something from me (no jab at you), which aren't very positive interactions to be honest (some people even get mad). Then there is competition with other packages that try to make you irrelevant...all in all not a great deal to be completely frank. The projects I do in real-life do have a significant impact, with palpable results and visibly happy faces, and above all I get paid. So besides maintaining this library and slightly improving it bit by bit, I'm not very motivated to continue development on new stuff. Sorry for the undirected rant...

@verdverm
Copy link
Author

I'm mostly looking to see if it is feasible to build on minify to create a Go native version of prettier.io

Not a feature request at this point, I may be open to contributing such functionality. Wondering if there might be a way to use rules that are defined outside of the program itself

@tdewolff
Copy link
Owner

Absolutely you can, it's actually pretty easy. You can use the parse library and create something similar to minify.

@jimafisk
Copy link

@verdverm did you ever make any progress on this? I was looking to format HTML (fix indentation, remove excessive whitespace, etc) and am already using github.com/tdewolff/minify and github.com/tdewolff/parse so figured it would be nice to stick with a solution that leverages these tools.

I did find 2 other projects that are implementing something similar in case it's helpful for anyone (I haven't tried them yet):

I'm sorry to hear the frustrations with supporting these projects @tdewolff. Open source is the best, but is often thankless. I just wanted to drop a quick note saying how much I appreciate your work, these projects are immensely helpful for my own side projects. Thank you for pouring so much effort into these and sharing them with all of us!

@verdverm
Copy link
Author

verdverm commented May 21, 2024

@jimafisk I went another path

  1. Use the best tool for a language, or at least a very common one. It becomes difficult to maintain something in one language for all other languages

  2. Wrap said tool in a container, api server, and cli

You can run hof fmt ./... (or any file glob) and it will format all files that it can, including html

https://docs.hofstadter.io/code-generation/formatting/

If you want custom formatters or rules, that is possible as well

@tdewolff
Copy link
Owner

There is a JS formatter already, the HTML/XML/SVG formatters are in the pipeline (specifically, building an AST from their sources which allows creating a formatter), and an improved CSS parser as well (to support rule nesting). I will re-open this issue to keep you updated! This will probably happen in a few months time though.

Thanks for the interest and the kind words, that is really appreciated. Great to hear that these libraries are so useful to you!

@tdewolff tdewolff reopened this May 21, 2024
@jimafisk
Copy link

Thank you for the quick replies @verdverm and @tdewolff!

There is a JS formatter already

That's amazing 🙌 this is something I was hoping for as well. I'd love to see an example of this if it's something that's easy to point to or demonstrate.

the HTML/XML/SVG formatters are in the pipeline (specifically, building an AST from their sources which allows creating a formatter), and an improved CSS parser as well (to support rule nesting). I will re-open this issue to keep you updated!

This is great news, I appreciate you keeping us in the loop!

This will probably happen in a few months time though.

Totally fine by me, I'm excited it's on the roadmap! Thanks so much!

@tdewolff
Copy link
Owner

Excellent! To pretty-print JS you can do something along these lines:

import "github.com/tdewolff/parse/v2"
import "github.com/tdewolff/parse/v2/js"

func main() {
    input := []byte(...)
    ast, err := js.Parse(parse.NewInputBytes(input), js.Options{})
    if err != nil {
        panic(err)
    }

    pretty := ast.JSString()
    ...
}

See the documentation for other options. There is an ast.JS(io.Writer) method that probably more efficient for your use-case. Also be aware that input should ideally have some additional unused capacity (1 byte to be precise) to avoid reallocation.

@jimafisk
Copy link

Thank you for the pretty-print JS example @tdewolff, that was so helpful! I just tried it out in my project and it worked great!

@tdewolff
Copy link
Owner

Awesome to hear, and thanks so much for sponsoring the project!!

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

No branches or pull requests

3 participants