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

🚀 Add minify contrib #563

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alwismt
Copy link

@alwismt alwismt commented Apr 29, 2023

🚀 Add minifying HTML, CSS and JavaScript responses

Description

This PR adds a new Minify middleware for Fiber. The middleware handles minifying HTML, CSS and JavaScript responses. This is located in the minify directory and contains the following files:

  • config.go: defines the configuration options for the middleware.
  • css_minify.go: contains the logic for minifying CSS responses.
  • html_minify.go: contains the logic for minifying HTML responses.
  • js_minify.go: contains the logic for minifying JavaScript responses.
  • minify.go: contains the main middleware logic.
  • css_minify_test.go, html_minify_test.go, js_minify_test.go, minify_test.go: contain test cases for each file and every possible test case.

Related

🚀 Feature: Add minify middleware

Changes:

  • Added new middleware for minifying HTML, CSS and JavaScript responses.

  • Created a new minify directory in the root directory and added the necessary files.

  • Added configuration options for enabling/disabling minification for HTML, CSS and JavaScript responses.

  • Added configuration options for excluding specific URLs and styles/scripts from minification.

  • Added default configuration options and a helper function for setting default values.

  • Added test cases for each file and possible test cases.

  • New feature (non-breaking change which adds functionality)

  • This change requires a documentation update

Usage:

app.Use(minify.New())

Custom Config

cfg := minify.Config{
    MinifyHTML:        true,
    MinifyHTMLOptions: MinifyHTMLOptions{
      MinifyScripts: true,
      MinifyStyles:  true
    },
    MinifyCSS:         true,
    MinifyJS:          true,
    Method:            GET,
}

app.Use(minify.New(cfg))

Config

Property Type Description Optional Default
MinifyHTML bool Enable / Disable Html minfy yes true
MinifyHTMLOptions MinifyHTMLOptions Options for the MinifyHTML yes MinifyHTMLOptions
MinifyCSS bool Enable / Disable CSS minfy yes false
MinifyCSSOptions MinifyCSSOptions Options for the MinifyCSS yes MinifyCSSOptions
MinifyJS bool Enable / Disable JS minfy yes false
MinifyJSOptions MinifyJSOptions Options for the MinifyJS yes MinifyJSOptions
Method Method Representation of minify route method yes GET
Next func(c *fiber.Ctx) bool Skip this middleware when returned true yes nil

MinifyHTMLOptions

Property Type Description Default
MinifyScripts bool Whether scripts inside the HTML should be minified or not false
MinifyStyles bool Whether styles inside the HTML should be minified or not false
ExcludeURLs []string URLs Exclud from minification nil

MinifyCSSOptions

Property Type Description Default
ExcludeStyles []string Styles exclud from minification []string{"*.min.css", "*.bundle.css"}

MinifyJSOptions

Property Type Description Default
ExcludeScripts []string Styles exclud from minification []string{"*.min.js", "*.bundle.js"}

minify/go.mod Outdated Show resolved Hide resolved
minify/minify_test.go Show resolved Hide resolved
minify/minify_test.go Show resolved Hide resolved
@alwismt alwismt requested a review from gaby April 29, 2023 16:42
@gaby
Copy link
Member

gaby commented Apr 29, 2023

@alwismt I ran all the code in the PR through GPT-4 which suggested around 4-5 fixes on each file. One thing that caught my attention is that in all the files it says the implementation is only good for small files, larger files would suffer from performance issues and suggestion using a library to do the minimization. It also mentioned how some tags/etc were not taken into account.

After doing a quick search, reading on reddit found this lib that covers all the use cases: https://github.com/tdewolff/minify

Would it make more sense to use that to build a middleware for fiber rather than try to implement our own minimizing logic?

@alwismt
Copy link
Author

alwismt commented May 1, 2023

@gaby I agree that using an external library like like https://github.com/tdewolff/minify would be a more efficient solution, especially for larger files. I will integrate it into the minify middleware for fiber. Thanks for your suggestion!

@ReneWerner87 ReneWerner87 added the ✏️ Feature New feature or request label May 7, 2023
@gaby
Copy link
Member

gaby commented May 16, 2023

@alwismt Any progress on this?

@gaby
Copy link
Member

gaby commented Jun 3, 2023

@alwismt Friendly ping :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Feature New feature or request 💡 Help wanted Extra attention is needed ♻ Wait for Response
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants