-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
module.go
130 lines (116 loc) · 2.67 KB
/
module.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
/*
Copyright © 2014–2020 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
"bytes"
"fmt"
"log"
"path/filepath"
"strings"
)
func loadModules(filenames []string, encoding string) []byte {
var (
processedModules = make(map[string]bool)
headTags [][]byte
)
for _, filename := range filenames {
if processedModules[filename] {
log.Printf("warning: load %s: Skipping duplicate.", filename)
continue
}
var (
source []byte
err error
)
switch normalizedFileExt(filename) {
// NOTE: The case values here should match those in `filesystem.go:knownFileType()`.
case "css":
source, err = loadModuleTagged("style", filename, encoding)
case "js":
source, err = loadModuleTagged("script", filename, encoding)
case "otf", "ttf", "woff", "woff2":
source, err = loadModuleFont(filename)
default:
// Simply ignore all other file types.
continue
}
if err != nil {
log.Fatalf("error: load %s: %s", filename, err.Error())
}
if len(source) > 0 {
headTags = append(headTags, source)
}
processedModules[filename] = true
statsAddExternalFile(filename)
}
return bytes.Join(headTags, []byte("\n"))
}
func loadModuleTagged(tag, filename, encoding string) ([]byte, error) {
source, err := fileReadAllWithEncoding(filename, encoding)
if err != nil {
return nil, err
}
source = bytes.TrimSpace(source)
if len(source) == 0 {
return source, nil
}
var (
idSlug = tag + "-module-" + slugify(strings.Split(filepath.Base(filename), ".")[0])
mimeType string
b bytes.Buffer
)
switch tag {
case "script":
mimeType = "text/javascript"
case "style":
mimeType = "text/css"
}
if _, err := fmt.Fprintf(
&b,
`<%s id=%q type=%q>%s</%[1]s>`,
tag,
idSlug,
mimeType,
source,
); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func loadModuleFont(filename string) ([]byte, error) {
source, err := fileReadAllAsBase64(filename)
if err != nil {
return nil, err
}
var (
family = strings.Split(filepath.Base(filename), ".")[0]
idSlug = "style-module-" + slugify(family)
ext = normalizedFileExt(filename)
mediaType = mediaTypeFromExt(ext)
hint string
b bytes.Buffer
)
switch ext {
case "ttf":
hint = "truetype"
case "otf":
hint = "opentype"
default:
hint = ext
}
if _, err := fmt.Fprintf(
&b,
"<style id=%q type=\"text/css\">@font-face {\n\tfont-family: %q;\n\tsrc: url(\"data:%s;base64,%s\") format(%q);\n}</style>",
idSlug,
family,
mediaType,
source,
hint,
); err != nil {
return nil, err
}
return b.Bytes(), nil
}