tdewolff / minify

Go minifiers for web formats
https://go.tacodewolff.nl/minify
MIT License
3.66k stars 212 forks source link

Format instead of minify #458

Open verdverm opened 2 years ago

verdverm commented 2 years ago

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

tdewolff commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

jimafisk commented 2 months ago

@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 commented 2 months ago

@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 commented 2 months ago

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!

jimafisk commented 2 months ago

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

There is a JS formatter already

That's amazing :raised_hands: 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 commented 2 months ago

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 commented 2 months ago

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 commented 1 month ago

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