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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ updates:
- "🤖 Dependencies"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/minify/" # Location of package manifests
labels:
- "🤖 Dependencies"
schedule:
interval: "daily"
5 changes: 5 additions & 0 deletions .github/testdata/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; https://editorconfig.org/
root = true

[.github/testdata2/*]
end_of_line = unset
77 changes: 77 additions & 0 deletions .github/testdata/minify_expected_data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# test cases expected data
HtmlMinify: <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><p>Hello, Fiber!</p><input type="hidden" disabled><br></body></html>
html_with_css_default: >-
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>
/* This a comment */
body {
font-family: sans-serif;
font-size: 16px;
color: #333333;
/*
Sets the background color to white
and the text color to dark gray
*/
background-color: #ffffff;
}
</style></head><body><p>Hello, Fiber!</p></body></html>
html_with_js_default: >-
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script>
// This is a comment
alert('Hello, Fiber!')
</script></head><body><p>Hello, Fiber!</p></body></html>
html_with_css_and_js: >-
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>
/* This a css comment */
body {
font-family: sans-serif;
font-size: 16px;
color: #333333;
/*
Sets the background color to white
and the text color to dark gray
*/
background-color: #ffffff;
}
</style></head><body><p>Hello, Fiber!</p><script>
// This is a js comment
alert('Hello, Fiber!')
</script></body></html>
html_config_options_styles_false: >-
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>
/* This a css comment */
body {
font-family: sans-serif;
font-size: 16px;
color: #333333;
/*
Sets the background color to white
and the text color to dark gray
*/
background-color: #ffffff;
}
</style></head><body><p>Hello, Fiber!</p><script>alert('Hello, Fiber!')</script></body></html>
html_config_options_js_false: >-
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{font-family:sans-serif;font-size:16px;color:#333;background-color:#fff}</style></head><body><p>Hello, Fiber!</p><script>
// This is a js comment
alert('Hello, Fiber!')
</script></body></html>
html_config_options_css_js_false: >-
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>
/* This a css comment */
body {
font-family: sans-serif;
font-size: 16px;
color: #333333;
/*
Sets the background color to white
and the text color to dark gray
*/
background-color: #ffffff;
}
</style></head><body><p>Hello, Fiber!</p><script>
// This is a js comment
alert('Hello, Fiber!')
</script></body></html>
html_config_options_css_js_true: <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{font-family:sans-serif;font-size:16px;color:#333;background-color:#fff}</style></head><body><p>Hello, Fiber!</p><script>alert('Hello, Fiber!')</script></body></html>
css_minify: body{font-family:Arial,sans-serif;font-size:16px;line-height:1.5;color:#333;background-color:#fff}h1{font-size:32px;margin-bottom:10px}@media only screen and (max-width:768px){body{font-size:14px;line-height:1.3}h1{font-size:24px;margin-bottom:5px}}@media only screen and (max-width:480px){body{font-size:12px;line-height:1.2}h1{font-size:20px;margin-bottom:5px}}
js_minify: var x=10;function factorial(n){if(n<=1){return 1;};return n*factorial(n-1);};var double=function(x){return x*2;};var numbers=[1,2,3,4,5];for(var i=0;i<numbers.length;i++){numbers[i]=double(numbers[i]);};var person={name:"John",age:30,greet:function(){console.log("Hello, my name is "+this.name+" and I'm "+this.age+" years old.");}};person.greet();
30 changes: 30 additions & 0 deletions .github/testdata/template-with-css-and-js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* This a css comment */
body {
font-family: sans-serif;
font-size: 16px;
color: #333333;
/*
Sets the background color to white
and the text color to dark gray
*/
background-color: #ffffff;
}
</style>
</head>
<body>
<!-- This is a html comment -->
<p>Hello, Fiber!</p>
<script>
// This is a js comment
alert('Hello, Fiber!')
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions .github/testdata/template-with-css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* This a comment */
body {
font-family: sans-serif;
font-size: 16px;
color: #333333;
/*
Sets the background color to white
and the text color to dark gray
*/
background-color: #ffffff;
}
</style>
</head>
<body>
<p>Hello, Fiber!</p>
</body>
</html>
16 changes: 16 additions & 0 deletions .github/testdata/template-with-js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// This is a comment
alert('Hello, Fiber!')
</script>
</head>
<body>
<p>Hello, Fiber!</p>
</body>
</html>
40 changes: 40 additions & 0 deletions .github/testdata/template.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* This is a comment
spanning multiple lines */
body {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
background-color: #fff;
}

/* This is a single-line comment */
h1 {
font-size: 32px;
margin-bottom: 10px;
}

@media only screen and (max-width: 768px) {
/* This is a nested comment */
body {
font-size: 14px;
line-height: 1.3;
}

h1 {
font-size: 24px;
margin-bottom: 5px;
}
}

@media only screen and (max-width: 480px) {
body {
font-size: 12px;
line-height: 1.2;
}

h1 {
font-size: 20px;
margin-bottom: 5px;
}
}
15 changes: 15 additions & 0 deletions .github/testdata/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>Hello, Fiber!</p>
<!-- This is a comment -->
<input type="hidden" disabled="" />
<br />
</body>
</html>
40 changes: 40 additions & 0 deletions .github/testdata/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This is a single-line comment
var x = 10; // This is also a single-line comment

/*
This is a multi-line comment
It can span across multiple lines
*/

// Function that returns the factorial of a number
function factorial(n) {
if (n <= 1) {
return 1;
};
return n * factorial(n - 1); // Recursive call
};

// Anonymous function assigned to a variable
var double = function(x) {
return x * 2;
};

// Array of numbers
var numbers = [1, 2, 3, 4, 5];

// Loop through the array and double each number
for (var i = 0; i < numbers.length; i++) {
numbers[i] = double(numbers[i]);
};

// Object with properties and methods
var person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello, my name is " + this.name + " and I'm " + this.age + " years old.");
}
};

// Call the greet method
person.greet();
19 changes: 19 additions & 0 deletions .github/workflows/release-drafter-minify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release Drafter Minify
on:
push:
# branches to consider in the event; optional, defaults to all
branches:
- master
- main
paths:
- 'minify/**'
jobs:
draft_release_casbin:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: release-drafter/release-drafter@v5
with:
config-name: release-drafter-minify.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ jobs:
working-directory: ./fiberzerolog
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
# -----
# -----
- name: Run Gosec (minify)
working-directory: ./minify
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
# -----
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ jobs:
run: cd ./fiberi18n && go test ./... -v -race
- name: Test fiberzerolog Middleware
run: cd ./fiberzerolog && go test ./... -v -race
- name: Test minify Middleware
run: cd ./minify && go test ./... -v race
80 changes: 80 additions & 0 deletions minify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Minify
Minify middleware for [Fiber](https://github.com/gofiber/fiber). The middleware handles minifying HTML, CSS and JavaScript responses.

### Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Minify HTML Options](#minifyhtmloptions)
- [Minify CSS Options](#minifycssoptions)
- [Minify JS Options](#minifyjsoptions)


### Signatures
```go
func New(config ...Config) fiber.Handler
```

### Examples
Import the middleware package that is part of the Fiber web framework
```go
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/minify"
)
```

Then create a Fiber app with app := fiber.New().

After you initiate your Fiber app, you can use the following possibilities:

### Default Config

```go
app.Use(minify.New())
```

### Custom Config

```go
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](#minifyhTMLoptions) | `yes` | `MinifyHTMLOptions` |
| MinifyCSS | `bool` | Enable / Disable CSS minfy | `yes` | `false` |
| MinifyCSSOptions | `MinifyCSSOptions` | [Options for the MinifyCSS](#MinifyCSSOptions) | `yes` | `MinifyCSSOptions` |
| MinifyJS | `bool` | Enable / Disable JS minfy | `yes` | `false` |
| MinifyJSOptions | `MinifyJSOptions` | [Options for the MinifyJS](#MinifyJSOptions) | `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"}` |
Loading