/
processor.go
273 lines (209 loc) · 5.63 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package html
import (
"regexp"
"strings"
"github.com/mateussouzaweb/compactor/compactor"
"github.com/mateussouzaweb/compactor/os"
"github.com/mateussouzaweb/compactor/pkg/css"
"github.com/mateussouzaweb/compactor/pkg/generic"
"github.com/mateussouzaweb/compactor/pkg/javascript"
"github.com/mateussouzaweb/compactor/pkg/typescript"
)
// Init processor
func Init(bundle *compactor.Bundle) error {
return os.NodeRequire("html-minifier", "html-minifier")
}
// ExtractAttribute find the value of the attribute
func ExtractAttribute(html string, attribute string, defaultValue string) string {
regex := regexp.MustCompile(attribute + `=("([^"]*)"|'([^']*)')`)
match := regex.FindStringSubmatch(html)
if match != nil {
return strings.Trim(match[1], `'"`)
}
return defaultValue
}
// Related processor
func Related(item *compactor.Item) ([]compactor.Related, error) {
var related []compactor.Related
// Detect imports
regex := regexp.MustCompile(`<!-- @import ?("(.+)"|'(.+)') -->`)
matches := regex.FindAllStringSubmatch(item.Content, -1)
for _, match := range matches {
source := match[0]
path := strings.Trim(match[1], `'"`)
file := os.Resolve(path, os.Dir(item.Path))
related = append(related, compactor.Related{
Type: "partial",
Dependency: true,
Source: source,
Path: path,
Item: compactor.Get(file),
})
}
// Detect scripts
regex = regexp.MustCompile(`<script(.+)?>(.+)?</script>`)
matches = regex.FindAllStringSubmatch(item.Content, -1)
for _, match := range matches {
code := match[0]
src := ExtractAttribute(code, "src", "")
// Ignore if is not a src script
if src == "" {
continue
}
// Ignore protocol scripts, only handle relative and absolute paths
if strings.Contains(src, "://") {
continue
}
file := os.Resolve(src, os.Dir(item.Path))
related = append(related, compactor.Related{
Type: "other",
Dependency: false,
Source: code,
Path: src,
Item: compactor.Get(file),
})
}
// Detect links
regex = regexp.MustCompile(`<link(.+)?\/?>`)
matches = regex.FindAllStringSubmatch(item.Content, -1)
for _, match := range matches {
code := match[0]
rel := ExtractAttribute(code, "rel", "")
href := ExtractAttribute(code, "href", "")
as := ExtractAttribute(code, "as", "")
if rel == "" || href == "" {
continue
}
if rel != "stylesheet" && (rel == "preload" && as == "") {
continue
}
file := os.Resolve(href, os.Dir(item.Path))
related = append(related, compactor.Related{
Type: "other",
Dependency: false,
Source: code,
Path: href,
Item: compactor.Get(file),
})
}
return related, nil
}
// Minify HTML content
func Minify(content string) (string, error) {
file := os.TemporaryFile("minify.html")
defer os.Delete(file)
err := os.Write(file, content, 0775)
if err != nil {
return content, err
}
_, err = os.Exec(
"html-minifier",
"--output", file,
"--collapse-whitespace",
"--conservative-collapse",
"--remove-comments",
"--remove-script-type-attributes",
"--remove-style-link-type-attributes",
"--use-short-doctype",
"--minify-urls", "true",
"--minify-css", "true",
"--minify-js", "true",
"--ignore-custom-fragments", "/{{[{]?(.*?)[}]?}}/",
file,
)
if err != nil {
return content, err
}
content, err = os.Read(file)
return content, err
}
// MergeContent returns the content of the item with the replaced partials dependencies
func MergeContent(item *compactor.Item) string {
if !item.Exists {
return ""
}
content := item.Content
for _, related := range item.Related {
if related.Type == "partial" && related.Item.Exists {
// Solves recursively
content = strings.Replace(
content,
related.Source,
MergeContent(related.Item),
1,
)
}
}
return content
}
// Execute processor
func Execute(bundle *compactor.Bundle) error {
content := MergeContent(bundle.Item)
for _, related := range bundle.Item.Related {
if related.Dependency {
continue
}
var err error
var file string
extension := os.Extension(related.Path)
path := related.Path
if extension == ".css" {
file, err = css.Resolve(path, bundle.Item)
} else if extension == ".sass" || extension == ".scss" {
file, err = css.Resolve(path, bundle.Item)
} else if extension == ".js" || extension == ".mjs" {
file, err = javascript.Resolve(path, bundle.Item)
} else if extension == ".ts" || extension == ".mts" {
file, err = typescript.Resolve(path, bundle.Item)
} else if extension == ".tsx" || extension == ".jsx" {
file, err = typescript.Resolve(path, bundle.Item)
} else {
continue
}
if err != nil {
return err
}
content = strings.Replace(content, path, file, 1)
}
destination := bundle.ToDestination(bundle.Item.Path)
perm := bundle.Item.Permission
err := os.Write(destination, content, perm)
if err != nil {
return err
}
return nil
}
// Optimize processor
func Optimize(bundle *compactor.Bundle) error {
if !bundle.ShouldCompress(bundle.Item.Path) {
return nil
}
destination := bundle.ToDestination(bundle.Item.Path)
content, err := os.Read(destination)
if err != nil {
return err
}
content, err = Minify(content)
if err != nil {
return err
}
perm := bundle.Item.Permission
err = os.Write(destination, content, perm)
if err != nil {
return err
}
return nil
}
// Plugin return the compactor plugin instance
func Plugin() *compactor.Plugin {
return &compactor.Plugin{
Namespace: "html",
Extensions: []string{".html", ".htm"},
Init: Init,
Related: Related,
Resolve: generic.Resolve,
Execute: Execute,
Optimize: Optimize,
Delete: generic.Delete,
}
}